我正在进行的项目基本上涉及输入框并将它们存储到mysql数据库中。我尝试使用javascript生成此表单,如果需要可以添加另一个表单。我认为我遇到的问题是让它分配一个不同的"名称"到第二个生成的表格。 ex red,red1,red2等。然后还试图让它与php-mysqli一起工作。
到目前为止,我已经能够将其发布到桌面上,但是它将值发布在第二组输入框中。 有人可以提供一些建议吗?
<form id="color_form" action="postcolors.php" method="post">
<input id="name" class="color_entry" action="postcolors.php" method="post" name="name" placeholder="song name" style="background-image: url("data:image/png;base64,iVBORw0KGgoAAAA…nt: scroll; background-position: right center; cursor: auto;"></input>
<input id="red" class="color_entry" action="" method="post" name="red" placeholder="red"></input>
<input id="green" class="color_entry" action="" method="post" name="green" placeholder="green"></input>
<input id="blue" class="color_entry" action="" method="post" name="blue" placeholder="blue"></input>
<input id="color" class="color_entry" action="" method="post" name="color" placeholder="color"></input>
<input id="name" class="color_entry" action="postcolors.php" method="post" name="name" placeholder="song name"></input>
<input id="red" class="color_entry" action="" method="post" name="red" placeholder="red"></input>
<input id="green" class="color_entry" action="" method="post" name="green" placeholder="green"></input>
<input id="blue" class="color_entry" action="" method="post" name="blue" placeholder="blue"></input>
<input id="color" class="color_entry" action="" method="post" name="color" placeholder="color"></input>
<input type="submit" value="submit" action="postcolors.php" method="post"></input>
</form>
&#13;
$song=mysqli_real_escape_string($connect, $_POST['name']);
$song = str_replace(' ', '', $song);
mysqli_query($connect, "CREATE TABLE $song (id int(4) NOT NULL auto_increment, red int(2) NOT NULL, green int(2) NOT NULL, blue int(2) NOT NULL, color varchar(30) NOT NULL, index(id))");
$red=mysqli_real_escape_string($connect, $_POST['red']);
$green=mysqli_real_escape_string($connect, $_POST['green']);
$blue=mysqli_real_escape_string($connect, $_POST['blue']);
$color=mysqli_real_escape_string($connect, $_POST['color']);
mysqli_query($connect, "INSERT INTO $song (red,green,blue,color) VALUES ('$red', '$green', '$blue', '$color')");
&#13;
答案 0 :(得分:1)
首先,id=""
在页面上必须是唯一的,因此,除非您实际在javascript中使用id's
,否则可以将其关闭。如果您在javascript中使用它们,则必须考虑使它们成为唯一的方法。
其次,您可以要求浏览器使用name="color[]"
作为数组而不是标量值返回字段名称。
所以更改名称字段如下: -
<form id="color_form" action="postcolors.php" method="post">
<input class="color_entry" action="postcolors.php" method="post" name="name[]" placeholder="song name" style="background-image: url("data:image/png;base64,iVBORw0KGgoAAAA…nt: scroll; background-position: right center; cursor: auto;"></input>
<input class="color_entry" action="" method="post" name="red[]" placeholder="red"></input>
<input class="color_entry" action="" method="post" name="green[]" placeholder="green"></input>
<input class="color_entry" action="" method="post" name="blue[]" placeholder="blue"></input>
<input class="color_entry" action="" method="post" name="color[]" placeholder="color"></input>
<input class="color_entry" action="postcolors.php" method="post" name="name[]" placeholder="song name"></input>
<input class="color_entry" action="" method="post" name="red[]" placeholder="red"></input>
<input class="color_entry" action="" method="post" name="green[]" placeholder="green"></input>
<input class="color_entry" action="" method="post" name="blue[]" placeholder="blue"></input>
<input class="color_entry" action="" method="post" name="color[]" placeholder="color"></input>
<input type="submit" value="submit" action="postcolors.php" method="post"></input>
</form>
现在$ _POST会有$ _POST ['color'] [0]和$ _POST ['color'] [1],所以你可以把处理改为像
foreach ($_POST['name'] as $idx => $name ) {
mysqli_query($connect,
"INSERT INTO $song (red,green,blue,color)
VALUES ('{$_POST['red'][$idx']}',
'{$_POST['green'][$idx']}',
'{$_POST['blue'][$idx']}',
'{$_POST['color'][$idx']}'
)"
);
}
答案 1 :(得分:0)
IIRC可以使用红色[]作为框的名称,这将使$ _POST [&#39; red&#39;]数组迭代。