Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Update Mysql using php

Posted on 2011-09-16
7
Medium Priority
?
362 Views
Last Modified: 2013-12-13
Hi I'm trying to update a record where column name is draw.

$draw = "<a href=/uploads/draw/".$_FILES['userfile']['name']." target=_blank>draw</a>";
   
 
  $conn = db_connect();
  
 $query = ("UPDATE competition SET draw = '$draw' WHERE id ='$ud_id' ");

 $result = $conn->query($query);
 echo $result;

  $conn->close();    
   

Open in new window

When I echo $draw the variable is loaded and there.
When I echo $result it shows 1 so it believes the function is complete.
However the record fiield is still null.
Not sure what I'm missing.
Thanks
0
Comment
Question by:encodeme
7 Comments
 
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 160 total points
ID: 36551856
Try this:

$query = ("UPDATE competition SET draw = '$draw' WHERE id ='" . $ud_id . " ' ");
0
 
LVL 82

Accepted Solution

by:
hielo earned 180 total points
ID: 36551881
>>When I echo $result it shows 1 so it believes the function is complete
OK, but without knowing what your db object is doing, we won't be able to help you.  My best guess is you are returning true on success, false on failure.

What I would do is:
For SELECT, SHOW :  on failure - return False; on success - return mysql_num_rows();
http://www.php.net/manual/en/function.mysql-num-rows.php

For INSERT, UPDATE, REPLACE or DELETE: on failure - return false; on success - return mysql_affected_records();
http://www.php.net/manual/en/function.mysql-affected-rows.php
0
 

Author Comment

by:encodeme
ID: 36551971
Hi Hielo
Something like this ?

$conn = db_connect();
  $query = ("UPDATE competition SET draw = '$draw' WHERE id ='" . $ud_id . " ' ");
 //$query = ("UPDATE competition SET draw = '$draw' WHERE id ='$ud_id' ");

 $result = $conn->query($query);
 if ($result) {
      echo  $conn->affected_rows." competition ammended.";
	  
  } else {
  	  echo "An error has occurred.  The item was not ammended.";
  }
	
  $conn->close();

Open in new window


When I run that it echo's 0 competition ammended
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 160 total points
ID: 36551994
You might need to use this function to prepare the data fields for use in a query:
http://php.net/manual/en/function.mysql-real-escape-string.php

You might want to print out the contents of the fully-resolved query string before you call $conn->query()

You might want to use var_dump($conn) to see the object.  Same thing about $result.  If you use these data visualization techniques you will probably find the issues very quickly.
0
 

Author Comment

by:encodeme
ID: 36552015
Curiously this line works and update the db.
$query = ("UPDATE competition SET draw = 'cat' WHERE id = '21'");

Open in new window


This line did not work
$query = ("UPDATE competition SET draw = 'cat' WHERE id ='" . $ud_id . " ' ");

Open in new window


But I've checked that variable $ud_id has a value..
Now I'm realyy confused.
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 36552083
Sorry, cancel unuseful spaces I inserted to more readibility and delete quotes (for numerical values)

$query = ("UPDATE competition SET draw = 'cat' WHERE id =" . $ud_id);

0
 

Author Closing Comment

by:encodeme
ID: 36552211
Thanks for all the help.
It was a valiant joint effort of giving me clues and tips on how to track the bug.
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question