0

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

我的样本表格:

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

}
4

1 回答 1

0

Try it:

Form:

<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); 
}
于 2012-10-17T16:38:11.813 回答