只是搜索了一下,找不到有合适答案的问题。我尝试在数组的末尾添加一个product_id,每次添加一些东西时,它也会被添加到数组中。目前,我每次添加product_id时,它都会替换以前存在的任何内容。我在Array.Below得到的只是一个product_id,这是我的代码的一部分,它将‘s添加到数组中。然后我使用
print_r($order);以显示数组中的内容。
$order = array();
switch ($action)
{
// Add
case "add":
$order[] = $product_id;
break;
}以下是页面的完整代码,去掉了一些MySQL内容。
include 'connection.php';
if (isset($_GET['action']))
{
$action = $_GET['action']; //Get's action from the URL
}
if (isset($_GET['product_id']))
{
$product_id = $_GET['product_id']; //the action from the URL
}
// if there is an product_id and that product_id doesn't exist display an error message
if ($product_id && !productExists($product_id))
{
die("Error. Product Doesn't Exist");
}
switch ($action)
{ //decide what to do
// Add
case "add":
$order[] = $product_id;
if (isset($_SESSION['user']))
{
$productadd = sprintf("INSERT INTO tbl_basket (customer_id, product_id, basket_status) VALUES ('" . $_SESSION['user'] . "','$product_id','basket')");
mysql_query($productadd);
break;
}
else
{
$productadd = sprintf("INSERT INTO tbl_basket (customer_id, product_id, basket_status, user_type) VALUES ('" . $_SESSION['guestuser'] . "','$product_id','basket', 'guest')");
mysql_query($productadd);
break;
}
// Remove
case "remove":
if (isset($_SESSION['user']))
{
$productremove = sprintf("DELETE FROM tbl_basket WHERE customer_id = '" . $_SESSION['user'] . "' AND product_id = '$product_id'");
mysql_query($productremove);
break;
}
else
{
$productremove = sprintf("DELETE FROM tbl_basket WHERE customer_id = '" . $_SESSION['guestuser'] . "' AND product_id = '$product_id'");
mysql_query($productremove);
break;
}
// Empty
case "empty":
if (isset($_SESSION['user']))
{
$empty = sprintf("DELETE FROM tbl_basket WHERE customer_id = '" . $_SESSION['user'] . "' AND order_status = 'basket'");
mysql_query($empty);
break;
}
else
{
$empty = sprintf("DELETE FROM tbl_basket WHERE customer_id = '" . $_SESSION['user'] . "' AND order_status = 'basket'");
mysql_query($empty);
break;
}
}
if (isset($_SESSION['user']))
{
$result = mysql_query("SELECT * FROM tbl_basket a INNER JOIN tbl_products b ON a.product_id = b.product_id WHERE a.customer_id = '" . $_SESSION['user'] . "'");
}
else
{
$result = mysql_query("SELECT * FROM tbl_basket a INNER JOIN tbl_products b ON a.product_id = b.product_id WHERE a.customer_id = '" . $_SESSION['guestuser'] . "'");
}发布于 2014-04-11 08:16:59
执行$order = array();操作时,将清除$order中以前的值
只需删除该行,$order[] = $product_id;将向数组中添加另一项。
https://stackoverflow.com/questions/23001329
复制相似问题