由於某種原因,當我嘗試執行此PHP代碼時,我的第一個選擇命令失敗,我找不到原因。任何幫助,將不勝感激。會話開始很好,據我所知,當我從產品表執行查詢選擇時問題出現了。它已被正確定義,並且所有值都已正確插入,否則其他站點Products將無法正確顯示。但我無法添加任何東西到我的購物車顯然是由於我的選擇命令的問題。SELECT命令失敗會有原因嗎?
<?php
session_start();
$productID = $_POST["productID"];
$cartNumber = $_SESSION["cartNumber"];
require_once('appVars.php');
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
or die('Could not connect to MySQL server as CIS55Student, cis55 database.');
$query = "SELECT * FROM products_nalanirowe WHERE productID = '$productID'";
$rs = mysqli_connect($dbc, $query) or die('SELECT command failed. ' . mysqli_error($dbc));
$num_rows = mysqli_num_rows($rs);
$row = mysqli_fetch_array($rs);
$productName = $row['productName'];
$productPrice = $row['productPrice'];
if ($cartNumber <> "") {
$query = "SELECT * FROM cart_nalanirowe WHERE cartNumber = '$cartNumber' and ProductID = '$productID'";
$rs = mysqli_connect($dbc, $query) or die('SELECT command failed. ' . mysqli_error($dbc));
$num_rows = mysqli_num_rows($rs);
if ($num_rows == 0) {
$query = "INSERT INTO cart_nalanirowe VALUES ('$cartNumber', '$productID', '$productName', CURDATE(), CURTIME())";
$rs = mysqli_connect($dbc, $query) or die('INSERT INTO command failed. ' . mysqli_error($dbc));
}
else {
}
}
else {
$query = "SELECT * FROM cart_nalanirowe ORDER BY cartNumber DESC";
$rs = mysqli_connect($dbc, $query) or die('SELECT command failed. ' . mysqli_error($dbc));
echo '<br>Selected record from table cart_nalanirowe';
$row = mysqli_fetch_array($rs);
$lastCartNumber = $row['cartNumber'];
$newCartNumber = $lastCartNumber + 1;
$query = "INSERT INTO cart_nalanirowe VALUES ('$newCartNumber', '$productID', '$productPrice', CURDATE(), CURTIME())";
$rs = mysqli_connect($dbc, $query) or die('INSERT INTO command failed. ' . mysqli_error($dbc));
$_SESSION["cartNumber"] = $newCartNumber;
}
mysqli_close($dbc);
ob_start();
header("Location: viewcart.php");
ob_flush();
?>
我敢打賭,如果您將您的代碼縮小到有問題的部分,您的問題將更加重視。很多人會看你的代碼,認爲「沒有辦法」,接下來的問題。 – Amarnasan
要執行查詢,請使用'mysqli_query($ dbc,$ query)',而不是'mysqli_connect()'。 –
你爲什麼要用「mysqli_connect」查詢 –