duanji1026 2016-08-17 13:14
浏览 49
已采纳

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

<?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>

here what i want is that, some buddy please tell me by what concept i'll make populated that information each and every details in my database table

here i provide snapeshot also for your understanding.....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条回答 默认 最新

  • doubei5310 2016-08-17 13:23
    关注

    You have an error in insert query, form your query like this

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

    EDIT: Problem was with GLOBAL variable, missing form and input query, here is working solution:

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

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!