php - PHP 中的检查不起作用

标签 php html mysql

我正在使用 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/

相关文章:

html - 将图像附加到标题标签之外

php - 为什么我要使用 PHP 将 NULL 值插入到 MySQL 表中?

php - 在 Laravel 4.2 中上传文件时使用 Input::all()

php - 重定向到新页面后 Laravel Auth session 丢失

javascript - AJAX 验证改进

mysql - 根据 group_concat 中 min、max 的差异查询获取项目

mysql - Capistrano "cap deploy:migrations"失败,因为它尝试运行所有迁移,而不仅仅是挂起的迁移

php - 在 Linux 上获取当前配置的网络接口(interface)信息的问题

javascript - 我如何创建动态表 - Crossfilter - dc.js

html - CSS 选择除每个表格的第一行之外的所有 HTML 元素