产品不会被添加到购物车

时间:2014-11-25 11:28:06

标签: php html mysql database

我正在处理应该显示表单的 onlineshop.php 页面,每个表单都有一个带有“添加到购物车按钮”的产品。产品会显示,但问题是我选择的产品没有添加到购物车中。我不知道它是查询中的问题还是代码本身。

onlineshop.php:

<?php 
ini_set('display_errors',1); 
error_reporting(E_ALL);
include('connection.php');

if ( isSet($_POST['Pro_ID'], $_POST['Price'] , $_POST['Pro_Name'] ) ) 
{
    $qry="INSERT INTO shopping_cart(Cart_Name,Pro_Name,Pro_ID,Price) VALUES ( 'dzfgtdfxdf','$_POST[Pro_Name]','$_POST[Pro_ID]' ,  '$_POST[Price]'  ) ";                 
    $qry = 'SELECT * FROM product' ;
}
else            
    $qry = 'SELECT * FROM product' ;
    //Run QUERY
    $result = mysql_query ($qry);
?> 


<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Online Shop</title>



    <!-- Custom CSS -->
    <link href="css/css.css" rel="stylesheet">

    <!-- Fonts -->
    <link href="http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">
    <link href="http://fonts.googleapis.com/css?family=Josefin+Slab:100,300,400,600,700,100italic,300italic,400italic,600italic,700italic" rel="stylesheet" type="text/css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

</head>

<body scroll="no" style="overflow: hidden">

<div class="wrapper">
<div style="z-index:10;float:right;top:20px;    position: relative;font-size:10px;">&nbsp;&nbsp;&nbsp; <a href="shoppingcart.php"><img src="cartt.png" alt="ٍاخححهىل }شقف" style="width:40px;height:40px"><br/>Shopping Cart</a></div>
    <div class="logo"><a href="index.html"><img src="logo.png"  alt="logo" height="162px"width="800px"  style="border-style:none" title="Home"/></a></div>



    <!-- Navigation -->
    <nav>
        <div class="container">
            <!-- Brand and toggle get grouped for better mobile display -->


            <!-- Collect the nav links, forms, and other content for toggling -->

          <ul id="nav">
  <li>
    <a href="index.html">Home</a>

  </li>  
  <li class="sousMenu"><a>Book/Cancel an appointment</a>
    <ul>
      <li><a href="book.html">Book an appointment</a>
      </li>
      <li><a href="cancel.html">Cancel an appointment</a>
      </li>
    </ul>
  </li>
  <li>
    <a href="onlineshop.html">Online Shop</a>
  </li>
  <li>
    <a href="viewcatalogue.html">View Catalogue</a>
  </li>
  <li>
    <a href="aboutus.html">About us</a>
  </li>
  <li>
    <a href="contactus.html">Contact us</a>
  </li>
  <li>
    <a href="managerlogin.html">Manager log-in</a>
  </li>
</ul>
            </div>
            <!-- /.navbar-collapse -->

        <!-- /.container -->
    </nav>
 <div id="boxcontainer">
<div class="contentboxes1">








        <div class="row">
            <div class="box">


                <div class="col-lg-12">

             <h2 style="text-align:center; color:#ab4b4b;">Online Shop</h2>


                    <hr class="visible-xs">             <br/>


                 <center><div style="left:100px;display:inline;"><select name="categories"
<option value="hair">Hair products</option>
<option value="makeup">Makeup</option>
<option value="nail">Nail Products</option>
<option value="skin">Skin Care</option>
</select></div>

    <form action="demo_form.asp"style="display:inline;" >
  <input type="search" name="searchSpecilist">
  <input type="submit" value="Search" style="float:none;    width: 70px; height: 20px;">
</form> <br><br><br></center>
<?php



