在php中添加多个产品到购物车

时间:2016-10-14 13:49:40

标签: php shopping-cart

我需要一些帮助,如何添加多个产品?我会添加一些代码或更改一些吗?请帮忙。谢谢你的回复。

if (isset($_POST['submit'])) {
    $connect = mysqli_connect("localhost", "root", "", "tut");
    $test= $values['item_name'];
    $test3 = $values["item_quantity"];
    $test2= $values["product_price"];
    $query = "INSERT INTO orders (Quantity,PriceDetails,OrderTotal) VALUES ('$test3','$test','$total')";
    $res = mysqli_query($connect, $query);
}

1 个答案:

答案 0 :(得分:0)

尝试使用下面的代码激发自己的灵感。 还可以尝试使用简单的插件来保护您的数据:

$test = mysql_real_escape_string($values['item_name']);

你可能会好起来的:

function db_action($data = array()) {
    switch ($data['action']) {
        case 'insert':
            $keys = array(); $values = array();

            foreach ($data['data'] as $key => $value) {
                $keys[$key] = $key;
                $values[$key] = "'".$value."'";
            }

            $keys = implode(', ', $keys);
            $values = implode(', ', $values);
            mysql_query("INSERT INTO ".$data['table']." (".$keys.") VALUES (".$values.")");
            break;
        case 'update':
            mysql_query("UPDATE ".$data['table']." SET ".$values." WHERE ".$key." = ".$id);
            break;
        case 'delete':
            mysql_query("DELETE FROM ".$data['table']." WHERE ".$key." = ".$id);
            break;
    }

    return;
}