Php 将表单中的数组值插入mysql
我有3个PHP文件,在select.PHP文件中,您必须选择所需的表单数量。。表单包含3个输入字段。 详情如下:Php 将表单中的数组值插入mysql,php,mysql,arrays,Php,Mysql,Arrays,我有3个PHP文件,在select.PHP文件中,您必须选择所需的表单数量。。表单包含3个输入字段。 详情如下: <form method="post" action="index.php" > Continue insertion with <select name="counters" id="insert_rows"> <option value="1">1</option> <option value="
<form method="post" action="index.php" >
Continue insertion with <select name="counters" id="insert_rows">
<option value="1">1</option>
<option value="2" selected="selected">2</option>
<option value="3">3</option>
</select>
rows
<input type="submit" value="Go" />
</form>
继续插入
1.
2.
3.
排
选择插入数后,它会将该数传递给index.php,其中包含:
<?php
echo "<form method = 'POST' action = 'process.php'>";
for($counter = 0; $counter < $_POST['counters']; $counter++)
{
echo "Service Name: <input type = 'text' name = 'name[]' class = 'class_name'/><br/>";
echo "Service Version : <input type = 'text' name = 'version[]' class = 'class_name'/><br/>";
echo "Service type : <input type = 'text' name = 'type[]' class = 'class_name'/><br/>";
}
echo "<input type = 'submit' value = 'SEND'/>";
echo "</form>";
?>
我建议将第一个提交的计数值保存在第二种形式的隐藏输入中,然后您将能够循环使用它并插入数据
<?php
echo "<form method = 'POST' action = 'process.php'>";
for($counter = 0; $counter < $_POST['counters']; $counter++)
{
echo "Service Name: <input type = 'text' name = 'name[]' class = 'class_name'/><br/>";
echo "Service Version : <input type = 'text' name = 'version[]' class = 'class_name'/><br/>";
echo "Service type : <input type = 'text' name = 'type[]' class = 'class_name'/><br/>";
}
echo '<input type="hidden" name="cntr" value="'.$_POST['counters'].'" />'
echo "<input type = 'submit' value = 'SEND'/>";
echo "</form>";
?>
var\u dump($\u POST)
将告诉您在PHP中得到了什么。从这里开始,它是直截了当的:获取一些值,将它们填充到数据库中。重复此操作,直到完成。使用类似的for
循环,用于为($counter=0;$counter<$\u POST['name'];$counter++)构建表单->。对于每个表单输入,使用$counter
->$\u POST['name'][$counter]
,$\u POST['version'][$counter]
,$\u POST['type'][$counter]
1。)学习如何编写简单的插入
查询。2.)学习如何使用或。3.)学习如何在PHP中处理表单提交。4.)如果且仅当您遇到错误或遇到特定问题时,请在此处询问。这里没有人会教你怎么做这些。@PatrickQ Patrick小姐,首先感谢你的回答。。1) 我知道如何编写一个简单的插入查询“如果你需要,我可以教你”2)我知道如何使用mysqli:)3)我知道如何用PHP处理表单。4) 我不会回复你,因为我们没有使用你自己的网站。。所以我会给你一个建议“教你自己如何提高你的沟通技巧”。br您的原始帖子(更新前)没有尝试执行上述任何步骤。您只需询问如何编写一个函数“将所有这些值插入数据库”。这是一个涉及多个部分的非常广泛的请求。即使在更新之后,您仍然没有尝试插入值。因此,你不应该只是说你知道如何做所有这些事情,而应该通过自己尝试解决这个问题来向我们展示。
<?php
echo "<form method = 'POST' action = 'process.php'>";
for($counter = 0; $counter < $_POST['counters']; $counter++)
{
echo "Service Name: <input type = 'text' name = 'name[]' class = 'class_name'/><br/>";
echo "Service Version : <input type = 'text' name = 'version[]' class = 'class_name'/><br/>";
echo "Service type : <input type = 'text' name = 'type[]' class = 'class_name'/><br/>";
}
echo '<input type="hidden" name="cntr" value="'.$_POST['counters'].'" />'
echo "<input type = 'submit' value = 'SEND'/>";
echo "</form>";
?>
if (!empty($_POST['cntr'])) {
$query = 'INSERT INTO services (name, version, type) VALUES ';
for ($x = 0; $x < $_POST['cntr']; $x++)
{
$query .= "('".mysqli_escape_string($_POST['name'][$x])."','"
.mysqli_escape_string($_POST['version'][$x])."','"
.mysqli_escape_string($_POST['type'][$x]).
"'),";
}
$query = substr_replace($query, '', -1);
mysqli->query($query);
}