PHP,按名称获取包含脚本的按钮

时间:2015-09-27 12:28:30

标签: php session button

我在做php会话购物车时遇到问题。我从数据库中获取产品列表,每个产品都有自己的按钮"添加到购物车"。问题是如何通过按钮点击添加到会话中来获取它们?

<?php session_start();</br>
include_once 'dbconnect.php';
$_SESSION['cart']= array();
$sql="SELECT * FROM products ORDER BY name ASC";
$query=mysql_query($sql);?>
<a href="cart-display.php">Go To Cart</a>

我开始循环输出数据库中的所有产品

<?php
while($row=mysql_fetch_array($query)){
?>

<form method="get">
<tr><td><?php echo $row['name'] ?></td>
<td><?php echo $row['description'] ?></td>
<td><?php echo $row['price'] ?></td>
<td><?php echo $elId= $row['id_product']?></td>
<td><button type="submit" name="btn-submit.<?php echo $elId;?>">Add To Chart</button></td>
</tr><br>
</form>

然后我尝试通过$ _GET []获取它们中的任何一个,然后尝试进入数组会话。但由于循环,它只添加了ast产品。请帮忙

<?php }

if(isset($_GET['btn-submit.$elId'])){
array_push($_SESSION['cart'], $elId);}?>

1 个答案:

答案 0 :(得分:1)

您必须重新考虑脚本:

  • 在第3行,你在每个页面加载时完全清空了$ _SESSION [cart]!
  • 您使用的是不推荐使用的函数:mysql
  • 你使用不必要的形式&#39;标签
  • 你只检查上一个产品的$ _GET输入,但方式不好

这是一个开始:

<?php
session_start();
// initialize session var only ONE TIME
if (!isset($_SESSION["cart"])) {
    $_SESSION["cart"]= array();
}
// store a new element ID
$elId="";
// check $_GET input and validate it
if(isset($_GET["elId"])){
    // !! filter $_GET data somehow (e.g. make sure it is_numeric)
    $elId=preg_replace("/[^0-9]/","",$_GET["elId"]);
    // you could check in DB if the $elId is a valid value
}
// update SESSION if elId is not empty after validating $_GET
if (!empty($elId)) {
    // if not already in array, initialize field
    /* 
    use a multidimensional array to count how many products 
    of one type are in the cart, eg: $_SESSION[cart][343]=3; 
    aka Three products with id: 343
    */
    if (!isset($_SESSION["cart"][$elId])) {
        $_SESSION["cart"][$elId]=0;
    }
    // update cart
    $_SESSION["cart"][$elId]++;
    // ! user should be alerted, e.g:
    echo "<p>New product added to cart!</p>";
}
// DB access
//include_once 'dbconnect.php';
require "dbconnect.php";
$sql="SELECT * FROM products ORDER BY name ASC";
// fetch results
// $query=mysql_query($sql);
/* !! USE mysqli INSTEAD !! in file dbconnect.php */
$conn = dbconnect();
$results = $conn->query($sql);
// start products' HTML
echo "<a href='cart-display.php'>Go To Cart</a>";
echo "<table>";
// again, use mysqli
// while($row=mysql_fetch_array($query)){
while($row = $results->fetch_array()) {
    // * <form> tags are unnecessary, <a> tags are sufficient
    $elId=$row["id_product"];
    echo "
        <tr>
        <td>$row[name]</td>
        <td>$row[description]</td>
        <td>$row[price]</td>
        <td>$row[id_product]</td>
        <td><a href='?elId=$elId'>Add to cart</a></td>
        </tr>
    ";
}
echo "</table>";

?>

dbconnect.php:

<?php
function dbconnect() {
    static $conn;
    if (!isset($conn)) {
        $conn = mysqli_connect('localhost','username','password','dbname');
    }
    if ($conn === false) {
        return mysqli_connect_error(); 
    }
    return $conn;
}
?>