1
$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'] . '">  ';

// Dynamic Checkout Btn Assembly
    $x = $i + 1;
    $pp_checkout_btn .= '<input type="hidden" name="item_name_' . $x . '" value="' . $product_name . '">
    <input type="hidden" name="amount_' . $x . '" value="' . $price . '">
    <input type="hidden" name="quantity_' . $x . '" value="' . $each_item['quantity'] . '">  ';

上記の入力データを使用して、別のページを設定してデータを取得します

<?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();
}
?>

これは上記の最初のもので取得しますが、カートの最初の item_name[] をスキップしました。上記の 2 番目のものは $_POST["item_name"] を使用してデータを取得できなかったため、2 番目のものからデータを取得するために使用できる他の単語1?

<?php 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//       Section 4 (if user wants to remove an item from cart)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] != "") {
    // Access the array and run code to remove that array index
    $key_to_remove = $_POST['index_to_remove'];
    if (count($_SESSION["cart_array"]) <= 1) {
        unset($_SESSION["cart_array"]);
    } else {
        unset($_SESSION["cart_array"]["$key_to_remove"]);
        sort($_SESSION["cart_array"]);
    }
}
?>

それは実際に私のインデックスに影響を与えました.. item_name[] または item_name[ ' の実装後。$x . ] ..行を 1 つ下に削除します。たとえば、最初の行をクリックして削除すると、2 行目が削除されます。2 行目をクリックすると、3 行目が削除されます。3 行目に何もない場合、応答が表示されません

Array ( [cartOutput] =>   Array ( [ 1 ] => 123 ) [quantity] => Array ( [ 1 ] => 1 ) [custom] => 19-1, ) array(4) { ["cartOutput"]=> string(44) "   array(1) { [" 1 "]=> string(3) "123" } ["quantity"]=> array(1) { [" 1 "]=> string(1) "1" } ["custom"]=> string(5) "19-1," } 
Notice: Undefined index: item_name in /home/u382560552/public_html/order_list.php on line 23
INSERT INTO orders (product_name, price, quantity, date_added) VALUES(?,?,?, NOW())

上記は item_name[ ' の printr(POST) と vardump(POST) です。バツ 。']

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Your Cart</title>
<link rel="stylesheet" href="style/style.css" type="text/css" media="screen" />
</head>
<body>
<div align="center" id="mainWrapper">
  <div id="pageHeader"><table width="100%" border="0" cellspacing="0" cellpadding="12">
  <tr>
    <td width="32%"><a href="http://chenlikpharmacy.freeserver.me/index.php"><img src="http://chenlikpharmacy.freeserver.me/style/logo.jpg" alt="Logo" width="252" height="36" border="0" /></a></td>
    <td width="68%" align="left">Your Cart</td>
  </tr>
  <tr>
    <td colspan="2"><a href="http://chenlikpharmacy.freeserver.me/index.php">Home</a> &nbsp; &middot; &nbsp; <a href="http://chenlikpharmacy.freeserver.me/product_list.php">Products</a> &nbsp; &middot; &nbsp; <a href="http://chenlikpharmacy.freeserver.me/help.php">Help</a> &nbsp; &middot; &nbsp; <a href="http://chenlikpharmacy.freeserver.me/contact.php">Contact</a></td>
    </tr>
  </table>
</div>  <div id="pageContent">
    <div style="margin:24px; text-align:left;">

    <br />
    <table width="100%" border="1" cellspacing="0" cellpadding="6">
      <tr>
        <td width="18%" bgcolor="#C5DFFA"><strong>Product</strong></td>
        <td width="45%" bgcolor="#C5DFFA"><strong>Product Description</strong></td>
        <td width="10%" bgcolor="#C5DFFA"><strong>Unit Price</strong></td>
        <td width="9%" bgcolor="#C5DFFA"><strong>Quantity</strong></td>
        <td width="9%" bgcolor="#C5DFFA"><strong>Total</strong></td>
        <td width="9%" bgcolor="#C5DFFA"><strong>Remove</strong></td>
      </tr>
     <tr><td><a href="product.php?id=13">BiO-LiFE BIO-VIZION 100 + 30 CAPS</a><br /><img src="inventory_images/13.jpg" alt="BiO-LiFE BIO-VIZION 100 + 30 CAPS" width="40" height="52" border="1" /></td><td>Provides the natural carotenoids lutein & zeaxanthin, plus The A.C.E. & Zinc antioxidants. Lutein and zeaxanthin are naturally present in the macula of the human retina. BiO-LiFE Bio-Vizion is a unique formula that provides a source of lutein and zeaxanthin preserved in their natural form from marigold flowers. It is also formulated with other anti free-radical nutrients such as Vitamin A,C,E plus the mineral zinc and selenium.

Features:

Dosage: Adult - One capsule daily with food or as recommended by your pharmacist.
Dosage: Children (6-12 yrs): One capsule every other day with food or as recommended by your pharmacist.
100 tablets per bottle or box</td><td>$140.00</td><td><form action="cart.php" method="post">
        <input name="quantity" type="text" value="1" size="1" maxlength="2" />
        <input name="adjustBtn13" type="submit" value="change" />
        <input name="item_to_adjust" type="hidden" value="13" />
        </form></td><td>   $140.00</td><td><form action="cart.php" method="post"><input name="deleteBtn13" type="submit" value="X" /><input name="index_to_remove" type="hidden" value="0" /></form></td></tr>     <!-- <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr> -->
    </table>
    <div style='font-size:18px; margin-top:12px;' align='right'>Cart Total :    $140.00 USD</div>    <br />
<br />
<form action="http://chenlikpharmacy.freeserver.me/order_list.php" method="post">
    <input type="hidden" name="cartOutput" id="cartOutput" value = "<?php echo $cartOutput; ?>
    <input type="hidden" name="item_name[1]" value="BiO-LiFE BIO-VIZION 100 + 30 CAPS">
    <input type="hidden" name="amount[1]" value="140.00">
    <input type="hidden" name="quantity[1]" value="1">  <input type="hidden" name="custom" value="13-1,">

    <input type="submit" id="cartOutput" value="Submit">
    </form>    <br />
    <br />
    <a href="cart.php?cmd=emptycart">Click Here to Empty Your Shopping Cart</a>
    </div>
   <br />
  </div>
  <div id="pageFooter">Copyright | <a href="http://chenlikpharmacy.freeserver.me/storeadmin/">Admin</a></div></div>
</body>
</html><script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-5263515-4']);
  _gaq.push(['_setDomainName', '.3eeweb.com']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

上記はhtmlビューソースです

<?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 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//       Section 1 (if user attempts to add something to the cart from the product page)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['pid'])) {
    $pid = $_POST['pid'];
    $wasFound = false;
    $i = 0;
    // If the cart session variable is not set or cart array is empty
    if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { 
        // RUN IF THE CART IS EMPTY OR NOT SET
        $_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "quantity" => 1));
    } else {
        // RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
        foreach ($_SESSION["cart_array"] as $each_item) { 
              $i++;
              while (list($key, $value) = each($each_item)) {
                  if ($key == "item_id" && $value == $pid) {
                      // That item is in cart already so let's adjust its quantity using array_splice()
                      array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1)));
                      $wasFound = true;
                  } // close if condition
              } // close while loop
           } // close foreach loop
           if ($wasFound == false) {
               array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1));
           }
    }
    header("location: cart.php"); 
    exit();
}
?>
<?php 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//       Section 2 (if user chooses to empty their shopping cart)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart") {
    unset($_SESSION["cart_array"]);
}
?>
<?php 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//       Section 3 (if user chooses to adjust item quantity)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") {
    // execute some code
    $item_to_adjust = $_POST['item_to_adjust'];
    $quantity = $_POST['quantity'];
    $quantity = preg_replace('#[^0-9]#i', '', $quantity); // filter everything but numbers
    if ($quantity >= 100) { $quantity = 99; }
    if ($quantity < 1) { $quantity = 1; }
    if ($quantity == "") { $quantity = 1; }
    $i = 0;
    foreach ($_SESSION["cart_array"] as $each_item) { 
              $i++;
              while (list($key, $value) = each($each_item)) {
                  if ($key == "item_id" && $value == $item_to_adjust) {
                      // That item is in cart already so let's adjust its quantity using array_splice()
                      array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $item_to_adjust, "quantity" => $quantity)));
                  } // close if condition
              } // close while loop
    } // close foreach loop
}
?>
<?php 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//       Section 4 (if user wants to remove an item from cart)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] != "") {
    // Access the array and run code to remove that array index
    $key_to_remove = $_POST['index_to_remove'];
    if (count($_SESSION["cart_array"]) <= 1) {
        unset($_SESSION["cart_array"]);
    } else {
        unset($_SESSION["cart_array"]["$key_to_remove"]);
        sort($_SESSION["cart_array"]);
    }
}
?>
<?php 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//       Section 5  (render the cart for the user to view on the page)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$cartOutput = "";
$cartTotal = "";
$pp_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, "en_US");
        $pricetotal = money_format("%10.2n", $pricetotal);
