jackfinnerty Posted May 3, 2010 Share Posted May 3, 2010 Hi all, Hopefully you can help me out with the following: I have a fairly simply MySQL database set up with a list of offices. I am creating a page to add/edit/delete offices from the database. I can successfully insert new rows and delete rows in my table but when it comes to updating existing records I have problems. This is the PHP I am using: <?php require("phpsqlajax_dbinfo.php"); $con = mysql_connect('***removed***', $username, $password); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db($database, $con); $ud_inputf0=$_POST['inputf0']; $ud_inputf1=$_POST['inputf1']; $ud_inputf2=$_POST['inputf2']; $ud_inputf3=$_POST['inputf3']; $ud_inputf4=$_POST['inputf4']; $ud_inputf5=$_POST['inputf5']; $ud_inputf6=$_POST['inputf6']; $ud_inputf7=$_POST['inputf7']; $ud_inputf8=$_POST['inputf8']; $ud_inputf9=$_POST['inputf9']; $ud_inputf10=$_POST['inputf10']; $ud_inputf11=$_POST['inputf11']; $ud_inputf12=$_POST['inputf12']; $query="UPDATE markers SET officename='$ud_inputf1', address1='$ud_inputf2', address2='$ud_inputf3', address3='$ud_inputf4', address4='$ud_inputf5', address5='$ud_inputf6', address6='$ud_inputf7', telephone='$ud_inputf8', imageurl='$ud_inputf9', lat='$ud_inputf10', lng='$ud_inputf11', type='$ud_inputf12' WHERE id='$inputf0'"; mysql_query($query); mysql_close($con); echo "Information updated."; ?> The problem is the code runs without any errors and echos "Information updated." at the end but the table does not get updated. I know the variables I am trying to update are getting passed correctly from the HTML form which triggers this code as I can echo them out. Am I missing something pretty obvious here? Thanks for any help you can give. Jack Quote Link to comment https://forums.phpfreaks.com/topic/200550-cannot-update-database/ Share on other sites More sharing options...
ChemicalBliss Posted May 3, 2010 Share Posted May 3, 2010 Yes, a little obvious . WHERE id='$inputf0'"; should be WHERE id='$ud_inputf0'"; It happens sometimes. -cb- Quote Link to comment https://forums.phpfreaks.com/topic/200550-cannot-update-database/#findComment-1052346 Share on other sites More sharing options...
PFMaBiSmAd Posted May 3, 2010 Share Posted May 3, 2010 You need to echo the $query to make sure it contains what you expect (using variables that are not named for the function they perform makes it difficult to write code that does what you want.) Your code does not have any error checking or error reporting logic in it to get it to tell you what it is or is not doing (for example, you should only echo that the information was updated if the query executed and the row was updated.) You code should be doing something like this - <?php if(mysql_query($query)){ // query executed without any errors if(mysql_affected_rows() > 0){ // one or more rows were updated echo "Information updated."; } else { // nothing was updated (either the data was identical or the WHERE clause did not match any rows) echo "Not updated."; } } else { // query failed, do some error reporting echo mysql_error(); } mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/200550-cannot-update-database/#findComment-1052347 Share on other sites More sharing options...
PFMaBiSmAd Posted May 3, 2010 Share Posted May 3, 2010 You should be developing and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON in your master php.ini (so that fatal parse errors will be displayed too) so that php will help you with things like mistyped variable names. Quote Link to comment https://forums.phpfreaks.com/topic/200550-cannot-update-database/#findComment-1052350 Share on other sites More sharing options...
jackfinnerty Posted May 3, 2010 Author Share Posted May 3, 2010 Thanks! Yes pretty obvious in the end... I actually did have error checking code in there but in the end removed it all in case I had made an error in the error checking code! Very impressed with so many responses in a very short time, I will come back! Jack Quote Link to comment https://forums.phpfreaks.com/topic/200550-cannot-update-database/#findComment-1052360 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.