Solved

Update Mysql using php

Posted on 2011-09-16
7
354 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 30

Assisted Solution

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

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

Accepted Solution

by:
hielo earned 45 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
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 40 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 30

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now