// Dynamic Checkout Btn Assembly
    $x = $i + 1;
    $pp_checkout_btn .= '<input type="hidden" name="item_name[' . $x . ']" value="' . $product_name . '">
    <input type="hidden" name="amount[' . $x . ']" value="' . $price . '">
    <input type="hidden" name="quantity[' . $x . ']" 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>$' . $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, "en_US");
    $cartTotal = money_format("%10.2n", $cartTotal);
    $cartTotal = "<div style='font-size:18px; margin-top:12px;' align='right'>Cart Total : ".$cartTotal." USD</div>";
    // Finish the Paypal Checkout Btn
    $pp_checkout_btn .= '<input type="hidden" name="custom" value="' . $product_id_array . '">

    <input type="submit" id="cartOutput" value="Submit">
    </form>';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Your Cart</title>
<link rel="stylesheet" href="style/style.css" type="text/css" media="screen" />
</head>
<body>
<div align="center" id="mainWrapper">
  <?php include_once("template_header.php");?>
  <div id="pageContent">
    <div style="margin:24px; text-align:left;">

    <br />
    <table width="100%" border="1" cellspacing="0" cellpadding="6">
      <tr>
        <td width="18%" bgcolor="#C5DFFA"><strong>Product</strong></td>
        <td width="45%" bgcolor="#C5DFFA"><strong>Product Description</strong></td>
        <td width="10%" bgcolor="#C5DFFA"><strong>Unit Price</strong></td>
        <td width="9%" bgcolor="#C5DFFA"><strong>Quantity</strong></td>
        <td width="9%" bgcolor="#C5DFFA"><strong>Total</strong></td>
        <td width="9%" bgcolor="#C5DFFA"><strong>Remove</strong></td>
      </tr>
     <?php echo $cartOutput; ?>
     <!-- <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr> -->
    </table>
    <?php echo $cartTotal; ?>
    <br />
