0

I'm trying to create an update function in PHP but the records don't seem to be changing as per the update. I've created a JSON object to hold the values being passed over to this file and according to the Firebug Lite console I've running these values are outputted just fine so it's prob something wrong with the sql side. Can anyone spot a problem? I'd appreciate the help!

<?php

$var1 = $_REQUEST['action']; // We dont need action for this tutorial, but in a complex code you need a way to determine ajax action nature
$jsonObject = json_decode($_REQUEST['outputJSON']); // Decode JSON object into readable PHP object

$name = $jsonObject->{'name'}; // Get name from object
$desc = $jsonObject->{'desc'}; // Get desc from object
$did = $jsonObject->{'did'};// Get id object


mysql_connect("localhost","root","");  // Conect to mysql, first parameter is location, second is mysql username and a third one is a mysql password
@mysql_select_db("findadeal") or die( "Unable to select database"); // Connect to database called test


$query = "UPDATE deal SET dname = {'$name'}, desc={'$desc'} WHERE dealid = {'$did'}";
$add = mysql_query($query);
$num = mysql_num_rows($add);

if($num != 0) {

echo "true";

} else {

echo "false";

} 

?>
1
  • Note there's a difference between {'$name'} and '{$name}' . Maybe you WHERE condition is not correct. Commented Feb 19, 2013 at 18:11

3 Answers 3

1

I believe you are misusing the curly braces. The single quote should go on the outside of them.:

"UPDATE deal SET dname = {'$name'}, desc={'$desc'} WHERE dealid = {'$did'}"

Becomes

"UPDATE deal SET dname = '{$name}', desc='{$desc}' WHERE dealid = '{$did}'"

On a side note, using any mysql_* functions isn't really good security-wise. I would recommend looking into php's mysqli or pdo extensions.

Sign up to request clarification or add additional context in comments.

2 Comments

We have a winner! Thanks so much for that! Who knew it'd be something so small like that. I know, it was brought to my attention before. I had very basic SQL experience so I was going on that at the start of my teaching but once I've the PHP side of things down and passing variables I plan on looking deeper in mysqi functions! Thanks so much for your time Kyle!
@user2025934 Not a problem. I am glad that I was able to help. Those curly braces always seem to be confusing.
1

You need to escape reserved words in MySQL like desc with backticks

UPDATE deal 
SET dname = {'$name'}, `desc`= {'$desc'} ....
                       ^----^--------------------------here

2 Comments

Even better, you need to check for errors before doing anything else.
Im new to coding and never even knew where was reserve words but thanks for bringing it to my attention :) I'm giving a look through your link now. That's unfortunately not after fixing my problem however =/
1

you need to use mysql_affected_rows() after update not mysql_num_rows

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.