zeinaK Posted May 29, 2011 Share Posted May 29, 2011 Hello, I am trying to update my database using a form. While submitting this function is called, but nothing is changed. If I replace the variables with values, the function works fine, but not with variables. function updateCandidate($id, $F_Name, $L_Name, $Gender, $Bday, $PhoneNo, $Address, $City, $Country, $Nationality, $experience, $Stat) { $q = "UPDATE candidates SET F_Name = '$F_Name', L_Name='$L_Name', Gender='$Gender', Bday='$Bday', PhoneNo='$PhoneNo', Address = '$Address', City='$City', Country = '$Country', Nationality = '$Nationality', Experience_idExperience='$experience', Status= $Stat WHERE Users_UsersID = $id"; return mysql_query($q, $this->connection); Quote Link to comment https://forums.phpfreaks.com/topic/237779-update-the-database/ Share on other sites More sharing options...
PFMaBiSmAd Posted May 29, 2011 Share Posted May 29, 2011 There are a few things that could prevent your query from working - 1) The $id variable either doesn't have any value (this would produce a query error) or it doesn't have a value that matches any row in your table (this would result in a query that affects zero rows), 2) One or more of the variables contains special sql characters and needs to be escaped (this would produce a query error), 3) The $Stat value is a non-numeric value and needs to be enclosed in single-quotes (this would produce a query error.) Have you echoed the $q variable so that you can see what it actually contains? Are you testing the value that mysql_query() returns somewhere so that you know if the query executed without or with an error? If the query executes without any errors, are you using mysql_affected_rows to find out if the query did or didn't update the row? Quote Link to comment https://forums.phpfreaks.com/topic/237779-update-the-database/#findComment-1221893 Share on other sites More sharing options...
zeinaK Posted May 29, 2011 Author Share Posted May 29, 2011 I have tried everything except using mysql_affected_rows(), the mysql_query() returns nothing, I made sure that the $id exist and that the $stat is numeric. Quote Link to comment https://forums.phpfreaks.com/topic/237779-update-the-database/#findComment-1221896 Share on other sites More sharing options...
jcbones Posted May 29, 2011 Share Posted May 29, 2011 What does this function return? function updateCandidate($id, $F_Name, $L_Name, $Gender, $Bday, $PhoneNo, $Address, $City, $Country, $Nationality, $experience, $Stat) { $q = "UPDATE candidates SET F_Name = '$F_Name', L_Name='$L_Name', Gender='$Gender', Bday='$Bday', PhoneNo='$PhoneNo', Address = '$Address', City='$City', Country = '$Country', Nationality = '$Nationality', Experience_idExperience='$experience', Status= $Stat WHERE Users_UsersID = $id"; if(mysql_query($q, $this->connection)) { return true; } else {echo $q . ' has encountered an error<br />' . mysql_error(); return false; } } Quote Link to comment https://forums.phpfreaks.com/topic/237779-update-the-database/#findComment-1221949 Share on other sites More sharing options...
zeinaK Posted May 29, 2011 Author Share Posted May 29, 2011 Thank you! It said that the error was near the WHERE, the $Stat was actually posted empty and I missed it. All fixed now! Thank you again Quote Link to comment https://forums.phpfreaks.com/topic/237779-update-the-database/#findComment-1222024 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.