警告:PDOStatement :: execute():SQLSTATE [HY093]:参数号无效:绑定变量数与...中的令牌数不匹配

时间:2017-02-25 18:56:40

标签: php mysql pdo

我可能会再次遇到麻烦但请帮助。

当我尝试向数据库提交记录时,出现以下错误:

Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in C:\xampp\htdocs\closures\forms\final.php on line 64

Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in C:\xampp\htdocs\closures\forms\final.php on line 75

以下是导致错误的代码:

//grab the last inserted ID from previous INSERT statement (not included here)
$last_id = mysqli_insert_id($conn);


//Database connection
$pdo = new PDO("mysql:dbname=myDB;host=localhost","myusername","mypassword",array(PDO::ATTR_PERSISTENT => true));

$sql = 'INSERT INTO `myDB`.`myTable` ( `employeeID`'
     . ', `sourcename`, `sourceaddress`, `income`,`spousename`,`spouseAddress`,`spouseincome` )'
     . ' VALUES ( ? , ? , ? , ? , ? , ? , ? )';
     $stmt = $pdo->prepare($sql);


//My problems have to do with the two foreach loops below
foreach($_POST['sourcename'] as $id=>$not_used)
{
    $stmt->execute(array(
            $last_id,
            $_POST['sourcename'][$id],
            $_POST['sourceaddress'][$id],
            $_POST['income'][$id]));
}

// loop over the second set of arrays, for the spouse income sources
foreach($_POST['spousename'] as $id=>$not_used)
{
    $stmt->execute(array(
            $last_id,
            $_POST['spousename'][$id],
            $_POST['spouseAddress'][$id],
            $_POST['spouseIncome'][$id]));
}

我知道错误正在发生,因为INSERT期望每个foreach循环具有与INSERT语句中的参数相同数量的绑定变量,如下所示:

VALUES ( ? , ? , ? , ? , ? , ? , ? )

如何协调两个foreach循环,使数字与INSERT语句相同?

如果您不太明白我的意思,请要求澄清。

我宁愿你对我大喊大叫,但不要在星期一去工作而不解决这个问题。

非常感谢

//更新代码:

<form action='final.php' method = 'POST'>
<input type="hidden" name="employeename" value="<?php echo $employeename; ?>">
<input type="hidden" name="ttitle" value="<?php echo $ttitle; ?>">
<input type="hidden" name="email" value="<?php echo $email; ?>">
<?php foreach ($rowIDs as $id) { ?>
<input type="hidden" name="sourcename[rowIDs]" value="<?php echo $_POST['sourcename' . $id]; ?>">
<input type="hidden" name="sourceaddress[rowIDs]" value="<?php echo $_POST['sourceaddress' . $id]; ?>">
<input type="hidden" name="income[rowIDs]" value="<?php echo $_POST['income' . $id]; ?>">
<?php }?>
<?php foreach ($row2IDs as $id) { ?>
<input type="hidden" name="spousename[row2IDs]" value="<?php echo $_POST['spousename' . $id]; ?>">
<input type="hidden" name="spouseAddress[row2IDs]" value="<?php echo $_POST['spouseAddress' . $id]; ?>">
<input type="hidden" name="spouseIncome[row2IDs]" value="<?php echo $_POST['spouseIncome' . $id]; ?>">
<?php } ?>
<a href="javascript:history.go(-1)" data-icon="back" data-role="button" data-theme="b">Return to make changes</a>
<input type="submit" value="submit" />
</form>

0 个答案:

没有答案