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.

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

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历