Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mySQL update 1

Status
Not open for further replies.

Nukoi

Programmer
Dec 20, 2001
82
US
hey guys..
I am pretty new with mysql and I cant figure out why this won't work for the life of me.. i am trying to do an update profile thing, info is passed from a form on one page to the php script that executes the actual updating..
it says that it works but when i check the database nothing has been changed. here is the live example of what happens..


the php script that tries to update it is this:

if ($HTTP_POST_VARS['password']== $HTTP_POST_VARS['password2']){
if (strstr($HTTP_POST_VARS['email'], "@") and strstr($HTTP_POST_VARS['email'], ".")){

$error=false;
$username=$_SESSION['username'];
$password=$HTTP_POST_VARS['password'];
$first=$HTTP_POST_VARS['first'];
$last=$HTTP_POST_VARS['last'];
$email=$HTTP_POST_VARS['email'];
$quote=$HTTP_POST_VARS['quote'];
$connection = mysql_connect("blah","blah","blah");
$db=mysql_select_db("blah",$connection);
$sql = "UPDATE users SET Login='$username', Password='$password', First='$first', Last='$last', Email='$email', Quote='$quote' WHERE id='$id'";

if(!mysql_query($sql, $connection)){
echo "failed";
}else{
echo "updated";
//header("Location: ".$_SESSION['return']);
//exit;
}
}else {
$error ="Invalid email address!";
}
}else{
$error ="Passwords do not match!";
}

I know my script is probably pretty weak, but I am new to both php and mysql :)

thanks in advance
//jason
 
Think this may be the trouble:

>> if(!mysql_query($sql, $connection)){

That's like saying 'If you can't call mysql_query with these parameters, do what's next...'

I usually do this:

$result = mysql_query($sql, $connection);
if (!$result) {
echo("Failed".mysql_error());
} else {
echo("Yay!");
}

Which says 'If you don't get a result from the query, do what's next...'

Hope this helps...
 
Thanks DumTech..
No luck tho.. I have been getting it to echo "updated" both with the way it was and the way with assigning $result to the query. Everything that PHP is gathering tells it that it went through ok but it never does the update on the mysql side. this is bizarre :(

Thanks
//Jason
 
Just realized that I overlooked the real problem - the update not working! Sorry about that... :)

To help debug, you can throw an echo $sql; in there before you get to the mysql_query just to see what the function is being passed, and then make sure your failed echo statement is like this:

echo("failed:".mysql_error());

so MySQL can tell you what its problem is.
 
Thanks DumTech!

When I echo'ed $sql i realized that I hadn't set $id=HTTP_POST_VARS['id'];
Stupid mistakes :( Thanks alot for the direction on that one.
 
Ah, 'stupid' mistakes... I make about 47 million of 'em a day... ;)

Glad the direction could help... It's one you can apply to any number of situations... :)
 
haha.. 'stupid' mistakes.. as opposed to the 'smart' mistakes ;) thanks again - i gave you a star

//jason
 
Thanks!

-grin-

Wish you could print those out to hang on the fridge. My wife would be so proud... ;)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top