Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts

    troubles saving data to database

    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.

    PHP Code:
    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

  • #2
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts
    bump up

  • #3
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts

    bump up

    bump up

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,194
    Thanks
    80
    Thanked 4,563 Times in 4,527 Posts
    DEBUG DEBUG DEBUG

    Change
    Code:
            if ($Database->query("UPDATE posts SET post_title='".$post_title."', post_image='".$post_image."', post_content='".$post_desc."' WHERE post_id='".$post_id."'" ))
            {
    to
    Code:
            $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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •