Solved

Update Mysql using php

Posted on 2011-09-16
7
357 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 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 109

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 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

821 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