2
<?php 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//       Section 5  (render the cart for the user to view on the page)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$cartOutput = "";
$cartTotal = "";
$pp_checkout_btn = '';
$checkout_btn = '';
$product_id_array = '';
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
    $cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>";
} else {
    // Start PayPal Checkout Button
    $pp_checkout_btn .= '<form action="http://chenlikpharmacy.freeserver.me/order_list.php" method="post">
    <input type="hidden" name="cartOutput" id="cartOutput" value = "<?php echo $cartOutput; ?>
    ';
    // Start the For Each loop
    $i = 0; 
    foreach ($_SESSION["cart_array"] as $each_item) { 
        $item_id = $each_item['item_id'];
$sqlCommand = "SELECT * FROM products WHERE id='$item_id' LIMIT 1";     
$sql = mysqli_query($myConnection,$sqlCommand);
        while ($row = mysqli_fetch_array($sql)) {
            $product_name = $row["product_name"];
            $price = $row["price"];
            $details = $row["details"];
        }
        $pricetotal = $price * $each_item['quantity'];
        $cartTotal = $pricetotal + $cartTotal;
        setlocale(LC_MONETARY, "ms_MY");
        $pricetotal = money_format("%10.2n", $pricetotal);
        // Dynamic Checkout Btn Assembly
        $x = $i + 1;
        $pp_checkout_btn .= '<input type="hidden" name="item_name[]" value="' . $product_name . '">
        <input type="hidden" name="amount[]" value="' . $price . '">
        <input type="hidden" name="quantity[]" value="' . $each_item['quantity'] . '">  ';
        // Create the product array variable
        $product_id_array .= "$item_id-".$each_item['quantity'].","; 
        // Dynamic table row assembly
        $cartOutput .= "<tr>";
        $cartOutput .= '<td><a href="product.php?id=' . $item_id . '">' . $product_name . '</a><br /><img src="inventory_images/' . $item_id . '.jpg" alt="' . $product_name. '" width="40" height="52" border="1" /></td>';
        $cartOutput .= '<td>' . $details . '</td>';
        $cartOutput .= '<td>RM ' . $price . '</td>';
        $cartOutput .= '<td><form action="cart.php" method="post">
        <input name="quantity" type="text" value="' . $each_item['quantity'] . '" size="1" maxlength="2" />
        <input name="adjustBtn' . $item_id . '" type="submit" value="change" />
        <input name="item_to_adjust" type="hidden" value="' . $item_id . '" />
        </form></td>';
        //$cartOutput .= '<td>' . $each_item['quantity'] . '</td>';
        $cartOutput .= '<td>' . $pricetotal . '</td>';
        $cartOutput .= '<td><form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '" type="submit" value="X" /><input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>';
        $cartOutput .= '</tr>';
        $i++; 
    } 
    setlocale(LC_MONETARY, "ms_MY");
    $cartTotal = money_format("%10.2n", $cartTotal);
    $cartTotal = "<div style='font-size:18px; margin-top:12px;' align='right'>Cart Total : ".$cartTotal." MYR</div>";
    // Finish the Paypal Checkout Btn
    $pp_checkout_btn .= '<input type="hidden" name="custom" value="' . $product_id_array . '">

    <input type="submit" type="button" name="submit">
    </form>';
}

?>  

以上是我在网上学习的创建多维数组并提交到paypal处理的源码。现在我尽量避免使用贝宝,并想插入提交到我的新数据库“订单”的数据。我为此目的创建了行 (id (primary,auto increment),product_name,price,quantity,date_added) 但我未能将数据插入其中......

我的尝试如下

