Jump to content

Archived

This topic is now archived and is closed to further replies.

jmif96

Updating a Table with PHP

Recommended Posts

Hey guys,

I can't seem to get this script to update a record in my MySQL database for me. It takes input from a form (POST) and assigns them variables. It then connects to the database, assigns the query to $query, and runs the query inside of an if statement to test if it worked. But I keep getting an error and when I check the database it hasn't updated.

Here's part of the code:


$query = "UPDATE Users SET AIM = '$AIM', YIM = '$YIM', MSN = '$MSN', website = '$website', Location = '$location', RW_Expirence = '$RW_Expirence', VATSIM ID = '$VATSIM', Auto = '$auto' WHERE `First` = '$First' AND Last = '$Last'";

if (mysql_query($query)) {
print "Your profile has been updated.";
?>
<form> <input type="button" value="Run Again" onClick="window.location = 'profile.php'"></form>
<?php
}
else {
print ("Uh Oh! There was an error, please try again.");
?>
<form><input type="button" value="Run Again" onClick="window.location = 'profile.php'"></form>
<?php
}

mysql_close();
?>


Any help is appreciated, thanks!

Share this post


Link to post
Share on other sites
do this:

mysql_query($query) or die(mysql_error());

and post what the error is.

also do this:

echo $query;

and post what it echos, as well.

(this should probably be in the php forum, btw)

Share this post


Link to post
Share on other sites
[!--quoteo(post=372453:date=May 8 2006, 07:28 PM:name=John Dunne)--][div class=\'quotetop\']QUOTE(John Dunne @ May 8 2006, 07:28 PM) [snapback]372453[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Hey guys,

I can't seem to get this script to update a record in my MySQL database for me. It takes input from a form (POST) and assigns them variables. It then connects to the database, assigns the query to $query, and runs the query inside of an if statement to test if it worked. But I keep getting an error and when I check the database it hasn't updated.

Here's part of the code:
$query = "UPDATE Users SET AIM = '$AIM', YIM = '$YIM', MSN = '$MSN', website = '$website', Location = '$location', RW_Expirence = '$RW_Expirence', VATSIM ID = '$VATSIM', Auto = '$auto' WHERE `First` = '$First' AND Last = '$Last'";

if (mysql_query($query)) {
print "Your profile has been updated.";
?>
<form> <input type="button" value="Run Again" onClick="window.location = 'profile.php'"></form>
<?php
}
else {
print ("Uh Oh! There was an error, please try again.");
?>
<form><input type="button" value="Run Again" onClick="window.location = 'profile.php'"></form>
<?php
}

mysql_close();
?>
Any help is appreciated, thanks!
[/quote]


The correct form for an update statement is:

update tablename
set columnname = value [, columnname = value ...]
where primarykey = pk_value

and you left off the last part.

Share this post


Link to post
Share on other sites
Thanks for your help guys, turns out it didn't know here to place the data because I used the $_POST to get the vars. So that messed up the header, thus messing up some session variables which I was using to place the table.

Thanks again!

Share this post


Link to post
Share on other sites
Actually, I do have a question.

When I submit the form I send it to xxx.php?ID=USER ID (taken from session).

And when I do this and run it the update works but if you just logged in and ran it for the first time during that session, you get this error:

Warning: Unknown(): Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively. in Unknown on line 0


However, it still updates the record, and when you run the script again, it dosen't come up. Is there any way to prevent that message?

Share this post


Link to post
Share on other sites
Well, the error message itself tells you how to turn it off... but it sounds like you should a) not use global variables for the session and b) upgrade your version of PHP.

Share this post


Link to post
Share on other sites
Just to be sure, you turn it off in your php.ini file correct?

Share this post


Link to post
Share on other sites
Sounds like it.

Share this post


Link to post
Share on other sites

×

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.