简体   繁体   English

PHP必填字段(无HTML)

[英]PHP Required fields (No HTML)

So I need help with a required form field. 因此,我需要有关必填字段的帮助。 I want the 3 fields (exam_id, subject, exam_date) to be required fields when filling out the PHP form. 填写PHP表单时,我希望3个字段(exam_id,subject,exam_date)是必填字段。 So when the insert button is hit, if a field is left blank and error will display or the action won't complete unless every field is filled in. I'm using all php, no HTML and no, I don't want to redo my form as HTML calling the php, I want it like this. 因此,当单击插入按钮时,如果将一个字段留空并且将显示错误,否则除非完成每个字段的填写,否则操作将无法完成。我正在使用所有php,没有HTML,也没有,我不想重做我的表单作为HTML调用php,我想要这样。 There's no security problems either, this is just a simple project. 也没有安全问题,这只是一个简单的项目。 My code: 我的代码:

<?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);

?>

Thanks in advance for anyone who can help me out! 预先感谢任何可以帮助我的人! I feel there is either a very simple solution i'm missing or it's very convoluted due to the absence of a generic HTML form. 我觉得我缺少一个非常简单的解决方案,或者由于缺少通用HTML表单而感到非常困惑。

Just do a check on the top with isset 只需在isset的顶部进行检查

if(!isset($_POST['exam_id'],$_POST['subject'],$_POST['exam_date']))
{
 echo "These fields are required ! Please fill it up";
 header("location:backtoform.php");exit;
}

Warning : Since you are passing the $_POST parameters directly onto your query, you are prone to SQL Injection attacks. 警告 :由于将$_POST参数直接传递到查询中,因此容易受到SQL注入攻击。


This( mysql_* ) extension is deprecated as of PHP 5.5.0 , and will be removed in the future. PHP 5.5.0不推荐使用此( mysql_* )扩展名,以后将删除该扩展名。 Instead, the MySQLi or PDO_MySQL extension should be used. 相反,应使用MySQLiPDO_MySQL扩展。 Switching to PreparedStatements is even more better to ward off SQL Injection attacks ! 改用PreparedStatements更好地抵御SQL注入攻击!

isset( ... ) is only used to see if a field is set or not. isset( ... )仅用于查看是否设置了字段。 It doesn't care if the value of the field is empty. 不管该字段的值是否为空。

You need to use empty( .. ) instead. 您需要改用empty( .. ) It would return true only if the field was ever set and is not empty. 仅当字段已设置且不为空时,它才会返回true。 Two apples with one shot. 与一个射击的两个苹果。

Across all your if statements, use !empty. 在所有if语句中,使用!empty。 Maintain an $error variable and initialize it to FALSE. 维护一个$error变量并将其初始化为FALSE。 Whenever an error occurs, set $error to TRUE. 每当发生错误时,请将$ error设置为TRUE。 In the end, perform the required operation only when $error == FALSE . 最后,仅在$error == FALSE时执行所需的操作。

$error = false;
    if (  !empty($_POST['update']   ){
    // stuff
    }
    else {
    // display error message
    $error=true;
    }

if(!$error){
// operations
}

This way you can neatly separate validations from operations. 这样,您可以将验证与操作巧妙地分开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM