问题
I´m trying to do an update with PDO and I´m getting an error that Im not detecting where the problem is.
The error is this: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in $updateCategory->execute();
Someone there have any idea why this is happening?
$urledit = $_GET['edit'];
$f['name'] = $_POST['name'];
$f['content'] = $_POST['content'];
$f['tags'] = $_POST['tags'];
$f['date'] = $_POST['date'];
$updateCategory = $pdo->prepare("UPDATE categories SET name=:name, url=:url, content=:content, tags=:tags, date=:date WHERE id=:urledit");
$updateCategory->bindValue(':name', $f['name']);
$updateCategory->bindValue(':url', $f['url']);
$updateCategory->bindValue(':content', $f['content']);
$updateCategory->bindValue(':tags', $f['tags']);
$updateCategory->bindValue(':date', $f['date']);
$updateCategory->bindValue(':id', $urledit);
$updateCategory->execute();
echo 'Update Sucessed!';
回答1:
Change :urledit to :id in your query. All the bind values must match in the query.
回答2:
Victory's answer is correct but please don't bind every parameter like that, PDO is awesome and the execute functions allows you to pass an array which ill bind them for you :) also if your only using the variable once there's no point to declaring it as a variable an example of this would be,
PHP
$parameters = array(
':name' => $_POST['name'],
':url' => $_POST['url'],
':content' => $_POST['content'],
':tags' => $_POST['tags'],
':date' => $_POST['date'],
':urledit' => $_GET['edit'])
);
$updateCategory = $pdo->prepare("UPDATE categories
SET name = :name, url = :url, content = :content, tags = :tags, date = :date
WHERE id = :urledit");
if($updateCategory->execute($parameters)) {
echo 'Update Sucessed!';
}
回答3:
This line needs to change:
$updateCategory->bindValue(':id', $urledit);
to this:
$updateCategory->bindValue(':urledit', $urledit);
Because in this line:
$updateCategory = $pdo->prepare("UPDATE categories SET name=:name, url=:url, content=:content, tags=:tags, date=:date WHERE id=:urledit");
you reference the placeholder :urledit rather than :id.
来源:https://stackoverflow.com/questions/22822828/pdostatementexecute-sqlstatehy093-invalid-parameter-number