我如何使此页面正常工作,它只会重定向到首页

时间:2019-03-25 08:48:01

标签: php sql

我正在制作电子商务网站并将其添加到购物篮脚本中却没有做任何事情    我希望它可以将数据从工作正常的产品页面插入购物篮。请看一下,帮我解决..它没有给出任何语法错误或语法分析错误,只是什么都不做,当我单击“购买”时,只需将我重定向到首页即可。

<?php
error_reporting(E_ALL);
session_start();
require("db.php");
require("functions.php");
$validid = pf_validate_number($_GET['id'], "redirect", $config_basedir);
$prodsql = "SELECT * FROM products WHERE id = " . $_GET['id'] . ";";
$prodres = mysqli_query($prodsql);
$numrows = mysqli_num_rows($prodres);
$prodrow = mysqli_fetch_assoc($prodres);
if($numrows == 0)
{     
  header("Location: " . $config_basedir);
} else {
  if($_POST['submit'])
  {
    if($_SESSION['SESS_ORDERNUM'])
    {
      $itemsql = "INSERT INTO orderitems(order_id, product_id, quantity) VALUES("
                 . $_SESSION['SESS_ORDERNUM'] . ", "
                 . $_GET['id'] . ", "
                 . $_POST['amountBox'] . ")";
      mysqli_query($itemsql);
    } else {
      if($_SESSION['SESS_LOGGEDIN'])
      {
        $sql = "INSERT INTO orders(customer_id, registered, date) VALUES("
               . $_SESSION['SESS_USERID'] . ", 1, NOW())";
        mysqli_query($sql);
        session_register("SESS_ORDERNUM");
        $_SESSION['SESS_ORDERNUM'] = mysqli_insert_id();
        $itemsql = "INSERT INTO orderitems(order_id, product_id, quantity) VALUES("
                   . $_SESSION['SESS_ORDERNUM']
                   . ", " . $_GET['id'] . ", "
                   . $_POST['amountBox'] . ")";
        mysqli_query($itemsql);
      } else {
        $sql = "INSERT INTO orders(registered, date, session) VALUES("
               . "0, NOW(), '" . session_id() . "')";
        mysqli_query($sql);
        session_register("SESS_ORDERNUM");
        $_SESSION['SESS_ORDERNUM'] = mysqli_insert_id();
        $itemsql = "INSERT INTO orderitems(order_id, product_id, quantity) VALUES("
                   . $_SESSION['SESS_ORDERNUM'] . ", " . $_GET['id'] . ", " 
                   . $_POST['amountBox'] . ")";
        mysqli_query($itemsql);
      }
    }
    $totalprice = $prodrow['price'] * $_POST['amountBox'] ;
    $updsql = "UPDATE orders SET total = total + "
              . $totalprice . " WHERE id = "
              . $_SESSION['SESS_ORDERNUM'] . ";";
    mysqli_query($updres);
    header("Location: " . $config_basedir . "showcart.php");
  } else {
    require("header.php");
    echo "<form action='addtobasket.php?id="
         . $_GET['id'] . "' method='POST'>";
    echo "<table cellpadding='10'>";
    echo "<tr>";
    if(empty($prodrow['image'])) 
    {
      echo "<td><img src='./productimages/dummy.jpg' width='50' alt='"
           . $prodrow['name'] . "'></td>";
    } else {
      echo "<td><img src='./productimages/" . $prodrow['image']
           . "' width='50' alt='" . $prodrow['name']
           . "'></td>";
    }
      echo "<td>" . $prodrow['name'] . "</td>";
      echo "<td>Select Quantity <select name='amountBox'>";
      for($i=1;$i<=100;$i++)
      {
        echo "<option>" . $i . "</option>";
      }
      echo "</select></td>";
      echo "<td><strong>&pound;"
           . sprintf('%.2f', $prodrow['price'])
           . "</strong></td>";
      echo "<td><input type='submit' name='submit' value='Add to basket'></td>";
      echo "</tr>";
      echo "</table>";
      echo "</form>";
   }
}
require("footer.php");
error_reporting(E_ALL);
?>

1 个答案:

答案 0 :(得分:1)

有两次重定向使您的用户返回您的首页

第一:

$validid = pf_validate_number($_GET['id'], "redirect", $config_basedir);

确保$_GET['id]具有有效值

秒:

$prodsql = "SELECT * FROM products WHERE id = " . $_GET['id'] . ";";
$numrows = mysqli_num_rows($prodres);
// ...
if($numrows == 0)
{     
  header("Location: " . $config_basedir);
}

在此行中检查您的查询:

$prodsql = "SELECT * FROM products WHERE id = " . $_GET['id'] . ";";

确保它不返回空结果($numrows == 0

首先在您的DBMS前端上对其进行测试