Jump to content

Updating a Table with PHP


jmif96

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!
Link to comment
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.
Link to comment
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!
Link to comment
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?
Link to comment
Share on other sites

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.