<br />
<?php echo $pp_checkout_btn; ?>
    <br />
    <br />
    <a href="cart.php?cmd=emptycart">Click Here to Empty Your Shopping Cart</a>
    </div>
   <br />
  </div>
  <?php include_once("template_footer.php");?>
</div>
</body>
</html>

要求された実際のフォーム

4

2 に答える 2

1

これは間違っています:

$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; ?>
';

<?php文字列を補間するときは使用しません。そのはず:

$pp_checkout_btn .= '<form action="http://chenlikpharmacy.freeserver.me/order_list.php" method="post">
<input type="hidden" name="cartOutput" id="cartOutput" value = "$cartOutput">';

そしてこれを変更します:

$x = $i + 1;
$pp_checkout_btn .= '<input type="hidden" name="item_name[' . $x . ']" value="' . $product_name . '">
<input type="hidden" name="amount[' . $x . ']" value="' . $price . '">
<input type="hidden" name="quantity[' . $x . ']" value="' . $each_item['quantity'] . '">  ';

に:

$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'] . '">  ';

これにより、PHP は 0 から始まる配列インデックスを埋めることができます。あなたは1から始めていました。

id="cartOutput"最後に、隠し入力と送信ボタンの2 つの要素があります。ID は一意でなければならないため、いずれかの名前を変更する必要があります。または、ID が何にも使用されていない場合は削除します。

于 2013-07-30T11:28:52.873 に答える
1

に変更name="item_name_' . $x . '"しますname="item_name[' . $x . ']"

配列であることに注意してください

これが、php コードで値を取得する方法です。$_POST['item_name'][$i]

すべての入力フィールドに対してこの変更を行います。

于 2013-07-30T09:47:38.650 に答える