将多个动态文本字段插入数据库

时间:2014-01-07 14:25:14

标签: php html mysql

我有一个数据库名称bfp,它有一个表bfp_personnel_eligibility 由姓名,日期,地点,评级栏组成。如何将下面显示的动态texttfield插入此表?我下面的PHP代码似乎不能正常工作只是将空白数据插入表

        <td><input type="text" name="name[]" value="" /></td>
        <td><input type="text" name="rating[]" value="" /></td>
        <td><input type="date" name="date[]" value="" /></td>
        <td><input type="text" name="place[]" value="" /></td>

PHP代码:

$name = $_POST['name']; 
foreach($name as $names) : 
  $values=mysql_real_escape_string($names); 
  $sql="INSERT INTO bfp_personnel_eligibility (id, eligibility_name, rating, doe, poe) VALUES ('{$k}','{$values}', '{$_POST['rating']}', '{$_POST['date']}', '{$_POST['place']}')"; 
 if (!mysql_query($sql,$con))
   { 
       die('Error: ' . mysql_error()); 
   } 
endforeach; 

2 个答案:

答案 0 :(得分:0)

i didnt understand your question properly , i guess u want below

 <td><input type="text" name="name[]" value="" /></td>
 <td><input type="text" name="rating[]" value="" /></td 
 <td><input type="date" name="date[]" value="" /></td>
 <td><input type="text" name="place[]" value="" /></td>

<?php

   foreach($_POST["name"] as $k=>$val){
    //assuming your primary key 'id' is autoincrement  
    $sql="INSERT INTO bfp_personnel_eligibility 
     ( 
       eligibility_name, 
      rating, 
      doe, 
      poe
      )
      VALUES
     (
      '".$val."',
     '".$_POST['rating'][$k].",
     '".$_POST['date'][$k]."',
     '".$_POST['place'][$k]."'
     )";
 }
?>

答案 1 :(得分:-1)

<?php
include 'db_config.php';
foreach ($_POST['specification_name'] as $key => $value) 
{
    $builders=$_POST['builders_name'];
    $project=$_POST['project_name'];

    $insert_query="INSERT INTO specifications(builders_id,project_id,specification_name,description)VALUES('".$builders."','".$project."','".$value."','".$_POST['specification_description'][$key]."')";

    $result=mysqli_query($db_connect,$insert_query);
}
if ($result) 
    {`enter code here`
        echo "Inserted";
    }`enter code here`
?>