2014-11-25 74 views
0

我正在致力于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); 
?> 
+0

把else语句'{''}' – Gowri 2014-11-25 11:31:38

+1

[**请不要在新代码'mysql_ *'功能**](HTTP:/ /bit.ly/phpmsql)。他们不再被维护[并且被正式弃用](http://j.mp/XqV7Lp)。看到[**红框**](http://j.mp/Te9zIL)?学习[*准备的语句*](http://j.mp/T9hLWi),并使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [这篇文章](http://j.mp/QEx8IB)将帮助你决定哪个。 – 2014-11-25 11:31:46

+2

INSERT INTO shopping_cart'查询似乎没有在任何地方执行。 – 2014-11-25 11:33:17

回答

0

变化

if (isSet 

如果(isset

卸下大写字母和小写

替换它
0

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

0

看起来您在执行INSERT之前覆盖了$ qry变量。

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

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

+0

我把\t \t \t \t \t \t $ result = mysql_query($ qry); 插入后,但它仍然无法正常工作 – user3419062 2014-11-25 11:44:55

+0

你是否也改变isSet isset像其他答案建议,你确定你正在发布你正在寻找的所有适当的项目吗?换句话说,你确定你确实在执行该代码,还是只是跳过并转到其他地方? 试穿 'echo“test”; exit();' 在INSERT语句之上,看它是否触发。 – jdcookie 2014-11-25 11:48:17

+0

你能解释你的最后一点吗? \t \t \t is this line wrong:\t \t if(isset($ _ POST ['Pro_ID'],$ _POST ['Price'],$ _POST ['Pro_Name']])??我如何知道它是否执行? – user3419062 2014-11-25 12:01:05

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); 



    ?> 
相关问题