PHP MYSQL与IF语句内的查询连接

时间:2011-10-30 13:35:06

标签: php mysql if-statement database-connection

解决:

我对变量的声明造成了冲突。事实证明我在我的connect_database中声明了一个$密码,但在我的帐户脚本中也意味着$ password总是被设置,因此总是跳过if到底...并且由于这是正在进行的工作,所以它是相同的简单密码作为我的帐户登录...

ORIGINAL:

我在PHP中的if语句中遇到问题。我正在做一个帐户更新脚本。

我需要连接到顶部的数据库,然后根据POST的结果,我在一些if语句中执行不同的查询。

如果它运行通过所有IF语句,它最后会运行一个查询。

如果它被任何一个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');

?>

3 个答案:

答案 0 :(得分:1)

  

问题是在执行if后脚本不会退出。

你错了。 exit运算符简单明了,始终工作 你怎么知道查询被执行了?你有任何调试输出吗?

您的代码存在许多问题,但至少使其重复性降低并且符合云纹

<?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
  • 使用相对路径(所有这些点)被视为不良做法,尤其是位置

答案 1 :(得分:-1)

我认为该位置区分大小写。尝试将其更改为

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

答案 2 :(得分:-1)

好的,我建议: 将所有exit替换为die() 如果这不起作用,请尝试用以下内容替换所有Location标头:

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

让我知道这是否有任何帮助:)