gpt4 book ai didi

php - 到另一个页面的超链接不起作用

转载 作者:行者123 更新时间:2023-11-29 19:52:20 25 4
gpt4 key购买 nike

当我点击添加到购物车超链接时,cart.php页面没有响应(它没有回显添加变量。超链接看起来不错。但是,cart.php中有问题。任何感谢您的回复。提前致谢。

<html>
<head>
</head>
<body>
<table>
<tr>
<td><?php echo $row['ISBN']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['title']; ?></td>
<td><?php echo $row['year']; ?></td>
<td><?php echo $row['price']; ?></td>
<td><?php echo $row['publisher']; ?></td>
<td> <a href="cart.php?add=abc">Add to cart</a></td>
<td><?php echo $row['ISBN']; ?></td>
</tr>
</table>
</body>
</html>

cart.php页面:

<?php
//
session_start();

$page = 'search.php';
$lpage = 'cart.php';

$db = new mysqli('localhost', 'root', '', 'cheapbook') or die('Error connecting to MySQL server.');
mysqli_set_charset($db, 'utf8');

if (isset($_GET['add'])) {

echo $_GET['add'];
$pieces = explode(":", $_GET['add']);
$quantity = mysqli_query('SELECT ISBN, title from book WHERE ISBN=$pieces[0]');
$result = mysqli_query($db, $quantity);
while ($quantity_row = mysqli_fetch_array($result)) {
if ($quantity_row['quantity'] != $_SESSION['cart_' . $_GET['add']]) {
$_SESSION['cart_' . $_GET['add']] += 1;
}
}

if ($pieces[1] == 'SearchByBookTitle') {
header('location:' . $page . 'SearchByBookTitle=' . $pieces[2]);
}
if ($pieces[1] == 'SearchByAuthor') {
header('location:' . $page . 'SearchByAuthor=' . $pieces[2]);
echo $pieces[1];
} else {
header('location:' . $lpage);
}

}

if (isset($_GET['remove'])) {
$_SESSION['cart_' . $_GET['remove']]--;
header('location:' . $page);
}

if (isset($_GET['delete'])) {
$_SESSION['cart_' . $_GET['remove']]--;
header('location:' . $page);
}

if (isset($_GET['cart'])) {
cart();
}

function cart()
{
foreach ($_SESSION as $name => $value) {
if ($value > 0) {
if (substr($name, 0, 5) == 'curt_') {
$total = 0;
$id = substr($name, 5, (strlen($name) - 5));
$get = mysql_query("SELECT ISBN, title, price FROM book where id='.$id.'");
$result = mysqli_query($db, $get);
while ($get_row = mysqli_fetch_array($result)) {
$sub = $get_row['price'] * $value;
echo $get_row['title'] . 'X' . $value . '@Dollar' . $get_row['price'] . '=' . $sub . '<a href="cart.php?remove=.$id.">[-]</a><a href="cart.php?add=.$id.">[+]</a><a href="cart.php?delete=.$id.">[Delete]</a>';
}
$total += $sub;
}
}
if ($total == 0) {
echo "Your cart is empty";
} else {
echo "Paypal button";
}
}
}
?>

最佳答案

这样编码的查询当然不会按预期工作

$quantity = mysqli_query('SELECT ISBN, title 
from book
WHERE ISBN=$pieces[0]');

您需要双引号字符串才能使用变量扩展,它在单引号字符串中不起作用。

您还需要在文本变量参数值两边添加单引号

$quantity = mysqli_query("SELECT ISBN, title 
from book
WHERE ISBN='$pieces[0]'");

将来,我们强烈建议您在尝试执行查询后添加一些错误检查代码,并使用准备好的参数化查询来避免 SQL 注入(inject)

$sql = "SELECT ISBN, title from book WHERE ISBN=?";
$stmt = mysqli_prepare($sql);
if ( ! $stmt ) {
echo mysqli_error();
exit;
}
$stmt->bind_param('s', $pieces[0] );
$stmt->execute();

关于php - 到另一个页面的超链接不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40788664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com