【问题标题】:getting pdo mysql error得到 pdo mysql 错误
【发布时间】:2014-01-22 02:46:07
【问题描述】:

我收到以下警告

警告:PDOStatement::execute(): SQLSTATE[HY093]: 无效参数号:绑定变量的数量与第 29 行 C:\xampp\htdocs\form\formProcess.php 中的标记数量不匹配

我的代码如下:

<?php
require_once 'DB.php';

//Peronal Info DB
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$email = $_POST['email'];
$position = $_POST['position'];
$id = 1;
//Company Info DB
$companyName = $_POST['companyName'];
//$address = $_POST['address'];
//$city = $_POST['city'];
//$state = $_POST['state'];
//$zip = $_POST['zip'];
//$phone = $_POST['phone'];





//INSERT INTO COMPANIES TABLE
$stmt = $DB->prepare("INSERT INTO companies (CompanyName) value (:Company_id");
$stmt->execute(array(':Company_id' => $companyName));


//INSERT INTO PERSONAL INFO TABLE
$stmt = $DB->prepare("INSERT INTO personalInfo (id, Company_id, firstName, lastName, email, position) value (:id, :Company_id, :firstName, :lastName, email, position)");
$stmt->execute(array(':id' => $id, ':Company_id' => $companyName, ':firstName' => $firstName, ':lastName' => $lastName, ':email' => $email, ':position' => $position));

echo "Form proccessed successfully $firstName $lastName $email $companyName $position!";

?>

html文件

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <link rel="stylesheet" type="text/css" href="style.css">
    </head>
<body>
    <div class="container">
        <form action="formProcess.php" method="post">
            <label for="firstName" class="formLabel">First Name:</label>
                <input type="text" name="firstName" id="firstName" />
            <label for="lastName" class="formLabel">Last Name:</label>
                <input type="text" name="lastName" id="lastName" />
            <label for="companyName" class="formLabel">Company Name:</label>
                <input type="text" name="companyName" id="companyName" />
            <label for="position" class="formLabel">Position:</label>
                <input type="text" name="position" id="position" />
            <label for="email" class="formLabel">Email:</label>
                <input type="text" name="email" id="email" />
            <input type="submit" value="Submit Form" />
        </form> 
    </div>
</body>

</html>

为什么会出现此错误?我看过了,无法确定错误。

【问题讨论】:

    标签: php mysql pdo


    【解决方案1】:

    您在第二个 INSERT 语句中遗漏了冒号。

    name, :lastName, email, position)");
                 ---^    --^
    

    【讨论】:

      【解决方案2】:

      看起来可能是错字,这一行:

      $stmt = $DB->prepare("INSERT INTO personalInfo (id, Company_id, firstName, lastName, email, position) value (:id, :Company_id, :firstName, :lastName, email, position)");
      

      应该是

      $stmt = $DB->prepare("INSERT INTO personalInfo (id, Company_id, firstName, lastName, email, position) value (:id, :Company_id, :firstName, :lastName, :email, :position)");
      

      【讨论】:

        【解决方案3】:

        你忘记了一些:

        $stmt = $DB->prepare("[...snip...] :lastName, email, position)");
                                                      ^--    ^---
        

        因此,当您尝试绑定到不存在的 :email:position 时,您会收到错误消息。

        【讨论】:

          【解决方案4】:

          在您的 sql 语句中将“value”单数更改为“values”复数。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-05-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-01-03
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多