Hello quick question regarding storage of variables in a mySQL database in php.
if(count($_SESSION['cart'])>0) {
$ids = array();
foreach ($_SESSION['cart'] as $id => $value) {
array_push($ids, $id);
}
$stmt = $food->readByIds($ids);
$total = 0;
$item_count = 0;
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
extract($row);
$quantity = $_SESSION['cart'][$id]['quantity'];
$sub_total = $price * $quantity;
//echo "<div class='product-id' style='display:none;'>{$id}</div>";
//echo "<div class='product-name'>{$name}</div>";
// =================
echo "<div class='cart-row'>";
echo "<div class='col-md-8'>";
echo "<div class='food-name m-b-10px'><h4>{$name}</h4></div>";
echo $quantity > 1 ? "<div>{$quantity} items</div>" : "<div>{$quantity} item</div>";
echo "</div>";
echo "<div class='col-md-4'>";
echo "<h4>$" . number_format($price, 2, '.', ',') . "</h4>";
echo "</div>";
echo "</div>";
// =================
$item_count += $quantity;
$total += $sub_total;
}
$sql = "INSERT INTO food_orders (food_list, food_total, created_on) VALUES (:$name, :$total, :current_timestamp)";
$stmt = $db->prepare($sql);
$stmt->bindParam(':$name', $_POST['food_list'], PDO::PARAM_STR);
$stmt->bindParam(':$total', $_POST['food_total'], PDO::PARAM_STR);
$stmt->bindParam(':current_timestamp',$_POST['created_on'], PDO::PARAM_STR);
$stmt->execute();
Currently, I am attempting to store the values calculated into the database under the $_POST tag, but I am constantly thrown the error:
Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number:
parameter was not defined in C:\wamp64\www\phpFoodSitewithCart\charge.php
Is there a better way to store the variables or sync them properly?
Thanks for the look and the help.