PHP,按名称获取按钮,其中包含脚本
问题描述:
我在执行php会话购物车时遇到问题。我从数据库中获得产品列表,并且每个都有自己的“添加到购物车”按钮。问题是如何通过点击按钮添加到会话中来获取它们中的任何一个?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);}?>
答
你不得不重新考虑该脚本:对每个页面加载3行,你完全清空$ _SESSION [车]
- !
- 您使用的是过时的功能:MySQL的
- 您使用不必要的“形式”标签
- 您检查$ _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;
}
?>
+0
非常感谢你!) –
+0
干杯!我希望它提供了你正在寻找的答案。 – verjas
''whaat。 _deprecated_ mysql函数和在PHP中不正确地使用HTML。 '$ query = mysql_query($ sql);?> Go To Cart'这些工作是否有效? – user5173426
'$ _GET ['btn-submit。$ elId']'这是什么?这是jQuery选择器?大声笑 – aldrin27
围绕最后一个陈述有什么样的循环?换句话说,你明确地循环?这是没有显示在你的代码... – Floris