我需要帮助来消除我得到的错误。我有一个购物车,通过购物车更新页面显示在产品页面上。我为每个列出的产品收到以下错误(通知):
Notice: Undefined variable: obj in C:\xampp\htdocs\Project2\browseproducts.php on line 89
Notice: Trying to get property of non-object in C:\xampp\htdocs\Project2\browseproducts.php on line 89
当我点击任何添加到购物车按钮时,没有任何内容添加到购物车,我得到:
Notice: Undefined index: Stock in C:\xampp\htdocs\Project2\cart_update.php on line 25
我的代码是:
<?php
$user = "root";
$password = "";
$database = "WebStore";
mysql_connect("localhost", $user, $password);
@mysql_select_db($database) or die("Unable to select database");
$current_url = base64_encode("http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
$query = "SELECT * FROM PRODUCTS ORDER BY Category";
$result = mysql_query($query);
if($result === FALSE) {
die(mysql_error());} // TODO: better error handling
// keeps getting the next row until there are no more to get
while ($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<form method='post' action='cart_update.php'>";
echo "<td>" . "<img src=" .$row['IconURL'] . "alt=" .$row['Name'] ."/>" . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Descr'] . "</td>";
echo "<td>" . '$' . $row['Price'] . "</td>";
echo "<td>" . $row['Stock'] . "</td>";
echo "<td> <button class='add_to_cart'>Add to Cart</button> </td>";
echo '<input type="hidden" name="product_code" value="'.$obj->product_code.'" />';
echo '<input type="hidden" name="type" value="add" />';
echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
echo "</form>";
echo "</tr>\n";
}
mysql_close();
?>
</table>
我的部分购物车更新代码:
//add item in shopping cart
if(isset($_POST["type"]) && $_POST["type"]=='add')
{
$product_code = filter_var($_POST["product_code"], FILTER_SANITIZE_STRING); //product code
$Stock = filter_var($_POST["Stock"], FILTER_SANITIZE_NUMBER_INT); //product code
$return_url = \base64_decode($_POST["return_url"]); //return url
//limit quantity for single product
if($Stock < 1){
die('<div align="center">We are currently out of stock<br /><a href="browseproducts.php/">Back To Products</a>.</div>');
}
$queryy = "SELECT TOP 1 Name,Price FROM Products WHERE product_code='$product_code'";
//MySqli query - get details of item from db using product code
$results = mssql_query($queryy);
$obj = mssql_fetch_object($results);
Aaaany的帮助将不胜感激!