Jump to content

'Twas the night before Christmas, and mySQL won't accept my Update query.


Recommended Posts

I've messed around with this for three hours now, and it's driving me batty.  I'm running the following Query:

 

mysql_select_db("myDB", $con);

$query = "UPDATE mytable SET Name = '$Name', Address = '$Address', City = '$City', State = '$State', Zip = '$Zip', Phone = '$Phone', Website = '$Website', Type = '$Type' WHERE id = '$ID'";

 

The problem is that it's not updating my database.  I've tried everything.  I've forced the page to Echo out the variables so that I know they're right.  I've run (successful) Delete queries to make sure I'm connecting to the right table.  I've forced it to spit out the variables at the end of the Update.  Everything I've tried works beautifully... except the Update itself.  It doesn't error out or anything- it acts like it's Updating, but the data never changes.  I can add to the database and delete from it, but it refuses to let me Update anything. 

 

I've looked all over the net, and tried every variation I can find.  Nothing works.  I also tried this:

 

$query = "UPDATE `testbed` SET `Name`=[$Name],`Address`=[$Address],`City`=[$City],`State`=[$State],`Zip`=[$Zip],`Type`=[$Type],`Phone`=[&Phone],`website`=[$Website] WHERE `ID`=[$ID]";

 

And

 

$result = mysql_query("UPDATE mytable SET Name = '$Name', Address = '$Address', City = '$City', State = '$State', Zip = '$Zip', Phone = '$Phone', Website = '$Website', Type = '$Type' WHERE id = '$ID'");

 

None of them do anything at all.

What does var_dump($result); show for the version where you assign the returned value to $result = mysql_query(....

 

Hi, and thanks for the response!

 

It shows:

bool(false) var_dump

 

Which I don't understand, unfortunately.

A false value means your query is failing due to an error of some kind.

 

echo mysql_error(); right after the mysql_query() line to find out why the query is failing.

 

Ah.  It appears that one of my Variables isn't coming across from the Edit Form properly.  As you can tell, this is my first large-scale database project.  :)  I'm not sure why the Variable isn't loading correctly, but I'm at least now I know where to look.  Thank you very much!

If the mysql_error message is referring to a point in the query where your external data is at, it is likely that you are not escaping the data before putting it into the query and the data contains special sql characters.

 

See this link - mysql_real_escape_string

Hola! I tried your suggestion.  The same thing happened.  No update.  :(

 

Here's the entirety of my current code:

 

<?php

$con = mysql_connect(localhost,myDB,myPass);

if (!$con)

  die('Could not connect: ' . mysql_error());

 

mysql_select_db("myDB", $con);

 

$query="UPDATE testbed SET Name=\"$_POST[Name]\", Address=\"$_POST[Address]\", State=\"$_POST[state]\" WHERE ID=\"$_POST[iD]\"";

echo mysql_error();

echo "<br>";

echo $query;

echo "<br>";

var_dump($result);

 

mysql_close($con);

?>

 

and here's what the tests return:

mysql error:

SQuery:UPDATE testbed SET Name="Test Name", Address="18501 W. Stanford Road", State="California" WHERE ID="96"

NULL

 

I don't see any errors, and yet the Table refuses to Update.  It just boggles my mind that it could be this hard to update a database entry.  :(

I LOL'ed when I saw your code. For your database to actually ever receive the query you need to call mysql_query() with your actual query as a parameter.

 

Something like this:

$result = mysql_query($query) or trigger_error('ERROR: ' . mysql_error() . '<br>QUERY: ' . $query, E_USER_ERROR);
if ($result && mysql_num_rows($result)) {
    // query executed successful AND it returned a result.
}

My original version, shown at the bottom of my first post, was:

$result = mysql_query("UPDATE mytable SET Name = '$Name', Address = '$Address', City = '$City', State = '$State', Zip = '$Zip', Phone = '$Phone', Website = '$Website', Type = '$Type' WHERE id = '$ID'");

The version that's in there now is what a guy from another board suggested I try.  :D  Since I'm not above any suggestion/ridicule, I put his code in and hoped for the best.  ;)

 

After reading the posts here, I realized that the problem was that I was half right the first time, and the guy who gave me my current code was also half-right.  So I took the right halves, and made this:

 

$result = mysql_query("UPDATE testbed SET Name=\"$_POST[Name]\", Address=\"$_POST[Address]\", City=\"$_POST[City]\" WHERE ID=\"$_POST[iD]\"");

 

And now it seems to work.  :)

 

THANK YOU to PFMaBiSmAD and ignace!  Your advice was the best Christmas Present ever!  My form is now updating!

 

*Edited to show the functioning code.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.