if($result) {
$did = 0;
Print " <table style='width:100%'>";

while($info=mysql_fetch_array($result))
{
if($did==0){

Print"
<tr>
<td>
<form action='' method='post' enctype='multipart/form-data' style='display:inline;' action=''  >

    <figure>
  <img src='B7Brush.jpg'  alt='The Pulpit Rock' width='130' height='130' style='left:00px;top:00px; '>
  <figcaption style='text-align:center;'><b>".$info['Pro_Name']."</b><br/>".$info['Price']."<p style='color:green;'>".$info['Availability']."</p><p style='font-size:13px;display:inline'>Quantity:</p>
  <input type='number' name='quantity' min='1' max='10' style='width:3em;'></figcaption>
</figure>

<input type='hidden' name='Pro_ID' value='".$info['Pro_ID']."' />
<input type='hidden' name='Price' value='".$info['Price']."' />
<input type='hidden' name='Pro_ID' value='".$info['Pro_Name']."' />
<input type='hidden' name='Availability' value='".$info['Availability']."' />



<input name='button1' type='submit' value='Add To Cart' style='width:8em;   ' />
</form>
</td> ";
$did++;


continue; }

if($did==1){
Print "
<td>
<form action='' method='post' enctype='multipart/form-data' style='display:inline;' action=''  >

   <figure>
  <img src='eye.jpg'  alt='The Pulpit Rock' width='130' height='130' style='left:00px;top:00px; '>
  <figcaption style='text-align:center;'><b>".$info['Pro_Name']."</b><br/>".$info['Price']."<p style='color:green;'>".$info['Availability']."</p><p style='font-size:13px;display:inline'>Quantity:</p>
  <input type='number' name='quantity' min='1' max='10' style='width:3em;'></figcaption>
</figure>

<input type='hidden' name='Pro_ID' value='".$info['Pro_ID']."' />
<input type='hidden' name='Price' value='".$info['Price']."' />
<input type='hidden' name='Pro_ID' value='".$info['Pro_Name']."' />
<input type='hidden' name='Availability' value='".$info['Availability']."' />
<input name='button1' type='submit' value='Add To Cart' style='width:8em;   ' />
</form>
</td>";
$did++;
continue; }
if($did==2){
Print"
<td>
<form action='' method='post' enctype='multipart/form-data' style='display:inline;' action=''  >

    <figure>
  <img src='korres.jpg'  alt='The Pulpit Rock' width='130' height='130' style='left:00px;top:00px; '>
  <figcaption style='text-align:center;'><b>".$info['Pro_Name']."</b><br/>".$info['Price']."<p style='color:green;'>".$info['Availability']."</p><p style='font-size:13px;display:inline'>Quantity:</p>
  <input type='number' name='quantity' min='1' max='10' style='width:3em;'></figcaption>
</figure>

<input type='hidden' name='Pro_ID' value='".$info['Pro_ID']."' />
<input type='hidden' name='Price' value='".$info['Price']."' />
<input type='hidden' name='Pro_ID' value='".$info['Pro_Name']."' />
<input type='hidden' name='Availability' value='".$info['Availability']."' />
<input name='button1' type='submit' value='Add To Cart' style='width:8em;   ' />
</form>
</td>";
$did++;
continue; }

if($did==3){

Print"
<td>
<form action='' method='post' enctype='multipart/form-data' style='display:inline;' action=''  >

   <figure>
  <img src='self.jpg'  alt='The Pulpit Rock' width='130' height='130' style='left:00px;top:00px; '>
  <figcaption style='text-align:center;'><b>".$info['Pro_Name']."</b><br/>".$info['Price']."<p style='color:green;'>".$info['Availability']."</p><p style='font-size:13px;display:inline'>Quantity:</p>
  <input type='number' name='quantity' min='1' max='10' style='width:3em;'></figcaption>
</figure>

<input type='hidden' name='Pro_ID' value='".$info['Pro_ID']."' />
<input type='hidden' name='Price' value='".$info['Price']."' />
<input type='hidden' name='Pro_ID' value='".$info['Pro_Name']."' />
<input type='hidden' name='Availability' value='".$info['Availability']."' />


<input name='button1' type='submit' value='Add To Cart' style='width:8em;   ' />
</form>
</td> </tr>";
$did=0;

}
}

Print " </table>";

}

else echo "Your cart is empty";

    ?>
                </div>
            </div>
        </div>


  <div style="font-size:8px;">

                    <p style="display:inline;">Copyright &copy; SOFTWARE II KSU 2014. <br><a href="Sitemap.html">Sitemap</a> </p>  <div style="z-index:10;float:right;">           <img src="img/instagram.png" alt="instagram" style="width:30px;height:30px">&nbsp;<img src="img/facebook.png" alt="facebook" style="width:30px;height:30px">&nbsp;<img src="img/twitter.png" alt="twitter" style="width:30px;height:30px"></div>
                </div>
    </div>

</div>
    <!-- /.container -->
</div>
</body>

</html>
</html>
<?php 
// close database connection
mysql_close ($con);
?> 

4 个答案:

答案 0 :(得分:0)

更改

if ( isSet

if(isset

删除大写字母并将其替换为小写

答案 1 :(得分:0)

您似乎撰写了INSERT查询,但是您没有运行它。使用函数mysql_query()将查询发送到要执行的MySQL服务器。

答案 2 :(得分:0)

在执行INSERT之前,您似乎要覆盖$ qry变量。

所以你永远不会将数据插入表中,你只是选择它。

在SELECT之前运行INSERT,它应该可以工作。

答案 3 :(得分:0)

试试这个:

<?php 
ini_set('display_errors',1); 
 error_reporting(E_ALL);
include('connection.php');

                    if ( isset($_POST['Pro_ID'], $_POST['Price'] , $_POST['Pro_Name'] ) ) {


                    $qry="INSERT INTO shopping_cart(Cart_Name,Pro_Name,Pro_ID,Price) VALUES ( 'dzfgtdfxdf','$_POST[Pro_Name]','$_POST[Pro_ID]' ,  '$_POST[Price]'  ) ";

                        // 1) Run your SQL
                        mysql_query($qry);
                    }
                    // Actually, You don't need ELSE block statement, because this lines of code repeats in IF block, and in ELSE block.
                        $qry = 'SELECT * FROM product' ;
                    //Run QUERY
                    $result = mysql_query ($qry);



  ?>