2014-03-28 24 views
0

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

Donor.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"; ?> 
+5

缩小你的问题告诉我们你卡在哪里? –

+0

不要嵌套if语句。 – user986959

+0

@DholakiyaAnkit如果我按下提交按钮没有填充任何领域,它不显示任何错误按摩。但它也不会移向其他条件,因为它们是INSERT查询。但是这些值不会进入数据库。该怎么办?? “#: – user3432180

回答

1

的PHP编写的MySQL库已过时,应考虑使用myslqi或PHP PDO来代替。

Here is a tutorial

你也应该小心:$first_name和当您显示形式,所以你会得到警告,他们是没有定义的其他变量。

无论如何,你的问题是,这种检查是始终为false:

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

最简单的(但不是最好的)的方式来纠正在表单中添加一个隐藏输入:

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

或者只是提交一个名称和值,这将是真实的。 – Anthony

1

你必须告诉浏览器重定向到另一个页面后退出PHP脚本:

header('Location: Donor.php'); 
exit; 

(除SQL注入和其他一些问题。)