请不要谴责我提出多次被问过的问题。我花了几个小时翻阅答案,但找不到我的解决方案。我刚刚学习使用PDO。我已成功使用它从数据库中选择并显示结果,但我遇到了更新功能的问题。
我收到错误:SQLSTATE [HY093]:参数号无效:参数未定义。
我已经检查并重新检查了我的代码,但没有输入错误,所以也许它与edit_date字段有关?如果有人可以帮助我,我将非常感激,因为我花了几个小时研究和试验没有解决方案。关心Tog。
这是代码:
$article_id = (trim ( $_POST [ 'id' ]) == ' ') ? die ( 'ERROR:no ID' ) : mysql_escape_string ( $_POST [ 'id' ] );
$article_id = (INT) $article_id;
$title = (trim ( $_POST [ 'title' ]) == ' ') ? die ( 'ERROR: Enter a Title' ) : mysql_escape_string ( $_POST [ 'title' ] );
$author = (trim ( $_POST [ 'author' ]) == ' ') ? die ( 'ERROR: Enter an Author name' ) : mysql_escape_string ( $_POST [ 'author' ] );
$image_url = (trim ( $_POST [ 'image_url' ]) == ' ') ? die ( 'ERROR: Enter an Image URL' ) : mysql_escape_string ( $_POST [ 'image_url' ] );
$main_article = (trim ( $_POST [ 'main_article' ]) == ' ') ? die ( 'ERROR: Enter some article content' ) : mysql_escape_string ( $_POST [ 'main_article' ] );
$snippet = (trim ( $_POST [ 'snippet' ]) == ' ') ? die ( 'ERROR: Enter snippet text' ) : mysql_escape_string ( $_POST [ 'snippet' ] );
$friendly_url = (trim ( $_POST [ 'friendly_url' ]) == ' ') ? die ( 'ERROR: Enter a friendly url' ) : mysql_escape_string ( $_POST [ 'friendly_url' ] );
$meta_title = (trim ( $_POST [ 'meta_title' ]) == ' ') ? die ( 'ERROR: Enter a Meta Title' ) : mysql_escape_string ( $_POST [ 'meta_title' ] );
$meta_description = (trim ( $_POST [ 'meta_description' ]) == ' ') ? die ( 'ERROR: Enter a Meta Description' ) : mysql_escape_string ( $_POST [ 'meta_description' ] );
$edited_by = (trim ( $_POST [ 'edited_by' ]) == ' ') ? die ( 'ERROR: who edited this?' ) : mysql_escape_string ( $_POST [ 'edited_by' ] );
$edit_date = CURRENT_TIMESTAMP;
try {
$dbh = new PDO("mysql:host=$hostname;dbname=gosport", $username, $password);
/*** echo a message saying we have connected ***/
echo 'Connected to database<br />';
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
/*** Update data ***/
$sql = "UPDATE sport SET title = :title,
author = :author,
image_url = :image_url,
main_article = :main_article,
snippet = :snippet,
edit_date = :edit_date,
friendly_url = :friendly_url,
meta_title = :meta_title,
meta_description = :meta_description,
edited_by = : edited_by
WHERE article_id = :article_id";
$stmt = $dbh->prepare($sql);
$stmt->bindParam(':title', $title, PDO::PARAM_STR);
$stmt->bindParam(':author', $author, PDO::PARAM_STR);
$stmt->bindParam(':image_url', $image_url, PDO::PARAM_STR);
$stmt->bindParam(':main_article', $main_article, PDO::PARAM_STR);
$stmt->bindParam(':snippet', $snippet, PDO::PARAM_STR);
$stmt->bindParam(':edit_date', $edit_date, PDO::PARAM_STR);
$stmt->bindParam(':friendly_url', $friendly_url, PDO::PARAM_STR);
$stmt->bindParam(':meta_title', $meta_title, PDO::PARAM_STR);
$stmt->bindParam(':meta_description', $meta_description, PDO::PARAM_STR);
$stmt->bindParam(':edited_by', $edited_by, PDO::PARAM_STR);
$stmt->execute();
/*** close the database connection ***/
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
感谢您的回复。我已相应地做了一些更改,不再收到错误消息,但是当我运行代码时,它不会更新数据库。一切都和我运行代码之前一样。
我的新代码:
$article_id = (trim ( $_POST [ 'id' ]) == ' ');
$article_id = (INT) $article_id;
$title = (trim ( $_POST [ 'title' ]) == ' ');
$author = (trim ( $_POST [ 'author' ]) == ' ');
$image_url = (trim ( $_POST [ 'image_url' ]) == ' ');
$main_article = (trim ( $_POST [ 'main_article' ]) == ' ');
$snippet = (trim ( $_POST [ 'snippet' ]) == ' ');
$friendly_url = (trim ( $_POST [ 'friendly_url' ]) == ' ');
$meta_title = (trim ( $_POST [ 'meta_title' ]) == ' ');
$meta_description = (trim ( $_POST [ 'meta_description' ]) == ' ');
$edited_by = (trim ( $_POST [ 'edited_by' ]) == ' ');
$edit_date = CURRENT_TIMESTAMP;
try {
$dbh = new PDO("mysql:host=$hostname;dbname=gosport", $username, $password);
/*** echo a message saying we have connected ***/
echo 'Connected to database<br />';
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
/*** Update data ***/
$sql = "UPDATE sport SET title = :title,
author = :author,
image_url = :image_url,
main_article = :main_article,
snippet = :snippet,
edit_date = :edit_date,
friendly_url = :friendly_url,
meta_title = :meta_title,
meta_description = :meta_description,
edited_by = :edited_by
WHERE article_id = :article_id";
$stmt = $dbh->prepare($sql);
$stmt->bindParam(':article_id', $article_id, PDO::PARAM_STR);
$stmt->bindParam(':title', $title, PDO::PARAM_STR);
$stmt->bindParam(':author', $author, PDO::PARAM_STR);
$stmt->bindParam(':image_url', $image_url, PDO::PARAM_STR);
$stmt->bindParam(':main_article', $main_article, PDO::PARAM_STR);
$stmt->bindParam(':snippet', $snippet, PDO::PARAM_STR);
$stmt->bindParam(':edit_date', $edit_date, PDO::PARAM_STR);
$stmt->bindParam(':friendly_url', $friendly_url, PDO::PARAM_STR);
$stmt->bindParam(':meta_title', $meta_title, PDO::PARAM_STR);
$stmt->bindParam('meta_description', $meta_description, PDO::PARAM_STR);
$stmt->bindParam(':edited_by', $edited_by, PDO::PARAM_STR);
$stmt->execute();
/*** close the database connection ***/
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
答案 0 :(得分:1)
您在WHERE子句中缺少:article_id
的绑定。因此,您准备好的语句与执行时传递的参数之间的参数数量不匹配。
同样如上面的评论中所述,不要使用“escape_string”类型的函数。参数化的预处理语句是不必要的,实际上,如果遇到转义序列的情况,会在记录数据中引入意外的字符转义。
另外,这里:
edited_by = : edited_by
您可能希望在冒号后丢失空格。老实说,我不知道这是否会导致错误,但至少是糟糕的形式。