使用php插入多个数组

时间:2012-10-17 16:23:27

标签: php mysql

我正在尝试从动态填充的输入文本字段中插入值。我必须采取哪些进一步措施?

我的样本表格:

<form action="" method="POST">
    <label>Name:</label><input type="text" name="name[]">
    <label>Name:</label><input type="text" name="grade[]">
    <input type="submit" name="submit">
</form>

我的insert.php文件:

$grade = $_POST['grade'];
$name = $_POST['name'];
$count_name = count($_POST['name']);

for($i=0;$i<$count_name ;$i++){
    $new_name  = $name[$i];

    $query = "INSERT INTO test_table (name, grade)VALUES('$new_name','$grade')";
    $result = mysql_query($query); 

}

1 个答案:

答案 0 :(得分:0)

试一试:

形式:

<form action="insert.php" method="POST">
    <label>Name:</label><input type="text" name="name[]">
    <label>Name:</label><input type="text" name="grade[]">
    <input type="submit" name="submit">
</form>

insert.php

$grade = $_POST['grade'];
$name = $_POST['name'];
$count_name = count($_POST['name']);

for($i=0;$i<$count_name ;$i++){
    $_name  = mysql_escape_string($name[$i]);
    $_grade  = mysql_escape_string($grade[$i]);

    $query = "INSERT INTO test_table (name, grade) VALUES ('$_name','$_grade')";
    $result = mysql_query($query); 
}