gpt4 book ai didi

PHP、MySQL 验证故障且搜索不起作用?

转载 作者:行者123 更新时间:2023-11-29 14:24:14 25 4
gpt4 key购买 nike

我创建了一个小的注册粘性表格。一切工作正常,但如果我输入任何错误的值,例如姓名中的数字、年龄中的字母甚至错误的电子邮件格式,那么数据仍然保存在数据库中,我无法找出验证问题。另外一个是搜索选项。每当我在搜索框中输入任何名字或姓氏时,它都应该显示数据库中的名称,否则会显示错误消息。任何 1 都可以建议我应该做什么。请参阅下面我的编码。

sticky_form代码...

<html>
<head>


<?php
global $fname,$lname,$gender,$age,$course,$email;

if(isset($_POST['register']))
{

$fname=$_POST['fname'];
$lname=$_POST['lname'];
$gender=$_POST['gender'];
$age=$_POST['age'];
$course=$_POST['course'];
$email=$_POST['email'];

if (preg_match("/[a-zA-Z ]+$/", $_POST['fname'])) {
$fname = trim($_POST['fname']);
}
else
{
echo '<p>The First name is empty or has illegal characters! To edit please go the link Display Data Information</p>';
//$error = true;
}


if (preg_match("/[a-zA-Z ]+$/", $_POST['lname'])) {
$lname = trim($_POST['lname']);
}
else
{
echo '<p>The last name is empty or has illegal characters! To edit please go the link Display Data Information</p>';
$error = true;
}

if(isset($_POST['gender']))
{
$gender = $_POST['gender'];
}
else
{
echo "<p>No gender found!</p>";
}

if (preg_match("/[a-zA-Z ]+$/", $_POST['age'])) {
$age = trim($_POST['age']);
}
else
{
echo '<p>Please enter age. Or your age contains illegal characters</p>';
//$error = true;
}

if(isset($_POST['course']))
{
$course = $_POST['course'];
}
else
{
echo "<p>Please Select Course!</p>";

}

// Validate the email:
if (preg_match("/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/", $_POST['email'] )){
$email = trim($_POST['email']);
}
else
{
echo '<p>The email is empty or has illegal characters! To edit please go the link Display Data Information</p>';
//$error = false;
}
echo "<br/>";
echo "<br/>";
echo "<br/>";



}

if($fname&&$lname&&$gender&&$age&&$email&&$course)
{
require_once('connection.php');
$query = mysql_query("INSERT INTO members SET FirstName='$fname', LastName='$lname', Gender='$gender', Age='$age', Email='$email', Course='$course'") or die(mysql_error());

if($query){
echo"Your Data Successfully Saved";
}
else
{
echo "Please recheck your Data!";
}


}


?>
</head>

<body id="body">
<h2><strong>Register Student Account</strong></h2>
<form action="student_form.php" method="post" >
<table border="1" id="container">


<tr>
<td>First Name</td>
<td>:</td>
<td><input type="text" name="fname" size="30" maxlength="50"/></td>
</tr>

<tr>
<td>Last Name</td>
<td>:</td>
<td><input type="text" name="lname" size="30" maxlength="50"/></td>
</tr>

<tr>
<td>Age</td>
<td>:</td>
<td><input type="text" name="age" size="3" /></td>
</tr>

<tr>
<td >Gender </td>
<td> : </td>
<td> Male
<input type="radio" name="gender" value="Male"/>

Female
<input type="radio" name="gender" value="Female"/></td>

</tr>


<tr>
<td valign="top">Course</td>
<td valign="top"> : </td>
<td> <input type="radio" name="course" value="Bachelor Of Computing"/>Bachelor Of Computing<br/>
<input type="radio" name="course" value="Bachelor Of Science"/>Bachelor Of Science<br/>
<input type="radio" name="course" value="Bachelor Of Software Engineering"/>Bachelor Of Software Engineering<br/>
<input type="radio" name="course" value="Bachelor Of Networking"/>Bachelor Of Networking<br/>
<input type="radio" name="course" value="Bacelor Of IT"/>Bacelor Of IT <br/>
<input type="radio" name="course" value="Bachelor Of Computer Science"/>Bachelor Of Computer Science<br/></td>

</tr>

<tr>
<td>Email Address</td>
<td>:</td>
<td><input type="text" name="email" size="30" maxlength="50"/></td>
</tr>


</table>
<input type="submit" name="register" value="REGISTER"/>

</form><br>
<p><a href="student_form.php" >Home</a></p>
<p><a href="display_data.php">Display Data Information</a>
<p><a href="search.php">To search for Members</a>
</body>
</html>

这是search_form代码......

<html>
<head>
<?php
//require_once('student_form.php');
if(isset($_POST['s1'])){
$id=$_REQUEST['id'];
$fname=$_POST['fname'];
//connect to the database
include('connection.php');
//-query the database table
$sql=mysql_query("SELECT * FROM members WHERE (FirstName LIKE '". $fname ."%' OR LastName LIKE '". $lname ."%'");
//-run the query against the mysql query function
$result=mysql_query($sql);

if($row=mysql_fetch_array($result)){
$fname=$row['FirstName'];
$lname=$row['LastName'];
/*$email=$row['Email'];
$age =$row['Age'];
$gender=$row['Gender'];
$course = $row['Course'];*/
}
//-display the result of the array
else
{
<?php echo $rows['FirstName']; ?>
<?php echo $rows['LastName']; ?>
}
}
?>
</head>
<body>
<form action="search.php" method="post">
<table>
<tr>
<td><strong>search box</strong></td>
<td><strong>:</strong></td>
<td><input type="text" name="search" value=""size="30"/><input type="submit" name="s1" value="Search"/></td>

</table>
</form>
</body>
</html>

最佳答案

$fname $lname $gender $age $email $course这样的变量被放入isset($_POST['register'])的if条件之后的if条件中。现在,即使您的验证有效,数据仍将输入数据库中。因为你已经设置了条件

if($fname&&$lname&&$gender&&$age&&$email&&$course)

现在,当所有这些变量中只有一个值时,控制将进入该 block 。一定会发生的情况是,您输入了错误的值,这些值正在验证,将显示消息,但是当第一个 if block 完成时,由于 $_POST 变量仍然具有某些值,无论它们是否无效,将进入第二个 if block 并且查询将被激发。

您可以做的是,无论您在哪里回显错误消息,都将相应的变量清空。像这样的东西:

if (preg_match("/[a-zA-Z ]+$/", $_POST['fname']))  {
$fname = trim($_POST['fname']);
}
else
{
echo '<p>The First name is empty or has illegal characters! To edit please go the link Display Data Information</p>';
$fname = "";
}

关于PHP、MySQL 验证故障且搜索不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11293712/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com