PHP / MySQL:如何使用WHILE循环插入数据库

时间:2017-12-17 05:58:21

标签: php mysql phpmyadmin

我想从输入表单中插入数据,同时从另一个表(shopping_cart)中插入一个表(顺序)。

我发现从输入表单传递值或从购物车中选择它没有问题,因为我打印了我想要用于在表(顺序)中插入的所有值并显示它。

但是,当执行要插入表顺序的查询时,输出失败并且没有任何内容插入数据库。

以下是我的代码:

<?php
include('dbconnection.php');

$query1 = mysql_query("SELECT * FROM shopping_cart");

$bil = 1;   

while ($data = mysql_fetch_array($query1)) {

    //FROM TABLE SHOPPING_CART
    $item_name=$data['ITEM_NAME'];
    $cart_price=$data['CART_PRICE'];
    $cart_quantity=$data['CART_QUANTITY'];

    //FROM HTML FORM
    $total_price = $_POST['ORDER_TOTALPRICE'];
    $fullname = mysql_real_escape_string($_POST['FULLNAME']);
    $address = mysql_real_escape_string($_POST['ADDRESS']);
    $phone = mysql_real_escape_string($_POST['PHONE']);

    echo $item_name;
    echo $cart_price;
    echo $cart_quantity;
    echo $fullname;
    echo $address;
    echo $phone;
    echo $total_price;

    //SOMETHING WRONG HERE ?
    $query = mysql_query("INSERT INTO order (ORDER_TOTALPRICE, ITEM_NAME, 
    CART_PRICE, CART_QUANTITY, FULLNAME, ADDRESS, PHONE) VALUES ('$total_price','$item_name','$cart_price','$cart_quantity','$fullname','$address','$phone')");

    if ($query){
        echo "success";
    }
    else{
        echo "fail";


    }
    $bil++;

}

?>

1 个答案:

答案 0 :(得分:-1)

试试这个

$query = mysql_query("INSERT INTO order (ORDER_TOTALPRICE, ITEM_NAME, 
    CART_PRICE, CART_QUANTITY, FULLNAME, ADDRESS, PHONE) VALUES (".$total_price.",".$item_name.",".$cart_price.",".$cart_quantity.",".$fullname.",".$address.",".$phone.")");