有一個與我聲明的變量衝突。事實證明,我在我的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');
?>
您的頁面是否設置爲顯示錯誤? 'if($ password == NULL){'也將用戶發送到與頁面底部相同的位置。在if內部設置一對斷點,如'1'內部的'echo';退出;'看看它被抓到的地方。 –