gpt4 book ai didi

php - PHP 中的检查不起作用

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

我正在使用 PHP 工作。我制作了一个名为 Donor.php 的表单并将其连接到数据库。现在我正在尝试在 PHP 中应用检查。但他们是一个问题。因为我已经在表单上应用了 PHP 中的空字段检查,但这些检查不起作用。请查看我的代码。因为我的工作就是因为这个问题而陷入困境。我的代码文件在这里:

捐赠者.php

<?php
//error_reporting(0);
if(isset($_POST['submit'])){

$first_name=$_POST['firstname'];
$last_name=$_POST['lastname'];
$Country=$_POST['country'];
$City=$_POST['city'];
$Gender=$_POST['gender'];
$Email=$_POST['email'];
$Password=$_POST['pwd'];

include_once "connectionn.php";
$emailChecker=mysql_real_escape_string($Email);
$sql_email_check=mysql_query("Select Email FROM user WHERE Email='$emailChecker'");
$email_check=mysql_num_rows($sql_email_check);


if((empty($first_name)) ||(empty($last_name)) ||(empty($City)) ||(empty($Gender)) ||(empty($Email)) ||(empty($Password))) {
$errorMsg='We are sorry, but there appears to be a problem with the form you submitted.';

if (empty($first_name)) {
$errorMsg.='$var is either 0, empty, or not set at all';
header('Location: Donor.php');
}
if(empty($last_name)){
$errorMsg.='lastname';
header('Location: Donor.php');
}
if(empty($City)){
$errorMsg.='City';
header('Location: Donor.php');
}
if(empty($Gender)){
$errorMsg.='Gender';
header('Location: Donor.php');
}
if(empty($Email)){
$errorMsg.='email';
header('Location: Donor.php');
}
if(empty($Password)){
$errorMsg.='Password';
echo "$errorMsg.";
header('Location: Donor.php');
}
}else if($email_check>0){
$errorMsg="invalid";
}else{
$sql="INSERT INTO user (User_ID,First_Name, Last_Name, gender, city, Email, Password) VALUES (NULL,'$first_name', '$last_name','$Gender','$City','$Email','$Password')";
$result=mysql_query($sql);


$UserID="SELECT max(User_ID) as usr from user";
$userIDResult=mysql_query($UserID);
if($userIDResult === false)
{
die(mysql_error());
}
while($R=mysql_fetch_array($userIDResult)){
$usrID= $R['usr'];

}
$donor="INSERT INTO donor(User_ID, Country)Values('".$usrID."','$Country')";
$resultdonor=mysql_query($donor);




mysql_close();
header('Location: DonorPro.php');

}
}

?>
<?php
include "Header.php";
//include "registration.php";
?>
<div class="DonorDiv">
<h1>Lets Join:</h1>
<form name="input" action="" method="post" <?php print"$errorMsg"; ?>>

First Name: <input type="text" name="firstname" placeholder="First Name" id="r">
<?php print "$first_name";
// if (!isset($_POST['firstname'])) {
//echo '$var is either 0, empty, or not set at all';
//}
?>


Last Name: <input type="text" name="lastname" placeholder="Last Name" id="u" <?php print "$last_name";?>> <br>
Institution: <input type="text" name="country" placeholder="Institution" id="" <?php print "$Institution";?>>
City: <input type="text" name="city" placeholder="City" id="" <?php print "$City";?>><br>
Country: <input type="text" name="country" placeholder="Country" id="" <?php print "$Country";?>><br>
Gender: <input type="text" name="gender" placeholder="Gender" id="" <?php print "$Gender";?>><br>
Email Address: <input type="Email" name="email" placeholder="Email" id="g" <?php print "$Email";?>><br>
Password:<input type="Password" name="pwd" placeholder="Password" id="v" <?php print"$Password";?>><br>

<input type="submit" src="images/button(9).png" alt="Submit" id="q">
</form>
</div>

<?php include "Footer.php"; ?>

最佳答案

PHP mysql 库已弃用,您应该考虑使用 myslqi 或 php PDO 代替。

Here is a tutorial

您还应该小心:$first_name 和其他变量,因为在显示表单时它们没有定义,因此您会收到警告。

无论如何,你的问题是这个检查总是错误的:

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

纠正这个问题的最简单(但不是最好)的方法是在表单中添加隐藏输入:

<input type="hidden" name="hidden">

关于php - PHP 中的检查不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22709329/

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