2tonejoe Posted July 29, 2007 Share Posted July 29, 2007 i am running a script that updates a mySQL database. It runs without error, but it doesn't actually update anything. I have added a few echo's for the variables I am receiving from the form and the changes are coming over it is just that they aren't updating in the db. . . here is my code: <?php include('/Library/WebServer/Documents/scripts/db-connnect.php'); $username = $_POST['name']; $userfacility = $_POST['facility']; $userposition = $_POST['position']; $useremail = $_POST['email']; $usercontact = $_POST['contact']; $userpass = $_POST['pass1']; $userid = $_POST['userid']; $sql = "UPDATE `users` SET `user_name` = '$username', `user_facility` = '$userfacility', `user_position` = '$userposition', `user_email` = '$useremail', `user_contact` = '$usercontact', `user_pass` = '$userpass', `update_date` = NOW( ) WHERE `users`.`user_id` ='$userid' LIMIT 1 ;"; mysql_select_db('addison'); $check = mysql_query("SELECT user_name FROM users WHERE user_name = '$username'") or die ("query 1: " . mysql_error()); $mysql_num = mysql_num_rows($check); echo $mysql_num; if ($mysql_num != 1) { echo "<br /><br /><br /><h2><center>I can't add <b>".$username."</b> because there is no such user.</h2></center>\n"; echo '<br /><h4><center>Please use the <a href="http://website.com/index.php?page=update-a-user">User Update Form</a> or simply <a href="' . $_SERVER['HTTP_REFERER'] . '">Retry</a> it.</h4></center>'; } else { echo $_POST['email']; mysql_query( $sql); echo "<br><br><center><h2>The user <b>".$username."</b> was updated succesfully!</h2></center>\n"; echo '<br /><center><h4><a href="' . $_SERVER['HTTP_REFERER'] . '">Go Back</a></center></h4>'; } mysql_close($mysql_link); ?> anyone have a guess as to what is happening . .? When I insert data it works fine btw. . . . using the came connection file too. Link to comment https://forums.phpfreaks.com/topic/62378-mysql-update-isnt-erroring-but-isnt-doing-anything/ Share on other sites More sharing options...
AndyB Posted July 29, 2007 Share Posted July 29, 2007 $sql = "UPDATE `users` SET ... That query never gets executed. And select the database before executing it. Link to comment https://forums.phpfreaks.com/topic/62378-mysql-update-isnt-erroring-but-isnt-doing-anything/#findComment-310453 Share on other sites More sharing options...
2tonejoe Posted July 29, 2007 Author Share Posted July 29, 2007 i am not following you. . sorry. Link to comment https://forums.phpfreaks.com/topic/62378-mysql-update-isnt-erroring-but-isnt-doing-anything/#findComment-310463 Share on other sites More sharing options...
AndyB Posted July 30, 2007 Share Posted July 30, 2007 You define a query string for the UPDATE but you never execute the query. And you need to select the database BEFORE any of your queries. $sql = "UPDATE users ....."; // define query string $result = mysql_query($sql); // execute the query and update Link to comment https://forums.phpfreaks.com/topic/62378-mysql-update-isnt-erroring-but-isnt-doing-anything/#findComment-310478 Share on other sites More sharing options...
2tonejoe Posted July 30, 2007 Author Share Posted July 30, 2007 but I am . . . or at least I thought I was.... right underneath my "$sql=UPDATE" there is "mysql_select_db('addison');" and then the execution is the fifth line from the bottom "mysql_query( $sql);". Am I simply doing this wrong?? Link to comment https://forums.phpfreaks.com/topic/62378-mysql-update-isnt-erroring-but-isnt-doing-anything/#findComment-310498 Share on other sites More sharing options...
Goose87 Posted July 30, 2007 Share Posted July 30, 2007 when you type $sql = "UPDATE...." you have to type $variable=mysql_query($sql). or just do what you did below and type $sql=mysql_query("UPDATE...") it's still a mysql_query even if it's an update. Link to comment https://forums.phpfreaks.com/topic/62378-mysql-update-isnt-erroring-but-isnt-doing-anything/#findComment-310501 Share on other sites More sharing options...
2tonejoe Posted July 30, 2007 Author Share Posted July 30, 2007 ok. so I change the code to the following and it still does nothing. It does error and outputs "The user myself was updated successfully! <?php include('/Library/WebServer/Documents/scripts/db-connnect.php'); $username = $_POST['name']; $userfacility = $_POST['facility']; $userposition = $_POST['position']; $useremail = $_POST['email']; $usercontact = $_POST['contact']; $userpass = $_POST['pass1']; $userid = $_POST['userid']; $sql = "UPDATE `users` SET `user_name` = '$username', `user_facility` = '$userfacility', `user_position` = '$userposition', `user_email` = '$useremail', `user_contact` = '$usercontact', `user_pass` = '$userpass', `update_date` = NOW( ) WHERE `users`.`user_id` ='$userid' LIMIT 1 ;"; mysql_select_db('addison'); $check = mysql_query("SELECT user_name FROM users WHERE user_name = '$username'") or die ("query 1: " . mysql_error()); $mysql_num = mysql_num_rows($check); if ($mysql_num != 1) { echo "<br /><br /><br /><h2><center>I can't add <b>".$username."</b> because there is no such user.</h2></center>\n"; echo '<br /><h4><center>Please use the <a href="http://222.223.22.35/index.php?page=update-a-user">User Update Form</a> or simply <a href="' . $_SERVER['HTTP_REFERER'] . '">Retry</a> it.</h4></center>'; } else { $queryexecute = mysql_query( $sql); echo "<br><br><center><h2>The user <b>".$username."</b> was updated succesfully!</h2></center>\n"; echo '<br /><center><h4><a href="' . $_SERVER['HTTP_REFERER'] . '">Go Back</a></center></h4>'; } mysql_close($mysql_link); ?> i am lost. sorry for the blank looks guys. i appreciate everyones help. Link to comment https://forums.phpfreaks.com/topic/62378-mysql-update-isnt-erroring-but-isnt-doing-anything/#findComment-310509 Share on other sites More sharing options...
teng84 Posted July 30, 2007 Share Posted July 30, 2007 when you type $sql = "UPDATE...." you have to type $variable=mysql_query($sql). or just do what you did below and type $sql=mysql_query("UPDATE...") it's still a mysql_query even if it's an update. do this after the update statement mysql_query($sql); Link to comment https://forums.phpfreaks.com/topic/62378-mysql-update-isnt-erroring-but-isnt-doing-anything/#findComment-310518 Share on other sites More sharing options...
2tonejoe Posted July 30, 2007 Author Share Posted July 30, 2007 ??? my mind is numb. i am not getting this. you mean directly after i define "$sql=my update" I have to do "mysql_query( $sql);"? as in the next line. . . . I do that and still nothing. . . does anyone have a link to an example or something that could help me understand a little better. . ? Link to comment https://forums.phpfreaks.com/topic/62378-mysql-update-isnt-erroring-but-isnt-doing-anything/#findComment-310523 Share on other sites More sharing options...
teng84 Posted July 30, 2007 Share Posted July 30, 2007 your code is weird i try to fix it but tell me what you really want with this code say the objective of the code Link to comment https://forums.phpfreaks.com/topic/62378-mysql-update-isnt-erroring-but-isnt-doing-anything/#findComment-310528 Share on other sites More sharing options...
2tonejoe Posted July 30, 2007 Author Share Posted July 30, 2007 just gather the form data and update the db based on the id given that is it Link to comment https://forums.phpfreaks.com/topic/62378-mysql-update-isnt-erroring-but-isnt-doing-anything/#findComment-310533 Share on other sites More sharing options...
teng84 Posted July 30, 2007 Share Posted July 30, 2007 try and tell me what happen <?php include('/Library/WebServer/Documents/scripts/db-connnect.php'); $username = $_POST['name']; $userfacility = $_POST['facility']; $userposition = $_POST['position']; $useremail = $_POST['email']; $usercontact = $_POST['contact']; $userpass = $_POST['pass1']; $userid = $_POST['userid']; $sql = "UPDATE `users` SET `user_name` = '".$username."', `user_facility` = '".$userfacility."', `user_position` = '".$userposition."', `user_email` = '".$useremail."', `user_contact` = '".$usercontact."', `user_pass` = '".$userpass."', `update_date` = NOW( ) WHERE users.user_id ='".$userid."' LIMIT 1 ;"; mysql_select_db('addison'); $check = mysql_query("SELECT user_name FROM users WHERE user_name = '$username'") or die ("query 1: " . mysql_error()); $mysql_num = mysql_num_rows($check); if ($mysql_num != 1) { echo "<br /><br /><br /><h2><center>I can't add <b>".$username."</b> because there is no such user.</h2></center>\n"; echo '<br /><h4><center>Please use the <a href="http://222.223.22.35/index.php?page=update-a-user">User Update Form</a> or simply <a href="' . $_SERVER['HTTP_REFERER'] . '">Retry</a> it.</h4></center>'; } else { $queryexecute = mysql_query( $sql)or die (mysql_error()); echo "<br><br><center><h2>The user <b>".$username."</b> was updated succesfully!</h2></center>\n"; echo '<br /><center><h4><a href="' . $_SERVER['HTTP_REFERER'] . '">Go Back</a></center></h4>'; } mysql_close($mysql_link); ?> Link to comment https://forums.phpfreaks.com/topic/62378-mysql-update-isnt-erroring-but-isnt-doing-anything/#findComment-310535 Share on other sites More sharing options...
2tonejoe Posted July 30, 2007 Author Share Posted July 30, 2007 same thing. . . it works fine but doesnt update the db. is there some witch I am missing? If I change the query (and nothing else) to an insert query. It will insert the data fine . . . wtf??? Link to comment https://forums.phpfreaks.com/topic/62378-mysql-update-isnt-erroring-but-isnt-doing-anything/#findComment-310538 Share on other sites More sharing options...
teng84 Posted July 30, 2007 Share Posted July 30, 2007 try to change this condition to more specific one look if ($mysql_num != 1) and echo this $mysql_num and see wats the result and u uername in your select i guess its better using id and can you explain the if else that you have Link to comment https://forums.phpfreaks.com/topic/62378-mysql-update-isnt-erroring-but-isnt-doing-anything/#findComment-310540 Share on other sites More sharing options...
ss32 Posted July 30, 2007 Share Posted July 30, 2007 if you are sure that the query is executing, first check that the WHERE clause is correct (you may be using an incorrect variable or something while putting the username string in there). reason i ask is that userid is typically a number and it doesnt look like you put one in there Link to comment https://forums.phpfreaks.com/topic/62378-mysql-update-isnt-erroring-but-isnt-doing-anything/#findComment-310544 Share on other sites More sharing options...
2tonejoe Posted July 30, 2007 Author Share Posted July 30, 2007 can someone just link me a tutorial page on how to do this? something has got to be wrong and I cant figure it out. .. . Link to comment https://forums.phpfreaks.com/topic/62378-mysql-update-isnt-erroring-but-isnt-doing-anything/#findComment-310578 Share on other sites More sharing options...
ss32 Posted July 30, 2007 Share Posted July 30, 2007 The fact that you can INSERT and not UPDATE is a clear sign that something is wrong with your query. as i said, check your WHERE statement Link to comment https://forums.phpfreaks.com/topic/62378-mysql-update-isnt-erroring-but-isnt-doing-anything/#findComment-310663 Share on other sites More sharing options...
2tonejoe Posted July 30, 2007 Author Share Posted July 30, 2007 The fact that you can INSERT and not UPDATE is a clear sign that something is wrong with your query. as i said, check your WHERE statement I agree. However. . . I can't find the problem. Can anyone notice what I am doing wrong?? Link to comment https://forums.phpfreaks.com/topic/62378-mysql-update-isnt-erroring-but-isnt-doing-anything/#findComment-310835 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.