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. Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
2tonejoe Posted July 29, 2007 Author Share Posted July 29, 2007 i am not following you. . sorry. Quote Link to comment 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 Quote Link to comment 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?? Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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); Quote Link to comment 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. . ? Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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); ?> Quote Link to comment 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??? Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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. .. . Quote Link to comment 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 Quote Link to comment 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?? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.