基本上,我有一个有5行的表格。
像这样:
<form action="process.php" method="post">
<p><label>Name</label>
<input type="text" name="name[]" />
<label>email</label>
<input type="text" name="email[]" />
</p>
<p><label>Name</label>
<input type="text" name="name[]" />
<label>email</label>
<input type="text" name="email[]" />
</p>
<p><label>Name</label>
<input type="text" name="name[]" />
<label>email</label>
<input type="text" name="email[]" />
</p>
<p><label>Name</label>
<input type="text" name="name[]" />
<label>email</label>
<input type="text" name="email[]" />
</p>
<p><label>Name</label>
<input type="text" name="name[]" />
<label>email</label>
<input type="text" name="email[]" />
</p>
<input name="Submit" type="submit" />
</form>
这是我的process.php
<?php
// connect to the database
include('db.php');
$name = count($_POST['name']);
$email = count($_POST['email']);
if ($name > 0 && $name == $email) {
$insertArr = array();
for ($i=0; $i<$name; $i++) {
$insertArr[] = "('" . $_POST['name'][$i] . "', '" . $_POST['email'][$i] . "')";
}
}
$sql = "INSERT INTO comments(name, email) VALUES " . implode(',', $insertArr) ;
$qry = $conn->prepare ($sql);
$qry->execute($insertArr);
echo("<pre>\n");
print_r($_POST);
echo("</pre>\n");
?>
但是如果用户刚刚输入了4条记录,则第5行没有值,但是它们被保存到数据库中。我想检查用户是否有5个输入,如果没有,那么我必须忽略空并只保存有输入的字段。有谁知道如何做到这一点?我很乐意感谢你的帮助。谢谢。
答案 0 :(得分:1)
而不是这两行:
$name = count($_POST['name']);
$email = count($_POST['email']);
替换为:
$name=0;$email=0;
foreach($_POST['name'] as $post_name)
{
($post_name!=='')?$name++:''; //checks for empty values and increments accordingly
}
foreach($_POST['email'] as $post_email)
{
($post_email!=='')?$email++:'';
}
echo 'name counted = '.$name.'<br>';
echo 'email counted = '.$email.'<br>';
此外,我将此位添加到您的代码中:
if($_POST['name'][$i]!='' || $_POST['email'][$i]!='') //This checks if there is value in name or email of that index, if not it won't enter any blank.
{
$insertArr[] = "('" . $_POST['name'][$i] . "', '" . $_POST['email'][$i] . "')";
}
答案 1 :(得分:0)
您可以在名称数组上使用array_filter来删除所有空值。
$names = array_filter($_POST['name']);
$emails = $_POST['email'];
然后从两个数组中删除那些没有相应匹配的数组,这样只有在输入电子邮件和名称时才会插入。
$nr = 0;
foreach ($emails as $e) {
if (!$names[$nr] || empty($emails[$nr)) unset($emails[$nr]);
$nr++;
}
清理阵列键
$names = array_values($names);
$emails = array_values($emails);
$i = 0;
foreach ($names as $n) {
$insertArr[] = "('" . $n . "', '" . $emails[$i] . "')";
$i++;
}
答案 2 :(得分:0)
我看到你想要实现的目标,但如果将来总是5个帖子,你可以像这样解决它:
$name = $_POST['name0'];
$email = $_POST['email0'];
if ((!empty($name))&&(!empty($email))) {
$mysqli->query("YOUR_SQL_COMMAND");
echo '<your text>';
}
$name = $_POST['name1'];
$email = $_POST['email1'];
if ((!empty($name))&&(!empty($email))) {
$mysqli->query("YOUR_SQL_COMMAND");
echo '<your text>';
} // and so on
并将HTML更改为以下内容:
<form action="process.php" method="post">
<p><label>Name</label>
<input type="text" name="name0" />
<label>email</label>
<input type="text" name="email0" />
</p>
<p><label>Name</label>
<input type="text" name="name1" />
<label>email</label>
<input type="text" name="email1" />
</p>
<p><label>Name</label>
<input type="text" name="name2" />
<label>email</label>
<input type="text" name="email2" />
</p>
<p><label>Name</label>
<input type="text" name="name3" />
<label>email</label>
<input type="text" name="email3" />
</p>
<p><label>Name</label>
<input type="text" name="name4" />
<label>email</label>
<input type="text" name="email4" />
</p>
<input name="Submit" type="submit" />
</form>