所以,我有以下代码:(不要担心SQL注入/ MySQL的折旧现在)插入验证foreach循环麻烦
$required = array('uexam_id', 'usubject', 'uexam_date');
$error = false;
//VALIDATION: first check all required fields are not empty. if post has values
if(!empty($_POST))
foreach($required as $field)
if (empty($_POST[$field]))
$error = true;
//a field was empty, show error
if ($error) {
die ("All fields required!!! <a href='examisud.php'> Back to PHP Form </a>");
}
//no error - try the query
elseif($error === false && !empty($_POST))
{
$InsertQuery = "INSERT INTO Exam (exam_id, subject, exam_date) VALUES ('$_POST[uexam_id]','$_POST[usubject]','$_POST[uexam_date]')";
$result = mysql_query($InsertQuery, $con) or die('query Failure:'. mysql_error());
}
所以,当我浏览到这个PHP形式(examisud.php) ,我首先迎接“所需的所有领域”。那么我可以返回到形式,如果不填写了所有字段在它工作正常插入数据并显示错误信息。 * 我怎样才能获得“所需的所有字段”不要在表单页面加载,只有当显示我需要它(当一个字段留空时)。 当我也更新或删除字段时,我也会得到“所需的所有字段”错误显示。然而,除了弹出错误外,所有更新/删除都是正常的。
所以基本上我只需要它来显示当一个字段被留在插入查询空白! 在此先感谢您的帮助!
*编辑我的形式:
{
echo "<form action=examisud.php method=post>"; //HTML FORM ECHOED OUT BY PHP
echo "<tr>";
echo "<td>" . "<input type=text name=exam_id value=" . $record['exam_id'] . " </td>";
echo "<td>" . "<input type=text name=subject value=" . $record['subject'] . " </td>";
echo "<td>" . "<input type=text name=exam_date value=" . $record['exam_date'] . " </td>";
echo "<td>" . "<input type=hidden name=hidden value=" . $record['exam_id'] . " </td>";
echo "<td>" . "<input type=image name=update value=update id=submit src=images/update.png" . " </td>";
echo "<td>" . "<input type=image name=delete value=delete id=submit src=images/delete.png" . " </td>";
echo "</tr>";
echo "</form>";
}
echo "<form action=examisud.php method=post>";
echo "<tr>";
echo "<td><input type=text name=uexam_id></td>";
echo "<td><input type=text name=usubject></td>";
echo "<td><input type=text name=uexam_date></td>";
echo "<td>" . "<input type=image name=insert value=insert id=submit src=images/insert.png" . " </td>";
echo "</form>";
echo "</table>";
只需在表单上放置任何隐藏字段,并检查隐藏变量是否存在于您的代码中 - 如果存在,表单已提交,否则为“首次访问” –
'不要担心SQL注入/对now' MySQL的折旧:您的输入/ 谢谢您的好意:它不是我们谁担心,'你确实:) –