0

I am having difficulty updating a table via PHP. Using MySQL prompt or a client app the SQL command works fine, yet when I attempt it in PHP using PDO it fails.

I have taken out the 'desc' field and the PHP script works. I think its getting confused with ASC/DES

$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$sql = "UPDATE Menu SET name=:name, desc=:desc WHERE id = :id"

$st = $conn->prepare ( $sql );


$st->bindValue( ":id",  $this->id, PDO::PARAM_INT );
$st->bindValue( ":name", $this->name, PDO::PARAM_STR );
$st->bindValue( ":desc", $this->name,  PDO::PARAM_STR );


$b = $st->execute();

Im pretty sure its just confused from field name to SQL language construct but is there a way of telling the PDO object/prepared statement that desc is a field name ?

i.e something linke

(this does not work by the way)
    $sql = "UPDATE Menu SET name=:name, \'desc\'=:desc WHERE id=:id
0

1 Answer 1

2

You have id = 1, not id = :id

You can't bind to a parameter that doesn't exist.

Also DESC is a reserved word, you cannot use it without quoting it with backticks.

$sql = "UPDATE `Menu` SET `name`=:name, `desc`=:desc WHERE `id`=:id"
Sign up to request clarification or add additional context in comments.

3 Comments

oops thanks, better fix the example, btw:this is not my actual code
Everytime I see this online I thought it was a single quote!! now this makes sense. cheers thankyou so much. All working now
Im new to StackOverflow, but Im learning so much here. Thanks Leigh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.