update PDO is not updating me

0

I'm having trouble updating that is not working for me I am using PDO. the queries work in the prefecture.

I have code 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();
}

I'm not sure what to do.

    
asked by anonymous 23.09.2015 / 18:17

1 answer

0

Use prepared statements and treat it with error if it occurs, verifying the return of execute() , text-type fields and variants must be enclosed in single quotation marks when querying the database, using prepared statements is not necessary 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 a PHP error has occurred.

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