【发布时间】:2014-03-28 18:29:12
【问题描述】:
所以我需要有关必填表单字段的帮助。在填写 PHP 表单时,我希望 3 个字段(exam_id、subject、exam_date)成为必填字段。因此,当点击插入按钮时,如果某个字段留空并且将显示错误,或者除非填写每个字段,否则操作将无法完成。 我正在使用所有 php,没有 HTML,也没有,我不想将我的表单重做为调用 php 的 HTML,我想要这样。也没有安全问题,这只是一个简单的项目。 我的代码:
<?php
echo '<link rel="stylesheet" type="text/css" href="css/tables.css" />';
$con = mysql_connect("localhost","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("StudentExams", $con);
if (isset($_POST['update']))
{
$UpdateQuery = "UPDATE Exam SET exam_id='$_POST[exam_id]', subject='$_POST[subject]', exam_date='$_POST[exam_date]' WHERE exam_id='$_POST[hidden]'";
mysql_query($UpdateQuery, $con);
};
if (isset($_POST['delete']))
{
$DeleteQuery = "DELETE FROM Exam WHERE exam_id='$_POST[hidden]'";
mysql_query($DeleteQuery, $con);
};
if (isset($_POST['insert']))
{
$InsertQuery = "INSERT INTO Exam (exam_id, subject, exam_date) VALUES ('$_POST[uexam_id]','$_POST[usubject]','$_POST[uexam_date]')";
mysql_query($InsertQuery, $con);
};
$sql = "SELECT * FROM Exam";
$Data = mysql_query($sql,$con);
echo "<table id='size' border='1'>
<tr>
<th>Exam_ID</th>
<th>Subject</th>
<th>Exam_Date</th>
</tr>";
while($record = mysql_fetch_array($Data))
{
echo "<form action=examisud.php method=post>";
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>";
echo "<a href='ExamForm.html'> Back to main page </a>";
mysql_close($con);
?>
提前感谢任何可以帮助我的人!我觉得要么我缺少一个非常简单的解决方案,要么由于没有通用的 HTML 表单而非常复杂。
【问题讨论】:
-
您对 SQL 注入攻击完全开放,如果您还没有被黑客入侵,那么您将被黑客入侵。请使用准备好的/参数化查询来防止这种情况发生。另见:How can I prevent SQL injection in PHP?
-
我看到 很多 的 HTML。仅仅因为它包含在 PHP echo 语句中,not 就意味着你没有使用它
-
I'm using all php, no HTML你正在使用一堆 html 你只是让你的 php 出于某种原因回显它。 -
Amal,这是一个个人项目,它将保留在我自己的 wamp 服务器上。我的意思是我会在没有在 HTML 页面上输入表单的情况下将 HTML 包装在 PHP 中。我在这里查找了很多解决方案,所有解决方案都在 html 页面上填写了 html 表单,而我的 html 页面将我带到可以插入、更新和删除所有字段的 php 页面。
标签: php validation field required