0

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>&#36;" . 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.

0

1 Answer 1

1

Placeholders should look like :name not :$name. By using $name anywhere inside a double-quoted string you're asking for string interpolation to kick in and unless $name is defined as something that basically removes it from the string, leaving just : which is not valid. The placeholder goes invisible.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.