2014-03-28 60 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注入和其他一些問題。)