2013-10-04 78 views
-2

這裏是我的代碼,它接受通過複選框和文本框的順序。如何使用複選框值訪問數據庫使用PHP?

place_order.php

<?php require_once("includes/session.php"); ?> 
    <?php require_once("includes/connection.php"); ?> 
    <?php require_once("includes/functions.php"); ?> 
    <?php global $public; 
    confirm_logged_in(); ?> 
    <?php include("includes/header.php"); ?> 
    <table> 
    <tr> 
     <td id="navigation"> 
      <br><br> <a href="index.php">Return to public site</a> 
     </td>`enter code here` 
    <td id="page"> 
    <br> 
    <h3>Place Your Order Here...</h3><br> 
    <?php 
    $menu = get_all_menu($public); 
    print "<form method='POST' action='process.php'>"; 
    while($info = mysql_fetch_array($menu)) 
    { 

     echo "<fieldset style='width:830;height:260 padding='15'>"; 
     echo "<legend><h4>{$info['menu_name']}</h4></legend>"; 
     echo "<table cellpadding='6'><tr>"; 
     $submenu = get_submenus_for_menu($info['id'],$public); 
     while($sub = mysql_fetch_array($submenu)) 
     { 
     if($sub['sub_position']==4 || $sub['sub_position']==7 || $sub['sub_position']==10 ||$sub['sub_position']==13) 
     { 
      echo "</tr><tr>"; 
     } 
     echo "<td><input type='checkbox' name='feature[]' value='{$sub['submenu_name']}'>". $sub['submenu_name'] ; 
     echo "<td><input type='textbox' name= 'quantity[]' value='Qty' size='4'>"; 


     } 
     echo "</table>"; 
     print "</fieldset>"; 
     echo "<br><br>"; 

    } 
     print "<input type='submit' name='b1' value='  ORDER !!  '>"; 

     //echo "</tr></table>"; 
     print "</form>"; 

    ?> 
    </td></tr></table> 
    <?php require("includes/footer.php"); ?> 

爲get_cost_for_submenus代碼()

function get_cost_for_submenus($submenu_name, $public = true) { 
     global $connection; 
     $query = "SELECT * 
       FROM submenu "; 
     $query .= "WHERE submenu_name= {$submenu_name} "; 
     //$query .= "LIMIT 1"; 
     $result_set = mysql_query($query, $connection); 
     confirm_query($result_set); 
     return $result_set; 
    } 

process.php用於計算價格......

<?php require_once("includes/session.php"); ?> 
<?php require_once("includes/connection.php"); ?> 
<?php require_once("includes/functions.php"); ?> 
<?php confirm_logged_in(); ?> 
<?php include("includes/header.php"); ?> 

<table> 
<tr> 
    <td id="navigation"> 
     <br><br> <a href="index.php">Return to public site</a> 
    </td> 
<td id="page"> 
<br> 
<?php 
    $items = $_POST['feature']; 
    $quantity = $_POST['quantity']; 
    $price=0; 
    $N = count($items); 
    $n = count($quantity); 
    for($i=0;$i<$N;$i++)  
    { 
     $temp_item=get_cost_for_submenus($items['i'],$public); 
     $price+=($temp_item['cost']*$quantity['i']);  
    } 
    echo "<br><h3> TOTAL AMOUNT FOR YOUR ORDER = ". $price ."</h1> "; 
?> 
</td></tr></table>    
<?php require("includes/footer.php"); ?> 

,我發現了以下錯誤當執行place_order.php時(登錄後)。

Notice: Undefined index: i in G:\xampp\htdocs\choc_room\process.php on line 22 

Notice: Undefined variable: public in G:\xampp\htdocs\choc_room\process.php on line 22 
Database query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 2 

請別人幫我....我是新來的PHP語言

+0

'$ items ['i']'應該是'$ items [$ i]' –

回答

0
$query = "SELECT * FROM submenu "; 
    $query .= "WHERE submenu_name= {$submenu_name} "; // string value not quoted 

是不正確的格式,應該是

$query = "SELECT * FROM submenu "; 
    $query .= "WHERE submenu_name= '$submenu_name' "; 

然後

$items['i'] should be $items[$i] 

相同

$quantity['i'])  
+0

Panky非常感謝......但是現在我將$ price的值作爲零....任何其他錯誤? m現在無能爲力.... – user2033058

+0

非常感謝...但是現在我將$ price的價值作爲零....是否還有其他錯誤?現在毫無頭緒.... – user2033058

+0

您從HTML發佈的數量不是數字,這就是爲什麼以及何時價格乘以它們,那麼它就變爲0.'name ='quantity []'value ='Qty'' that value應該是一個數字,我想 –