Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/200550-cannot-update-database/
Share on other sites

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);
?>

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.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.