我正在尝试在 MySQL 中存储信息,但遇到了问题。尽管我试图同时存储名称和总数,但只有“名称”存储在 MySQL 中。我尝试使用电子邮件而不是总计,但它仍然没有存储,这告诉我这不是一个特定于变量的问题?
目前我有这个代码:
<?php
require 'cart.php';
define('DB_NAME', 'orders');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if(!$db_selected) {
die('Unable to use ' . DB_NAME . ': ' . mysql_error());
}
...
$name = $_POST['name'];
$total = $_POST['total'];
$sql = "INSERT INTO orders (name, total) VALUES ('$name', '$total')";
if (!mysql_query($sql)) {
die ('Error: ' . mysql_error());
}
mysql_close();
?>
这是我的整个 cart.php:
<?php
session_start();
$page = 'index.php';
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('cart') or die(mysql_error());
if (isset($_GET['add'])) {
$quantity = mysql_query('SELECT id, quantity FROM products WHERE id='.mysql_real_escape_string((int)$_GET['add']));
while ($quantity_row = mysql_fetch_assoc($quantity)) {
if ($quantity_row['quantity']!=$_SESSION['cart_'.(int)$_GET['add']]) {
$_SESSION['cart_' . (int)$_GET['add']] +='1';
header('Location: order.php');
}
}
header('Location: order.php');
}
if (isset($_GET['remove'])) {
$_SESSION['cart_'.(int)$_GET['remove']]--;
header ('Location: order.php');
}
if (isset($_GET['delete'])) {
$_SESSION['cart_' . (int)$_GET['delete']]='0';
header ('Location: order.php');
}
function products() {
$get = mysql_query('SELECT id, name, description, price FROM products WHERE quantity > 0 ORDER BY id ASC');
if (mysql_num_rows($get) == 0) {
echo "There are no products to display.";
}
else {
echo "<center>\n";
echo " <table class='menufinal' border=0 width=75%>\n";
echo " <tr>\n";
echo " <th>View</th>\n";
echo " <th>Dish</th>\n";
echo " <th>Description</th>\n";
echo " <th>Item Price</th>\n";
echo " </tr>\n";
while ($get_row = mysql_fetch_assoc($get)) {
?>
<tr>
<td><img src="template.png" height="110" width="110"/> </td>
<td> <?echo '<p><strong>'.$get_row['name'] . '</strong>'?> </td>
<td> <?echo $get_row['description']?> </td>
<td><strong> <?echo '<br>£'.number_format($get_row['price'],2).'<br><br> <a href="cart.php?add='.$get_row['id'].'"><button>Add</button></a></p>';?> </strong></td>
</tr>
<?
}
echo "</table>\n";
echo "</center>\n";
}
}
function cart() {
$output = '';
$output .= '<center>';
$output .= '<table class="menufinal" border=0 width=75%>';
$output .= '<tr>';
$output .= '<th>Remove Item</th>';
$output .= '<th>Item Name</th>';
$output .= '<th>Item Price</th>';
$output .= '<th>Quantity</th>';
$output .= '<th>Line Total</th>';
$output .= '</tr>';
foreach($_SESSION as $name => $value) {
if ($value>0){
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, (strlen($name)-5));
$get = mysql_query('SELECT id, name, price FROM products WHERE id='.mysql_real_escape_string((int)$id));
while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['price']*$value;
$output .= '<tr>';
$output .= '<td><a href="cart.php?delete=' .$id.'"><img src="x.png"></a><br></td>';
$output .= '<td>' . $get_row['name'] . '</td>';
$output .= '<td>£ ' . number_format($get_row['price'], 2) . '</td>';
$output .= '<td><a href="cart.php?remove=' .$id. '"style="text-decoration:none"><strong>- </strong></a>' .$value. '<a href="cart.php?add=' .$id. '"style="text-decoration:none"> +</a></td>';
$output .= '<td>£ ' . number_format($sub, 2) . '</td>';
$output .= '</tr>';
}
}
if (empty($total)) {
if (empty($sub)) {
//do nothing
} else {
$total = $sub;
}
} else {
$total += $sub;
}
}
}
$output .= '</table>';
if (empty($total)){
header ('Location: index.php');
exit;
}
$output .= '<br><br><br><br><div id="finalPrice">Total: £ ' . number_format($total, 2) . '<br></div>';
$output .= '<br><br><br><p><a href="index.php"><img src="dishes.png" width="240" height="152"></a> <img src="spacer.png" width="110"> <a href="checkout.php"><img src="checkout.png" width="240" height="151"></a>';
echo $output;
}
?>
如果我需要显示更多代码,请告诉我。
最佳答案
这里有几个相关的问题。您的 cart
功能实际上是:
function cart() {
//print some stuff
foreach($_SESSION as $name => $value) {
if (empty($total)) {
if (empty($sub)) {
//do nothing
} else {
$total = $sub;
}
} else {
$total += $sub;
}
}
}
您在函数内设置了一个名为 $total
的变量,但 PHP 将其视为所谓的局部变量 - 该变量存在于函数本身内部,但不在函数外部。同样,在函数外部设置的变量不能在函数内部使用,除非您显式地使它们可用 - 这称为变量范围。
有几种方法可以访问函数内部设置的变量。由于您已经使用 $_SESSION
,我建议使用此:
function cart() {
$total = 0;
//print some stuff
foreach($_SESSION as $name => $value) {
if (! empty($sub)) {
$total += $sub;
}
}
$_SESSION['total'] = $total;
}
我在函数开始时初始化$total
,因此您无需在尝试添加之前检查它是否存在。我还在 session 中设置了一个变量,您可以在函数外部使用该变量。
其次 - 您需要先调用您的函数,然后才能使用它。上面的代码都没有调用您的函数 - 您所需要做的就是在其上添加一行 cart();
。
然后,当您设置数据库变量时,可以使用:
$name = $_POST['name'];
$total = $_SESSION['total'];
其他一些事情 - mysql_
函数已弃用,并将从 PHP 的 future 版本中删除。您现在不应该真正将它们用于新代码 - mysqli_
函数相当容易切换到,并允许您使用准备好的语句,这有助于您使代码更安全;还有 PDO
库,它不能直接将代码切换到该库,但它并不绑定(bind)到特定的数据库。
此外 - 这是个人偏好 - 您的 cart()
函数正在打印购物车的内容,并对其进行一些计算。我尝试让函数只做一件事情,而不是把它们混在一起,即使它们确实合适。在这种情况下,您可以有一个 print_cart()
函数和一个 get_cart_total()
函数 - 我发现维护以这种方式编写的代码要容易得多。
关于php/mysql - 向 MySQL 发送多个数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27888525/