update PDO is not updating me

Asked

Viewed 170 times

0

I’m having trouble doing an update not working me I am using PDO. the queries works in prefection.

code I have in the script that receives the form data

<script>
    function update() {
        var data = {"func": "save"};
        data = $("#editar").serialize() + "&" + $.param(data);          
        $.ajax({
            type: 'POST',
            url: "../Logica/info/info.php",
            data: data,
            success: function () {
                alert("dados modificados com sucesso");
            }
        });        
    } ;
</script>

php code

$id= filter_input(INPUT_POST,'ID');
$observacoes= filter_input(INPUT_POST,'observacoes');
if (isset($_POST["func"]) && !empty($_POST["func"])) { //Checks if action value exists
    $action = $_POST["func"];
    switch($action) { //Switch case for value of action
        case "save": editar($id,$observacoes);
        break;
    }
}

function editar($id,$observacoes){
    $db = new ligacao();
    $conn = $db->open();
    $sql = "UPDATE INFO
    SET OBS =$observacoes
    WHERE id=$id";
    $stmt = $conn->prepare($sql);
    $stmt->execute();
}

is not updating me

  • When running in php, print_r($_POST) key appears func?

  • 1

    yes appears I already found out... I was not receiving the id...because before I have a form but this id was not there because no one can know ...I will ask you to use a session variable... I already say if gave or not

  • 1

    in the form I created a hidden input to store the id that I will fetch the comic before, I have a select and put the information in a form.

  • That solved the problem?

  • 1

    yes solved the problem.. was not saving the id so it would never be sent to the sql server

1 answer

0

Use Prepared statements and treat the error if it occurs by checking the return of execute(), text fields and variants should be in simple quotes when making a query in the database, when using Prepared statements it is not necessary to worry about it.

function editar($id,$observacoes){
    $db = new ligacao();
    $conn = $db->open();
    $sql = "UPDATE INFO SET OBS = ? WHERE id = ?";
    $stmt = $conn->prepare($sql);
    if(!$stmt->execute(array($observacoes, $id))){
        echo'Erro:<pre>';
        print_r($stmt->errorInfo());
        return false;
    }else{
        return true;
    }
}

Modify javascript to see if an error has occurred in PHP.

success: function (data) {
   console.log(data);
   alert("dados modificados com sucesso");
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.