...

View Full Version : troubles saving data to database



surreal5335
08-04-2011, 08:27 AM
I am updating a row and currently one field is getting updated ($post_desc which gets put into the post_content field). I have gone through and double checked that my form names match the names I am trying to call into my php function.



function update_post()
{
// create new record
if(isset($_POST['update']))
{
//echo 'update submit';
$Database = new mysqli('#############');

mysqli_report(MYSQLI_REPORT_ERROR);





$post_title = htmlentities($_POST['post_title'], ENT_QUOTES);
$new_post_image = htmlentities($_POST['new_post_image'], ENT_QUOTES);
$current_post_image = htmlentities($_POST['current_post_image'], ENT_QUOTES);
$post_desc = htmlentities($_POST['post_desc'], ENT_QUOTES);
$post_id = htmlentities($_POST['post_id'], ENT_QUOTES);

$post_image = $current_post_image;
if($new_post_image != '')
{
$post_image = $new_post_image;
}



$post_title = $Database->real_escape_string($user_title);
$post_image = $Database->real_escape_string($post_image);
$post_desc = $Database->real_escape_string($post_desc);
$post_id = $Database->real_escape_string($post_id);




if ($Database->query("UPDATE posts SET post_title='".$post_title."', post_image='".$post_image."', post_content='".$post_desc."' WHERE post_id='".$post_id."'" ))
{



}
else
{
echo 'Error: could not prepare SQL statement.<br />';
}

$Database->close();
header("Location: posts.php");
} // end isset($_POST['submit'])




} // end update_post()



The field names in the query do also match up together.

These values were called in using ajax, if that matters.

I appreciate the help

surreal5335
08-05-2011, 07:01 AM
bump up

surreal5335
08-07-2011, 10:37 AM
bump up

Old Pedant
08-08-2011, 12:12 AM
DEBUG DEBUG DEBUG

Change


if ($Database->query("UPDATE posts SET post_title='".$post_title."', post_image='".$post_image."', post_content='".$post_desc."' WHERE post_id='".$post_id."'" ))
{

to


$sql = "UPDATE posts SET post_title='".$post_title."', post_image='".$post_image."', post_content='".$post_desc."' WHERE post_id='".$post_id."'";
echo "<hr>DEBUG SQL: " . $sql . "<hr>\n";
if ($Database->query( $sql ) )
{

Take a look at the actual query you are using. Is it right? If it looks right, then test it by running it from some MySQL tool, *NOT* from PHP. Any errors? Did it work?

And so on.

One step at a time.

If you can't debug, you can't program. Sad but true fact.

*************

Just noticed: You said this is done via AJAX. Okay, so instead of just echoing the SQL, you could write it out to some text file and then look at the text file.

Or you could just read the echo from the JavaScript code (it *will* come back in the responseText).

Or you could change from $_POST[...] to $_GET[...] and test it by just passing the values in via a query string.

Statement still stands: If you can't debug, you can't program. A truism.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum