Jump to content


Photo

Updating a Table with PHP


  • Please log in to reply
7 replies to this topic

#1 jmif96

jmif96
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 08 May 2006 - 11:28 PM

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!

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 09 May 2006 - 03:33 AM

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)
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 datamodeler

datamodeler
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 09 May 2006 - 04:28 AM

[!--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) View Post[/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.

#4 jmif96

jmif96
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 09 May 2006 - 11:57 PM

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!

#5 jmif96

jmif96
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 10 May 2006 - 12:07 AM

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?

#6 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 10 May 2006 - 03:53 PM

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.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#7 jmif96

jmif96
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 11 May 2006 - 02:34 AM

Just to be sure, you turn it off in your php.ini file correct?

#8 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 13 May 2006 - 05:26 PM

Sounds like it.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users