ERRROR PDOStatement :: bindParam()期望参数3为long,给定字符串

时间:2016-05-16 12:35:57

标签: php mysqli pdo

希望我的一个快速而明显的错误,我看了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为long,给定字符串

     

致命错误:未捕获的异常&#39; PDOException&#39;消息&#39; SQLSTATE [HY093]:参数号无效:没有参数被绑定&#39;

错误的可能原因?

我正在键入的Dreamweaver会显示一个帮助功能,因为它经常(paramNo, Param, ParamType)我应该指定我在这里绑定2个参数吗? (虽然我试过但是和上面的错误相同。)

表格

enter image description here

万分感谢

1 个答案:

答案 0 :(得分:4)

替换

$statement = $db->prepare($sql);
$statement->bindParam("ss", $type, $name);

$statement = $db->prepare($sql);
$statement->bindParam(1, $type, PDO::PARAM_STR);
$statement->bindParam(2, $name, PDO::PARAM_STR);

有关详细信息,请参阅手册PDO Bind Param