Solved

Update Mysql using php

Posted on 2011-09-16
7
359 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

636 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