当我们通过for循环提供输入字段时如何填充数据库表?

时间:2016-08-17 13:14:04

标签: php mysqli

<?php
mysql_connect('localhost','root','');
mysql_select_db('filters');
$select_no="";
?>
<!DOCTYPE html>
<html>
<head>
    <title> filters program</title>
</head>
<body>
<form method="POST" action="">
    Select No. OF Fields    <input type="number" style="width:100px;border-   radius:10px;" id="selct_no" name="select_no" value=""> To <input type="submit" name="create" id="create_fields" value="create" style="width:70px;border-radius:10px;" onclick="createFields()"> <br><br>

        <?php
        //IF CONDIOTIN FOR CREATIG ITEMS FIELD depend on user need....
        if(isset($_POST['create'])){
            $select_no=$_POST['select_no'];
        //FOR loop for creating fileds...  
      // loop for generating given no. of fields... 
            for ($i=0; $i < $select_no ; $i++) { 
                global $x;
                $x = $i+1;

                echo $x.'.) Enter Products Name and Prices.... <br>' ;
                echo "<input type='text' style='width:300px; height:25px; border-radius:10px;' placeholder='Enter Items' id='id_item' name='items".$x."' value=''> <input type='text' style='width:100px;height:25px; border-radius:10px;' placeholder='Price' id='id_price' name='price".$x."' value=''> <br/><br/> ";
        }       
        echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button name='submit'                     
                    style='width:300px;height:25px;border-radius:10px'     id='submit_cat'>submit</button>";
        echo "<br><No of created fields are ".$x;

}
        //if condition for saving data in database table name filters and table name products....
        if(isset($_POST['submit'])){
            global $x;
            for ($a=1;$a<=$x;$a++){
                $product=$_POST["items".$a];
                $price=$_POST["price".$a];
                echo "$product";
                echo "$price";
                $sql= "INSERT INTO products SET     product='$product',price='$price'";
                $res=mysql_query($sql);
                if ($res) {
                echo " data submitted Successfully";
                }else{
                echo "Not submitted because ".mysql_error();}
        }
    }


?>

</form>
</body>
</html>

这里我想要的是,一些好友请告诉我我将用什么概念填充信息,我的数据库表中的每一个细节

这里我提供snapeshot也是为了你的理解..... this is the first view when we enter value it will creat fields in this image 4 fields are created when we press (create) button after enter 4 in first input field

1 个答案:

答案 0 :(得分:0)

您在插入查询中遇到错误,请像这样形成您的查询

  INSERT INTO table_name  
 (column1,column2,column3,...)
  VALUES (value1,value2,value3,...);

编辑: 问题是GLOBAL变量,缺少表单和输入查询,这是工作解决方案:         

    <html>
        <head>
            <title> filters program</title>
        </head>
        <body>
            <?php
            //if condition for saving data in database table name filters and table name products....
            if(isset($_POST['name_s'])){
                session_start();
                $x = $_SESSION["number_of_x"];
                for ($a=1;$a<=$x;$a++){
                    $product=$_POST["items".$a];
                    $price=$_POST["price".$a];
                    echo "$product";
                    echo "$price";
                    $sql= "INSERT INTO products (product, price) VALUES('$product','$price')";
                    $res=mysql_query($sql);
                    if ($res) {
                    echo " data submitted Successfully";
                    }else{
                    echo "Not submitted because ".mysql_error();}
                }
            }else{
            ?>
                <form method="POST" action="#">
                    Select No. OF Fields    <input type="number" style="width:100px;border-   radius:10px;" id="selct_no" name="select_no" value=""> To <input type="submit" name="create" id="create_fields" value="create" style="width:70px;border-radius:10px;" onclick="createFields()"> <br><br>
                </form>
            <?php
            }
            //IF CONDIOTIN FOR CREATIG ITEMS FIELD depend on user need....
            if(isset($_POST['create'])){
                $select_no=$_POST['select_no'];
                // Start the session
                session_start();
                 $_SESSION["number_of_x"] = $_POST['select_no'];
            //FOR loop for creating fileds...  
            // loop for generating given no. of fields... 
            ?>
            <form method="POST" action="#">
            <?php
                for ($i=0; $i < $select_no ; $i++) { 
                    global $x;
                    $x = $i+1;

                    echo $x.'.) Enter Products Name and Prices.... <br>' ;
                    echo "<input type='text' style='width:300px; height:25px; border-radius:10px;' placeholder='Enter Items' id='id_item' name='items".$x."' value=''> <input type='text' style='width:100px;height:25px; border-radius:10px;' placeholder='Price' id='id_price' name='price".$x."' value=''> <br/><br/> ";
                }       
            ?>
               <input type="submit" name='name_s'  style='width:300px;height:25px;border-radius:10px'     id='submit_cat'></input>";
            </form>
            <?php
            }
            ?>
        </body>
    </html>

希望这有帮助。