Solved

Update Mysql using php

Posted on 2011-09-16
7
358 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 110

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
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.

710 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