Update only the field that has been sent using PHP and MySQL

0

I'm doing a blog, and when I edit that blog for example the title, the other variables are declared as undefinex , what can be done in this case?

$body  = $_POST['body-post'];
    $title  = $_POST['title-post'];
    $img = $_POST['avatar-post'];
    $idautor = $_POST['idautor'];
    $catid = $_POST['categoria-post'];
    if ($title == "" || $catid == "" || $body == "" || $idautor == "" || $img == "" ) {
        echo "Campos vacios";
    }else{
        $query = "INSERT INTO tbl_post(cat, title, body, image, id_autor, tags) 
            VALUES ('$catid', '$title' ,'$body','$img','$idautor', '')";

            $inserted_rows = mysql_query($query);
            if ($inserted_rows) {
                echo "Bien datos Insertados";
                header("Location: ../../index.php");
            }
    }
    
asked by Josbert Hernandez 16.12.2016 в 00:05
source

3 answers

1

You can also verify that you are receiving $ _POST, add this in your php script

print_r ($ _ POST); die;

If the variables you need do not come, make sure there is no error at the origin of the request

    
answered by 16.12.2016 в 02:17
0

If you are passing the data through ajax, verify that the form data is being captured correctly and that it is being passed to the php script. If you are doing this directly from the submit, verify that the name of the form field is correct with the global variable $ _POST.

Note: The code you place is to perform the Insert no Update.

    
answered by 16.12.2016 в 01:15
0

If you are using AJAX I recommend the following

$body  = $_REQUEST['body-post'];
    $title  = $_REQUEST['title-post'];
    $img = $_REQUEST['avatar-post'];
    $idautor = $_REQUEST['idautor'];
    $catid = $_REQUEST['categoria-post'];

$ _ REQUEST instead of $ _POST plus you have an echo of the variables to see what they bring. Could you post your AJAX?

    
answered by 04.01.2017 в 16:32