pdo UPDATE does not work

0

I'm trying to send some values to SQL, but the PDO does not work as it should, or I'm forgetting something, I'm new to using PDO.

Functional code example.

    $conn = new PDO('mysql:dbname=config_database;host=127.0.0.1', 'root', '');
    $statement = $conn->prepare("UPDATE stream_table SET src='Funcional' WHERE start = '60'");
    $statement->execute();

Example of non-functional code (I tried several other possibilities without success) (Does not return error, simply does nothing)

    $conn = new PDO('mysql:dbname=config_database;host=127.0.0.1', 'root', '');
    //$value = $_POST['value'];              //A ideia real é pegar via POST
    $value = 'C:\sample.code';               //Simplificando o test
    $statement = $conn->prepare("UPDATE stream_table SET src='?' WHERE start = '60'");
    $statement->bindValue(":value", $value); //Testei com bindParam e bindValue
    $statement->execute();

I do not know if it's due to the apostrophes or anything else, I've followed several examples on the internet and it does not even work, it's evil .

How can I perform this command functionally?

Maybe this post here from SO-pt help.

@Edit

  

Error: I can not use placeholders for table or column names.

    
asked by anonymous 11.06.2014 / 21:24

1 answer

4

When using ? in bindValue place the order of the argument and variable:

$statement = $conn->prepare("UPDATE stream_table SET src = ? WHERE start = ? ");
$statement->bindValue(1, $value);
$statement->bindValue(2, $id);

With the markings ( :nome ) the order does not matter

$statement = $conn->prepare("UPDATE stream_table SET src = :value WHERE start = :id ");
$statement->bindValue(':id', $id);
$statement->bindValue(':value', $value);

To display the error add the optional argument in the constructor in addition to PDO :: ERRMODE_EXCEPTION else other forms treat the error.

$options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
$conn = new PDO('mysql:dbname=config_database;host=127.0.0.1', 'root', '', $options);

try {
   //....
   $statement->execute();
} catch(Exception $e) {
   echo $e->getMessage();
}
    
11.06.2014 / 21:30