I am likely to get in trouble with you guys again but please help.
When I attempt to submit records to the database, I get the following error:
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
Below is the code causing the error:
//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]));
}
I know the error is happening because the INSERT is expecting each of the foreach loops to have have same number of bound variables as the parameters in the INSERT statement as shown:
VALUES ( ? , ? , ? , ? , ? , ? , ? )
How do I reconcile to the two foreach loops into so the numbers equal the same as the INSERT statement?
If you don't quite understand what I mean please, please ask for clarification.
I would rather you yell at me but help than having to go to work on Monday without resolving this issue.
Thanks a lot
//updated code:
<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>