PHP MYSQL 连接与 IF 语句内的查询
已解决:
我的变量声明存在冲突。事实证明,我在我的 connect_database 中声明了一个 $password,而且在我的帐户脚本中声明了 $password,这意味着 $password 始终被设置,因此总是跳过 if 到最后......并且由于这是正在进行的工作,因此它是相同的简单密码作为我的帐户登录...
原文:
我在 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');
?>
SOLVED:
There is a conlict with my declaring of variables. It turns out i declare a $password in my connect_database, but also in my account script meaning the $password is always set and therefore always skips pas the if's to the end...and since this is work in progress it's the same simple password as my account login...
ORIGINAL:
I have a problem with queries inside if statements in PHP. I'm doing an account update script.
I require the connection to the database at the top and then depending on the result from the POST, I do diffrent queries in some if statements.
If it runs passed all IF statements it runs a query at the end.
If it's caught by any of the if's, a query is made and I want the script to redirect with a message code and terminate the code with an exit.
The problem is the script will not exit after an if execution is made. It does the query but it runs all the way to the end - no redirect and exit...
I found a workaround which requires the database to be required inside the if statements and then again at the bottom instead of only at the top, but my initial idea was to just include it at the top and use the connection in the if statement and again at the bottom.
Can anyone explain why one works and the other doesn't?
It's not a big deal. I just don't understand why...
Thanks a lot
This doesnt work (Require database outside of the IF statement):
<?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');
?>
This works (Require database inside of the IF statement and then again at the bottom):
<?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');
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您错了。
exit
运算符简单明了,并且始终有效。您如何知道查询已执行?你有任何调试输出吗?
您的代码有很多问题,但至少可以减少重复性和云纹一致性
另请注意,
you are wrong.
exit
operator is plain and simple and always work.How do you know the query was executed? Any debug output you've got?
Thre are many issues with your code but at least make it less repetitive and moire consistent
Also note
我相信该位置区分大小写。尝试将其更改为
I believe that location is case sensitive. Try changing it to
好的,这就是我的建议:
将所有
exit
替换为die()
如果这不起作用,请尝试用以下内容替换所有位置标头:
让我知道这是否有任何帮助:)
Ok here is what i suggest :
Replace all
exit
withdie()
And if that doesn't work try replacing all the Location headers with this :
Let me know if that helps in any way :)