自从2天以来我一直在寻找和实践我想要实现的目标,但由于谷歌和其他来源无法解决我的问题,我在这里发布。我有一个表单,我提交数组并通过PHP发布它,将其插入到mysql数据库。
表格值
<tr class="alternate-1">
<td>1</td>
<td><input name="description[]" id="description-1" type="text" value="test1"></td>
<td><input name="amount[]" id="amount-1" type="text" value="500.00"></td>
<td><input name="paidby[]" id="paidby-1" type="text" value="">Company</td>
<td><input name="paidto[]" id="paidto-1" type="text" value="client"></td>
</tr>
<tr class="alternate-2">
<td>2</td>
<td><input name="description[]" id="description-2" type="text"></td>
<td><input name="amount[]" id="amount-2" type="text" value="400.00"></td>
<td><input name="paidby[]" id="paidby-2" type="text" value="">Company</td>
<td><input name="paidto[]" id="paidto-2" type="text" value=""></td>
</tr>
<tr class="alternate-3">
<td>3</td>
<td><input name="description[]" id="description-3" type="text" value=""></td>
<td><input name="amount[]" id="amount-3" type="text" value=""></td>
<td><input name="paidby[]" id="paidby-3" type="text" value=""></td>
<td><input name="paidto[]" id="paidto-3" type="text" value=""></td>
</tr>
<tr class="alternate-4">
<td>4</td>
<td><input name="description[]" id="description-4" type="text" value=""></td>
<td><input name="amount[]" id="amount-4" type="text" value=""></td>
<td><input name="paidby[]" id="paidby-4" type="text" value=""></td>
<td><input name="paidto[]" id="paidto-4" type="text" value=""></td>
</tr>
<tr class="alternate-5">
<td>5</td>
<td><input name="description[]" id="description-5" type="text" value=""></td>
<td><input name="amount[]" id="amount-5" type="text" value=""></td>
<td><input name="paidby[]" id="paidby-5" type="text" value=""></td>
<td><input name="paidto[]" id="paidto-5" type="text" value=""></td>
</tr>
如果您看到第一行包含所有必需值。在第二行,仅输入金额,并且未输入description
字段和paidto
字段。因此,我不想处理表单,应该抛出错误。
我的PHP代码就像这样
if($_POST['sform'] == "1")
{
foreach($_POST['description'] as $key => $desc)
{
if (!empty($_POST['description'][$key]) && !empty($_POST['amount'][$key]) || !empty($_POST['paidto'][$key]))
{
$query = 'INSERT INTO `expenses` (`amount`, `description`, `paidby`, `paidto`,) VALUES ('.$conn->qstr($_POST['amount'][$key]).', '.$conn->qstr($_POST['description'][$key]).', '.$conn->qstr($_POST['paidby'][$key]).', '.$conn->qstr($_POST['paidto'][$key]).')';
$results = $conn->Execute($query);
}
else
{
$error = "Form contain errors, please fix";
}
}
}
使用此代码,它会抛出错误并插入前2行数据。
因为缺少第二行description
和paidby
值而不应插入的位置。如何正确验证并仅将正确的数据插入数据库。
请帮助......
答案 0 :(得分:0)
对代码的评论太长,所以我会发布一个答案,希望它可以帮到你。我认为最快/最简单的方法是循环数据两次。一旦确认它不是空白,一旦实际插入数据......
$error = ''; foreach($_POST['description'] as $key => $desc){ if (empty($_POST['description'][$key]) || empty($_POST['amount'][$key]) || empty($_POST['paidto'][$key])) { $error = "Form contain errors, please fix"; break; // this stops it from going through every input. if one fails, they all fail } } if ($error != ''){ foreach($_POST['description'] as $key => $desc){ $query = 'INSERT INTO `expenses` (`amount`, `description`, `paidby`, `paidto`,) VALUES ('.$conn->qstr($_POST['amount'][$key]).', '.$conn->qstr($_POST['description'][$key]).', '.$conn->qstr($_POST['paidby'][$key]).', '.$conn->qstr($_POST['paidto'][$key]).')'; $results = $conn->Execute($query); } }