jeudi 22 novembre 2018

Error using variable inside UPDATE query, why? [duplicate]

This question already has an answer here:

I have this code to update my record and to check if there's any value on the respective input. But it's not even updating my record, and i got this error:

Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE title = ?'

Result ofvar_dump($sets); = string(38) ":title = :title, :keywords = :keywords"

if(isset($_POST["updateBTN"])){    

  $insert_data = array(
    'keywords'         => $_POST['keywords'],
    'img'              => $_POST['img'],
    'widht'            => $_POST['widht'],
    'status'           => $_POST['status'],
    'name'             => $_POST['name'],
    'height'           => $_POST['height']
  );
    $sets="";
    //percorre o array
    foreach ($insert_data as $column => $value) {

        if ($value!=""){
            //constroi a variável para usar na declaração UPDATE
            $sets .= $column." = ".$column.", ";

        }
    }
     $sets = rtrim($sets, ', ');

     $query = "UPDATE table SET $sets WHERE title = :title";
     $stmt = $conn->prepare($query);
     $stmt->execute($insert_data);

}

html:

<form  method="post">
<div>
    <input type="text" name="title"> 
    <span data-placeholder="Title"></span>          
</div>
<div>
    <input type="text" name="keywords"> 
    <span data-placeholder="keywords"></span>          
</div>
<div>
    <input type="text" name="img"> 
    <span data-placeholder="img"></span>          
</div>
.
.
.
<button type="submit" name="updateBTN">Send</button>
</form>

Aucun commentaire:

Enregistrer un commentaire