2016-08-17 28 views
0
<?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> 

在这里,我想那是什么,有些哥们请什么概念我会让人口稠密,在我的数据库表中的信息的每一个细节当我们通过for循环提供输入字段时如何填充数据库表?

这里我提供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

+0

无效的插入查询。请查看在mysql中插入查询的语法 –

回答

0

你在插入查询错误,表格这样

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

编辑: 问题是与全局变量,丢失的形式输入查询,这里是工作的解决方案:

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

希望这有助于。

+0

ohke thanku先生我会尝试这个查询... 非常感谢... –

+0

actully先生它的我的错误,我没有看到我的代码发布之前 有在我的两个注释之间代码我错过这两个线路如下所示 '// IF CONDIOTIN FOR CREATIG ITEMS领域.... 如果(isset($ _ POST [ '创建'])){ $ select_no = $ _ POST [ 'select_no']; // for循环建立的Fileds ...' 和问题是,它在我的鳕鱼不会 进入'如果(isset($ _ POST [ '提交'])){}' 洙请帮助我, 在此先感谢..... –

+0

事情是你的提交按钮没有做任何事情,因为我可以看到,你需要创建

Oncodeeater