【发布时间】:2016-05-16 12:35:57
【问题描述】:
希望我是一个快速而明显的错误,我通过previous questions 看了一眼,但找不到与我的问题直接相关的问题,所以请原谅我打开了一个应该被视为相当简单的问题,但是在使用 mysql 多年之后,PDO 切换并不容易
我想做什么
只需将输入的表单数据插入表格的新行
代码
HTML
<form name="animals" id="animals" method="post">
<label for="animalType">Animal Type: </label>
<input type="text" required="required" name="animalType" />
<br />
<br />
<label for="animalName">Animal Name:</label>
<input type="text" required="required" name="animalName" />
<br />
<button type="submit" name="submit">Add</button>
</form>
PHP
if(isset($_POST['submit'])){
//here we get the data submitted in form and assign to vars
$type = $_POST['animalType'];
$name = $_POST['animalName'];
//here we are preparing our SQL statment
$sql = "INSERT INTO animals
(animal_type, animal_name)
VALUES
(?,?)";
//prepare DB as per above
$statement = $db->prepare($sql);
$statement->bindParam("ss", $type, $name);
$success = $statement->execute();
if($success){
echo'Welldone Chuck, Successfully Updated';
}
else{
$error = $db->error;
echo'Whoopsy Doopsy someone made a boopsy, ERROR INFO: $error';
}
$statement->close();
}
错误:
PDOStatement::bindParam() 期望参数 3 很长,给定字符串
致命错误:未捕获的异常“PDOException”,带有消息“SQLSTATE[HY093]:无效参数号:未绑定参数”
可能的错误原因?
Dreamweaver 在我输入时会调出一个帮助功能,就像(paramNo, Param, ParamType) 经常做的那样,我是否应该在此处指定我正在绑定 2 个参数? (我尝试过,但与上面的错误相同。)
表格
谢谢一百万
【问题讨论】:
-
PDO 的
bindParam没有ss并且是 mysqli_prepared statements 语法。你没看说明书吗?? -
@Fred-ii- 抱歉,当我删除我收到错误
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens -
我们有了它。问题解决了。使用
bind_param,你会很高兴;-) -
更好的地方:准备好的语句手册php.net/pdo.prepared-statements 和错误检查php.net/manual/en/pdo.error-handling.php
-
@NareshKumar 考虑到 OP 从一开始就使用的未知 API,我的 cmets 是相关的。当我发现他在使用什么时,我删除了我的 cmets。因此,如果我从一开始就知道并确切地知道我们正在处理哪些动物,我就不会发布那些 cmets。我觉得我从一开始就没有错。