Can someone explain me why this is not working, Im trying to insert multiple values into a database, first I was inserting carpirces only and was working, but now Im trying to insert also de Ids but now the code don't work
if(!empty($_POST))
{
$query = "INSERT INTO prices (carid, vendorid, carprice) values (:carid, 2, :carprice)";
$query_params = array(':carprice' => $_POST['carprice']);
$price = null;
$carids = null;
try
{
$stmt = $db->prepare($query);
$stmt->bindParam(':carprice', $price);
foreach($_POST['carprice'] as $value) {
$price = $value;
$stmt->execute();
}
$stmt->bindParam(':carid', $carids);
foreach($_POST['carid'] as $value) {
$carids = $value;
$stmt->execute();
}
}
catch(PDOException $ex)
{
die("Error 1 " . $ex->getMessage());
}
header("Location: update.php");
die("Rendirecting to update.php");
}
?>
<form action="prices.php" method="post">
<table border=1>
<tr>
<th>Id</th>
<th>car</th>
<th>model</th>
<th>Price</th>
</tr>
<?php foreach($rowscars as $row): ?>
<tr>
<th><input type="hidden" name="carid[]" value="<?php echo ' ' . htmlentities($row['carid'], ENT_QUOTES, 'UTF-8') . ' ';?>" /><?php echo '' . htmlentities($row['carid'], ENT_QUOTES, 'UTF-8') . '';?></th>
<th><?php echo '' . htmlentities($row['car'], ENT_QUOTES, 'UTF-8') . '';?></th>
<th><?php echo '' . htmlentities($row['model'], ENT_QUOTES, 'UTF-8') . '';?></th>
<th><input type="text" name="carprice[]" value=""></th>
</tr>
<?php endforeach; ?>
</table>
<input type="submit" value="Submit">
</form>