我试图在此表单中添加自定义验证消息,但无法弄清楚我的生活.. 任何帮助/建议表示赞赏......谢谢。
我已经尝试了一些教程,但似乎没有任何东西可以点击给我,我确信它很简单,但我似乎无法弄明白。
由于
验证表格:
<?php
if (isset($_POST['insert'])) {
require_once('connection.php');
$OK = false;
$sql = 'INSERT INTO students (studentTitle, studentFirstName, studentLastName)
VALUES(:studentTitle, :studentFirstName, :studentLastName)';
$stmt = $conn->prepare($sql);
$stmt->bindParam(':studentTitle', $_POST['studentTitle'], PDO::PARAM_STR);
$stmt->bindParam(':studentFirstName', $_POST['studentFirstName'], PDO::PARAM_STR);
$stmt->bindParam(':studentLastName', $_POST['studentLastName'], PDO::PARAM_STR);
$stmt->execute();
$OK = $stmt->rowCount();
if ($OK) {
header('Location: http://localhost/mysqlquiz/student.php');
exit;
} else {
$error = $stmt->errorInfo();
if (isset($error[2])) {
$error = $error[2];
}
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Add Student Details</title>
<link href="css/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1 class="header">New student details</h1>
<p><a href="student.php">Student Listing </a></p>
<?php
if (isset($error)) {
echo "<p class='warning'>Error: $error</p>";
}
?>
<?php
// define variables and set to empty values
$studentTitleErr = $studentFirstNameErr = $studentLastNameErr = "";
$studentTitle = $studentFirstName = $studentLastName = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["studentTitle"])) {
$studentTitleErr = "A title is required";
} else {
$studentTitle = test_input($_POST["studentTitle"]);
}
if (empty($_POST["studentFirstName"])) {
$studentFirstNameErr = "First name is required";
} else {
$studentFirstName = test_input($_POST["studentFirstName"]);
}
if (empty($_POST["studentLastName"])) {
$studentLastNameErr = "Last name is required";
} else {
$studentLastName = test_input($_POST["studentLastName"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<p>
<label for="studentTitle">Title:</label>
<select name="studentTitle" id="studentTitle" ><span class="error">* <?php echo $studentTitleErr;?></span>
<option value="Mr">Mr.</option>
<option value="Mrs">Mrs.</option>
<option value="Ms">Ms.</option>
<option value="Miss">Miss.</option>
</select>
</p>
<p>
<label for="studentFirstName">First Name:</label>
<input type="text" name="studentFirstName" id="studentFirstName" ><span class="error">* <?php echo $studentFirstNameErr;?></span>
</p>
<p>
<label for="studentLastName">Last Name:</label>
<input type="text" name="studentLastName" id="studentLastName" ><span class="error">* <?php echo $studentLastNameErr;?></span>
</p>
<p>
<input type="submit" name="insert" value="Add Details" id="insert">
<input type="reset" name="clear" value="Clear" id="clear">
<input name="studentID" type="hidden" value="<?php echo $studentID; ?>">
</p>
</form>
</body>
</html>
答案 0 :(得分:0)
因为您的数据库插入操作首先运行,并且没有检查所需的字段数据是否运行。因此,它运行,并在成功时重定向并退出:
header('Location: http://localhost/mysqlquiz/student.php');
exit;
清除POST
并且您的必填字段逻辑永远不会运行。
答案 1 :(得分:0)
我已更新您的代码。你应该先检查验证然后插入。
<?php
// define variables and set to empty values
$studentTitleErr = $studentFirstNameErr = $studentLastNameErr = "";
$studentTitle = $studentFirstName = $studentLastName = "";
if (isset($_POST['insert'])) {
require_once('connection.php');
$error = $OK = false;
if (empty($_POST["studentTitle"])) {
$studentTitleErr = "A title is required";
$error = true;
} else {
$studentTitle = test_input($_POST["studentTitle"]);
}
if (empty($_POST["studentFirstName"])) {
$studentFirstNameErr = "First name is required";
$error = true;
} else {
$studentFirstName = test_input($_POST["studentFirstName"]);
}
if (empty($_POST["studentLastName"])) {
$studentLastNameErr = "Last name is required";
$error = true;
} else {
$studentLastName = test_input($_POST["studentLastName"]);
}
if($error == false)
{
$sql = 'INSERT INTO students (studentTitle, studentFirstName, studentLastName)
VALUES(:studentTitle, :studentFirstName, :studentLastName)';
$stmt = $conn->prepare($sql);
$stmt->bindParam(':studentTitle', $_POST['studentTitle'], PDO::PARAM_STR);
$stmt->bindParam(':studentFirstName', $_POST['studentFirstName'], PDO::PARAM_STR);
$stmt->bindParam(':studentLastName', $_POST['studentLastName'], PDO::PARAM_STR);
$stmt->execute();
$OK = $stmt->rowCount();
if ($OK) {
header('Location: http://localhost/mysqlquiz/student.php');
exit;
} else {
$error = $stmt->errorInfo();
if (isset($error[2])) {
$error = $error[2];
}
}
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Add Student Details</title>
<link href="css/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1 class="header">New student details</h1>
<p><a href="student.php">Student Listing </a></p>
<?php
if (isset($error)) {
echo "<p class='warning'>Error: $error</p>";
}
?>
<?php
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<p>
<label for="studentTitle">Title:</label>
<select name="studentTitle" id="studentTitle" ><span class="error">* <?php echo $studentTitleErr;?></span>
<option value="Mr">Mr.</option>
<option value="Mrs">Mrs.</option>
<option value="Ms">Ms.</option>
<option value="Miss">Miss.</option>
</select>
</p>
<p>
<label for="studentFirstName">First Name:</label>
<input type="text" name="studentFirstName" id="studentFirstName" ><span class="error">* <?php echo $studentFirstNameErr;?></span>
</p>
<p>
<label for="studentLastName">Last Name:</label>
<input type="text" name="studentLastName" id="studentLastName" ><span class="error">* <?php echo $studentLastNameErr;?></span>
</p>
<p>
<input type="submit" name="insert" value="Add Details" id="insert">
<input type="reset" name="clear" value="Clear" id="clear">
<input name="studentID" type="hidden" value="<?php echo $studentID; ?>">
</p>
</form>
</body>
</html>