2011-10-30 18 views
0

解決済み:PHP MYSQLとIF文内のクエリ

私の変数の宣言には矛盾があります。それは私のconnect_database内の$パスワードを宣言することがわかりますが、私のアカウントのスクリプトでも$パスワードは常に設定されているため、常に最後にpasをスキップすることを意味します...そして、これは進行中の作業なので、アカウントのログイン...

ORIGINALとして:

私はPHPでのif文の内側のクエリに問題があります。私はアカウントの更新スクリプトをやっています。

私はデータベースのトップへの接続が必要で、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'); 

?> 

:私はちょうど...なぜ

どうもありがとう

このdoesntの仕事を(は、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; 
} 

//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){'はユーザをページの一番下と同じ場所にも送ります。あなたの内側に 'echo 'inside if 1'のようないくつかのブレークポイントを設定します。出口;それが捕らえられるところを見てください。 –

答えて

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

    こんにちは。私は問題が何であるか知っています。変数の宣言には矛盾があります。 connect_databaseでは$パスワードを宣言しますが、スクリプト内でも同様にパスワードチェックを使ってifをスキップします...私はこれを週末を通して考えています。私はちょうどそれを実現した....しかし、あなたのすべての提案に感謝! – Woozle

    +0

    このコードの横には多くの問題があります。 –

    +0

    あなたは何を教えてくれますか?私はこれで新しいです。 – Woozle

    -1

    私は信じていますその場所は大文字と小文字が区別されます。ここで[OK]を

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

    大文字と小文字は区別されません。私は前に何度も使用されている小文字をすべて見てきました。 –

    +0

    ありがとうイーグル。それは問題ではないようです。私はちょうどそれらを変更し、それはまだ同じです。 – Woozle

    -1

    にそれを変更してみてください、私がお勧めです: はdie() ですべてexitを交換し、問題が解決しない場合は、これですべての場所のヘッダを交換してください:

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

    は私をしてみましょうどのように役立つかを知ってください:)

    +0

    exitをdie()で置き換えるのは何ですか? –

    +0

    出口ではなくダイでいいです。私は他のすべてのページでこれを行い、退出とリダイレクトの前触れはありません。 if内部でmysql接続が使用されているのはこれだけです... – Woozle

    +0

    最初のif文が機能します。 1つは、ユーザーと電子メールが空白ですが、必要な接続の下にあるすべてが機能していない場合... – Woozle