<?php 
// This file is www.developphp.com curriculum material
// Written by Adam Khoury January 01, 2011
// http://www.youtube.com/view_play_list?p=442E340A42191003
session_start(); // Start session first thing in script
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
// Connect to the MySQL database  
include "storescripts/connect_to_mysqli.php"; 
?>
<?php 
// Parse the form data and add inventory item to the system
if (isset($_POST['cartOutput'])) { 
$product_name=mysqli_real_escape_string($myConnection,$_POST[' . $product_name . ']);
$price=mysqli_real_escape_string($myConnection,$_POST[' . $price . ']);
$quantity=mysqli_real_escape_string($myConnection,$_POST[$each_item['quantity']]);
$date = date('d/m/Y', time());
$sqlCommand = ""INSERT INTO orders (product_name, price, quantity, date_added) VALUES('" . $_POST[$product_name] . "','" . $_POST[$price] . "','" . $_POST[$each_item['quantity']] . "', now())";      

$sql = mysqli_query($myConnection,$sqlCommand) or die (mysqli_error());
$stmt = $mysqli->prepare($sql); 
$countArray = count($_SESSION["cart_array");
for ($i = 0; $i < $countArray; $i++) {
$stmt->bind_param('ssd', $array[$product_name][$i], $array[$price][$i], $array[$each_item['quantity']][$i],$array[$date][$i]);
   $stmt->execute();
} 



echo $sql   ; 

exit();
}
?>

帮助~~~我需要知道我应该输入什么 if (isset($_POST['cartOutput'])) { 为了检索日期......我真的是一个初学者。我需要一些魔法......

4

4 回答 4

0

我的示例使用 PDO,但我认为您明白了,您还应该使用 PDO 或 MYSQLI 而不是 mysql

这个例子:

<?php
// pdo example

$sql = 'INSERT INTO table (field1, field2, field3) VALUES (:value1, :value2, :value3)';

// $dbh is pdo connection
$insertTable = $dbh->prepare($sql);

$countArray = count($array);

for ($i = 0; $i < $countArray; $i++) {
   $insertTable->bindParam(':value1', $array['value1'][$i], PDO::PARAM_INT); // if value is int
   $insertTable->bindParam(':value2', $array['value2'][$i], PDO::PARAM_STR); // if value is str
   $insertTable->bindParam(':value3', $array['value3'][$i], PDO::PARAM_STR);
   $insertTable->execute();
}

?>

输入名称格式

我看到您正在这样做:amount_' . $x . '您的数组帖子将如下所示:

[amount_0] => 100
[amount_1] => 200
[amount_2] => 1
[quantity] => 10
[quantity] => 20
[quantity] => 1

但如果你写amount[]数组将如下所示:

[amount] => Array
    (
        [0] => 100
        [1] => 200
        [2] => 1
    )

[quantity] => Array
    (
        [0] => 10
        [1] => 20
        [2] => 1

最后一个选项可以更好地读取数组。

MYSQLI 示例

<?php
$sql = 'INSERT INTO table (field1, field2, field3) VALUES (?, ?, ?)';
$stmt = $mysqli->prepare($sql); 

$countArray = count($array);

for ($i = 0; $i < $countArray; $i++) {
$stmt->bind_param('ssd', $array['value1'][$i], $array['value2'][$i], $array['value3'][$i]);
   $stmt->execute();
} 
?>

如您所见ssd,参数前面有这些类型,有 4 种类型:

  • i = 整数
  • s = 字符串
  • d = 双倍
  • b = 斑点

您必须始终定义这一点。

编辑

你应该使用这个:

<?php 
// Parse the form data and add inventory item to the system
if (isset($_POST['cartOutput'])) { 

$sql= 'INSERT INTO orders (product_name, price, quantity, date_added) VALUES(?,?,?, NOW())';      

$stmt = $myConnection->prepare($sql); 
$countArray = count($_POST["item_name");
for ($i = 0; $i < $countArray; $i++) {
$stmt->bind_param('sss', $_POST['item_name'][$i], $_POST['amount'][$i], $_POST['quantity'][$i]);
$stmt->execute();
}

echo $sql   ; 

exit();
}
?>
于 2013-07-29T09:45:58.747 回答
0

1) pp_checkout_btn 的表单操作仍显示“paypal web”。我认为这只是一个错字,所以请先更改它。

2)此外,您的插入语句可以使用一些调整。请提供以下两个语句的结果,以构建准确的插入语句:

print_r($_POST) // once the data has been posted
echo $sql // place this just before calling exit

-- 寻求者01

于 2013-07-29T10:04:13.377 回答
0

我认为您的插入代码应如下所示
$sql = mysql_query("INSERT INTO orders (product_name, price, quantity, date_added) VALUES('" . $_POST[$product_name] . "','" . $_POST[$price] . "','" . $_POST[$each_item['quantity']] . "', now())") or die (mysql_error());

编辑:
另外,循环内的语句for应该是
$stmt->bind_param('ssd', $array[$product_name][$i], $array[$price][$i], $array[$each_item['quantity']][$i]);

于 2013-07-29T10:08:28.263 回答
0

这是您应该参考的代码:

<?php 
// This file is www.developphp.com curriculum material
// Written by Adam Khoury January 01, 2011
// http://www.youtube.com/view_play_list?p=442E340A42191003
session_start(); // Start session first thing in script
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
// Connect to the MySQL database  
include "storescripts/connect_to_mysqli.php"; 
?>
<?php 
// Parse the form data and add inventory item to the system
if (isset($_POST['cartOutput'])) {
    $product_name=$_POST[$product_name];
    $price=$_POST[$price];
    $quantity=$_POST[$each_item['quantity']];
    $sqlCommand = "INSERT INTO orders (product_name, price, quantity, date_added) VALUES('" . $_POST[$product_name] . "','" . $_POST[$price] . "','" . $_POST[$each_item['quantity']] . "', now())";       

    $sql = mysqli_query($myConnection,$sqlCommand) or die (mysqli_error());
    $stmt = $mysqli->prepare($sql); 
    $countArray = count($_SESSION["cart_array"]);
    for ($i = 0; $i < $countArray; $i++) {
        $stmt->bind_param('ssd', $array[$product_name][$i], $array[$price][$i], $array[$each_item['quantity']][$i]);
       $stmt->execute();
    } 

    echo $sql;

    exit();
}
?>

要检索日期:

$date = date('d/m/Y', time());

试试这个,然后看看它是否有效。

于 2013-07-29T12:04:17.110 回答