2011-10-30 87 views
0

求助:PHP的MYSQL連接與查詢內部IF語句

有一個與我聲明的變量衝突。事實證明,我在我的connect_database中聲明瞭一個$密碼,但是在我的帳戶腳本中也是這樣,這意味着$密碼總是被設置,因此總是跳過pas,直到最後...因爲這是正在進行的工作,所以它是相同的簡單密碼我的賬戶登錄...

ORIGINAL:

我有一個問題,詢問裏面如果PHP語句。我在做一個帳戶更新腳本。

我需要在頂部的連接到數據庫,然後根據從POST的結果,我在一些if語句做不同勢查詢。

如果它運行通過了所有的IF語句運行在最後的查詢。

如果它是由任何的,如果抓住了,進行查詢時,我希望腳本與消息重定向代碼,並與退出終止代碼。

的問題是,該腳本將不會退出,如果在執行後製成。它執行查詢,但它一直運行到最後 - 沒有重定向和退出...

我發現了一個解決方法,它需要在if語句內部需要數據庫,然後再在底部而不是僅在頂部,但我最初的想法是將它包含在頂部,並在if語句中使用連接,並在底部再次使用連接。

任何人都可以解釋爲什麼一個工程,另一個沒有? 這不是什麼大不了的事。我只是不明白爲什麼...

非常感謝

這不工作(需要數據庫IF語句外):

<?php 
session_start(); 
if(!isset($_SESSION["user"])) { 
    header("location: ../../../login/login_form.php"); 
    exit; 
} 

$user = mysql_real_escape_string($_POST['user']); 
$password = mysql_real_escape_string($_POST['password']); 
$email = mysql_real_escape_string($_POST['email']); 
$id = $_SESSION['user_id']; 

//Display if user and email is blank - try again--------------- 
if($user==NULL || $email==NULL){ 
    header('location: ../../../index.php?show=account&message=1'); 
    exit; 
} 

require_once('../../connect_database.php'); 

//Check if password is blank - meaning only updating user and email ----- 
if ($password==NULL){ 

    $query = "UPDATE user SET user='$user', email='$email' WHERE id=".$id; 
    mysql_query($query) or die(mysql_error()); 

    header('location: ../../../index.php?show=account&message=0'); 
    mysql_close(); 
    exit; 
} 

//Display if password less than 8 characers---------------- 
if(strlen($password)<8 && $password!=NULL){ 
    header('location: ../../../index.php?show=account&message=2'); 
      mysql_close(); 
    exit; 
} 

//Run this if everything is to be changed incl. password------- 

$query = "UPDATE user SET user='$user', password=md5('$password'), email='$email' WHERE id=".$id; 
mysql_query($query) or die(mysql_error()); 

mysql_close(); 

header('location: ../../../index.php?show=account&message=0'); 

?> 

此作品(內需要數據庫的IF聲明,然後再次在底部):

<?php 
session_start(); 
if(!isset($_SESSION["user"])) { 
    header("location: ../../../login/login_form.php"); 
    exit; 
} 

$user = mysql_real_escape_string($_POST['user']); 
$password = mysql_real_escape_string($_POST['password']); 
$email = mysql_real_escape_string($_POST['email']); 
$id = $_SESSION['user_id']; 

//Display if user and email is blank - try again--------- 
if($user==NULL || $email==NULL){ 
    header('location: ../../../index.php?show=account&message=1'); 
    exit; 
} 

//Check if password is blank - meaning only updating user and email ---- 
if ($password==NULL){ 

    require_once('../../connect_database.php'); 

    $query = "UPDATE user SET user='$user', email='$email' WHERE id=".$id; 
    mysql_query($query) or die(mysql_error()); 

    header('location: ../../../index.php?show=account&message=0'); 
    mysql_close(); 
    exit; 
} 

//Display if password less than 8 characers------------------ 
if(strlen($password)<8 && $password!=NULL){ 
    header('location: ../../../index.php?show=account&message=2'); 
    exit; 
} 

//Run this if everything is to be changed incl. password---------- 
require_once('../../connect_database.php'); 

$query = "UPDATE user SET user='$user', password=md5('$password'), email='$email' WHERE id=".$id; 
mysql_query($query) or die(mysql_error()); 

mysql_close(); 

header('location: ../../../index.php?show=account&message=0'); 

?> 
+0

您的頁面是否設置爲顯示錯誤? 'if($ password == NULL){'也將用戶發送到與頁面底部相同的位置。在if內部設置一對斷點,如'1'內部的'echo';退出;'看看它被抓到的地方。 –

回答

1

問題是腳本將不會退出後,如果執行。

你就錯了。 exit運營商很簡單,並且總是工作。
您如何知道查詢已執行?你有任何調試輸出?

THRE與你的代碼中的許多問題,但至少使其減少重複和雲紋一致

<?php 
session_start(); 
if(!isset($_SESSION["user"])) { 
    header("location: ../../../login/login_form.php"); 
    exit; 
} 

$user  = $_POST['user']; 
$password = $_POST['password']; 
$email = $_POST['email']; 
$id  = $_SESSION['user_id']; 

//Display if user and email is blank - try again--------------- 
if (!$user || !$email) { 
    $message=1; 
} 
//Display if password less than 8 characers---------------- 
elseif ($password && strlen($password)<8){ 
    $message=2; 
} else { 
    require_once('../../connect_database.php'); 
    $user = mysql_real_escape_string($user); 
    $email = mysql_real_escape_string($email); 

    if ($password) { 
     $password = "password='".md5($password)."',"; 
    } 
    $query = "UPDATE user SET user='$user',$password email='$email' WHERE id=".$id; 
    mysql_query($query) or trigger_error(mysql_error()); 
    $message = 0; 
} 
header("location: ../../../index.php?show=account&message=$message"); 

還要注意

  • 不能使用mysql_real_escape_string使用相對路徑連接
  • 之前(全這些點)認爲是不好的做法,特別是與地點
+0

嗨Col Shrapnel。我知道問題是什麼。我在聲明變量時存在衝突。在connect_database我聲明一個$密碼,但也在腳本中,因此它總是跳過密碼檢查的if ...我一直在想這個週末。我只是意識到這一點....但謝謝大家的建議! – Woozle

+0

您的代碼在這個旁邊有很多問題。 –

+0

你能告訴我什麼嗎?我是新來的。 – Woozle

-1

我相信e該位置區分大小寫。請嘗試更改到

header("Location:../../../index.php?show=account&message=0"); 
+0

它不區分大小寫。我以前看過所有的小寫字母。 –

+0

謝謝伊格爾。這似乎不是問題。我只是改變了它們,它仍然是一樣的。 – Woozle

-1

確定這裏是我的建議: 替換所有exitdie() 如果不行嘗試與此更換所有的位置標題:

<script> 
window.location='/path/to/your/redirection'; 
</script> 

讓我知道是否有任何幫助:)

+0

用die()替換exit有什麼意義? –

+0

與模具,而不是出口不好。我在所有其他頁面上都這樣做,沒有退出和重定向的前兆。只有這個地方在if裏面使用mysql連接... – Woozle

+0

第一條if語句有效。如果用戶和電子郵件是空白的,但一切低於要求連接不起作用... – Woozle