执行查询时参数号无效

时间:2013-12-03 08:46:58

标签: php mysql

我正在尝试查询值以及是否有DUPLICATE KEY UPDATEperson。但我收到错误Invalid parameter number'。这导致查询根本不起作用。 personal_id是唯一标识符,以便正确执行DUPLICATE KEY UPDATE。如何在查询中修复此无效参数号? EXAMPLE

PHP

if(isset($_POST['submit'])) {
    //Insert or Update Values 
    $f = 1;
    while(isset($_POST['person_fname_' . $f])) {
        $person_fname = $_POST['person_fname_' . $f];
        $person_lname = $_POST['person_lname_' . $f];
        $personal_id = $_POST['person_id_' . $f];

        $query_init3 = "INSERT INTO person (person_id, academy_id, first_name, last_name) VALUES (:personal_id, :id,:person_fname,:person_lname) 
        ON DUPLICATE KEY UPDATE academy_id=:id, first_name=:person_fname, last_name=:person_lname";
        $query_prep3 = $db_con->prepare($query_init3);
        $query_prep3->execute(array(
            "id" => $id,
            "personal_id" => $person_id,
            "person_fname" => $person_fname,
            "person_lname" => $person_lname
        ));
        $f++;
    }   
}

HTML

<ul id="pq_entry_1" class="clonedSection">

    <li>
        <input id="person_fname_1" name="person_fname_1" placeholder="Person #1 - First Name" type="text" />
    </li>

    <li>
        <input id="person_lname_1" name="person_lname_1" placeholder="Last Name" type="text" />
    </li>

    <li>
        <input id="person_id_1" name="person_id_1" type="text" />
    </li>

</ul>

<input type='button' id='btnAdd' value='add another Person' />
<input type='button' id='btnDel' value='delete Delete' />

2 个答案:

答案 0 :(得分:1)

你不能两次使用相同的绑定参数,你需要特定一个新的

$query_init3 = "INSERT INTO person (person_id, academy_id, first_name, last_name) VALUES (:personal_id, :id,:person_fname,:person_lname)
                  ON DUPLICATE KEY UPDATE academy_id=:id2, first_name=:person_fname2, last_name=:person_lname2";
$query_prep3 = $db_con->prepare($query_init3);
$query_prep3->execute(array(
    "id" => $id,
    "personal_id" => $person_id,
    "person_fname" => $person_fname,
    "person_lname" => $person_lname,
    "id2" => $id,
    "person_fname2" => $person_fname,
    "person_lname2" => $person_lname
));

答案 1 :(得分:0)

您错过了每个参数前的:

        $query_prep3->execute(array(
            ":id" => $id,
            ":personal_id" => $person_id,
            ":person_fname" => $person_fname,
            ":person_lname" => $person_lname
        ));

这不是很直观,但在文档中解释:http://php.net/manual/es/pdostatement.bindvalue.php