doubutao6216 2013-09-24 10:05
浏览 63

从DB中的复选框和文本框插入数据

I've created two php files with code for an order form for a cafeteria and the correspronding lines of code for inserting the posted values in my database. Here I present them.

CODE FOR addorder_form.php

**<?php 

       db_connect();

       $cats=array("Kafedes", "Rofhmata", "Pota", "Snack/Glyka"); 
       $arrlength=count($cats);

    for($i=0;$i<$arrlength;$i++) {  
        $sql = mysql_query('SELECT title FROM products WHERE cname="'.$cats[$i].'"') or die(mysql_error());
        echo '<div id="main_content">';
        echo "<h4 style=color:#800000> ".$cats[$i]."</h4>"; 
        echo "<br />"; 


        while($row = mysql_fetch_array($sql, MYSQL_BOTH)){
            echo "<div id='center' style='align:center'>";
            echo "<input style='text-align:right;' type='checkbox' action='addorder.php' name='products[]' value='".$row["title"]."'>".$row["title"];
            echo '</div>';

            echo ' <div id="center_side" style="float:right"><form "method="post" action="addorder.php"><input type="text" size="4" padding-left="0.2em" name="quantity"/>';
            echo '</div>';
        echo '</div>';  
        echo '<br />';  
        }
    }
    echo '<form name="addorder" method="" action="addorder.php" onclick="addorder.php">';
                echo '<input type="submit" value="Add order" style="float: right;"><br/>';
                echo '</form>';   
    ?>**

CODE FOR addorder.php

**<?php
        include_once("buzzcafe_fns.php");
        do_html_header("");




$quantity = '';
$title = '';

if (isset($_POST['quantity']) && isset($_POST['products'])) {
    if(isset($_POST["Submit"])) {
    $quantity = $_POST['quantity'];
$title = $_POST['products'];
if($_POST["Submit"] == "Submit")
{   
for ($i=0; $i<sizeof($title); $i++) {
db_connect();
$insertOrder = mysql_query("INSERT INTO orders VALUES('".$title[i]."','".$quantity."')")or die(mysql_error());

    }
echo "Record inserted"; 

}
}
}
?>**

When I run them I have non any syntax errors, though it does not work. As far as the db_connect() is set in the buzzcafe_fns.php file I've included and it is checked that works properly. How can I make my "INSERT INTO" work?

  • 写回答

1条回答 默认 最新

  • dousi6087 2013-09-24 10:29
    关注
    You can use another insert query
    First You will check if $title variable array or not.
    Next Using For loop
    <?php 
    $con=mysql_connect("localhost","root","");
    $db=mysql_select_db("yourdatabasename here" ,$con);
    $count=count($title);
    for($i=0;$i<$count;$i++){
    mysql_query("INSERT INTO orders SET title='".$title[$i]."', quantity='".$quantity."'");
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算