如何使用复选框值使用PHP访问数据库?

时间:2013-10-04 14:00:12

标签: php mysql checkbox

这是我的代码,它通过复选框和文本框接受订单。

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语言的新手

1 个答案:

答案 0 :(得分: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'])