php变量在IF语句中不可见

时间:2013-04-20 05:13:31

标签: php mysql post if-statement mysqli

我有两个步骤注册表格。从第一步开始,我将获取电子邮件和用户名,从第二步开始,我想获取名字和姓氏,并将其添加到数据库中。 但问题是,我从第一个POST形式得到的变量,$bridge_username确切地说,在下面的IF语句(从底部开始的第一个)中不可用。问题是它们在其他任何地方都可见,但不在这个特定的IF语句中。我已经尝试了一切,包括会话。我可以清楚地看到变量仍在那里(使用vardump或只是echo出来),到处都是,但不是我需要的地方...... 我很乐意听取你的意见。

$bridge_email = $_POST['email'];
$bridge_username = $_POST['username'];
$bridge_pass = $_POST['password'];
$bridge_pass_conf = $_POST['passconf'];
$bridge_terms = $_POST['terms'];
$bridge_pass_counted = strlen($bridge_pass);
$bridge_username_counted = strlen($bridge_username);
if (isset ($_POST['email']) AND isset ($_POST['password']) AND isset ($_POST['passconf']) AND isset ($_POST['username'])) { 

if ($bridge_email != '' AND $bridge_pass != '' AND $bridge_pass_conf != '' AND $bridge_username != '' AND $bridge_terms != '') {

if ($bridge_pass == $bridge_pass_conf) {

if ($bridge_pass_counted >= 33 OR $bridge_pass_counted <= 5) {
} else {

if ($bridge_username_counted >= 65 OR $bridge_username_counted <= 3) {
} else {

if (is_numeric(substr($bridge_username, 0, 1))) {
                } else {
//CHECK IF USERNAME OR EMAIL ALREADY EXIST  
$checkreguser = $mysqli->query("SELECT username FROM `engine_users` WHERE username = '$bridge_username' OR email = '$bridge_email' LIMIT 0, 1 ");
$checkreguser = $checkreguser->fetch_assoc();
if ($checkreguser == '') {
//CREATING A NEW USER 
$mysqli->query("INSERT INTO `users` (`id`, `username`, `password`, `email`, `fname`, `lname`, `company`, `address`, `city`, `state`, `zip`, `country`, `currency`, `phone`, `vat`, `userlevel`, `created`, `notes`, `lastlogin`, `lastip`, `active`) VALUES\n"
    . "(NULL, '$bridge_username', '1411678a0b9e25ee2f7c8b2f7ac92b6a74b3f9c5', '$bridge_email', '', '', NULL, '', '', '', '', '', '', '', NULL, 5, '2011-05-01 18:10:14', '', '2013-04-19 22:25:11', '127.0.0.1', 'y')");
}}}}}}}

$bridge_fname = $_POST['1_1_3'];
$bridge_lname = $_POST['1_1_4'];
if (isset ($_POST['1_1_3']) AND isset ($_POST['1_1_4'])) {
$mysqli->query("UPDATE `users` SET `fname` = '$bridge_fname',`lname` = '$bridge_lname' WHERE `users`.`username` = '$bridge_username'");
}

4 个答案:

答案 0 :(得分:0)

使用

if(isset($_POST['bridge_username']))

查看它是否存在。

您还可以使用ternary operator

$email = isset($_POST['bridge_username']) ? $_POST['bridge_username'] = false;

你们......“确切地说,$ bridge_username在下面的IF语句中不可用。”

如果您想要更详细的答案,请告诉我们确切的错误:)

答案 1 :(得分:0)

http://php.net/manual/en/function.isset.php

我会尝试将代码分解为简单的几行并测试if语句。更好地确定它在哪里破碎。也许在不同的步骤中添加一些echo语句或注释并逐步执行代码。以下示例。

$bridge_email = $_POST['email'];
$bridge_pass = $_POST['password'];

if (isset($_POST['email']) AND isset($_POST['password']){
   // EXECUTE AN ALERT
echo"email and pass are set";
}else {
echo"not passing";
}

答案 2 :(得分:0)

我修改了你的代码,让你成为一个很好的例子, 主要问题是如何构建查询字符串

..." username = '$bridge_username' "

这会产生一个像你看到的字符串 (在执行查询之前打印查询是很好的调试) 你必须改为:

." username = '".$bridge_username."' "

并且变量将替换为其值。

还添加了对帖子值的检查,因此如果未设置警告,则不会收到警告。

$bridge_email = (isset($_POST['email']) ? $_POST['email'] : null);
$bridge_username = (isset($_POST['username']) ? $_POST['username'] : null);
$bridge_pass = (isset($_POST['password']) ? $_POST['password'] : null);
$bridge_pass_conf = (isset($_POST['passconf']) ? $_POST['passconf'] : null);
$bridge_terms = (isset($_POST['terms']) ? $_POST['terms'] : null);
//$bridge_pass_counted = strlen($bridge_pass); 
//$bridge_username_counted = strlen($bridge_username);

//return early and stay back from chained IFs
if (!$bridge_email || !$bridge_username || !$bridge_pass || !$bridge_pass_conf) {
    return;
}

if ($bridge_pass != $bridge_pass_conf) {
    return;
}

if ($bridge_pass AND strlen($bridge_pass) > 5 AND strlen($bridge_pass) < 33) {
    return;
}

if ($bridge_username AND strlen($bridge_username) > 5 AND strlen($bridge_username) < 33) {
    return;
}

if (is_numeric(substr($bridge_username, 0, 1))) {
    return;
}

$result = $mysqli->query("SELECT username FROM `engine_users` WHERE username = '" . $bridge_username . "' OR email = '" . $bridge_email . "' LIMIT 0, 1 ");
$checkreguser = $result->fetch_assoc(); // returns associative array of strings  or NULL if there are no more rows 
//if ($checkreguser == '') {

if ($checkreguser === null) {
//CREATING A NEW USER 
    $mysqli->query("INSERT INTO `users` (`id`, `username`, `password`, `email`, `fname`, `lname`, `company`, `address`, `city`, `state`, `zip`, `country`, `currency`, `phone`, `vat`, `userlevel`, `created`, `notes`, `lastlogin`, `lastip`, `active`) VALUES\n"
            . "(NULL, '" . $bridge_username . "', '1411678a0b9e25ee2f7c8b2f7ac92b6a74b3f9c5', '" . $bridge_email . "', '', '', NULL, '', '', '', '', '', '', '', NULL, 5, '2011-05-01 18:10:14', '', '2013-04-19 22:25:11', '127.0.0.1', 'y')");
}

$bridge_fname =  (isset($_POST['1_1_3']) ? $_POST['1_1_3'] : null);
$bridge_lname =  (isset($_POST['1_1_4']) ? $_POST['1_1_4'] : null);
if ($bridge_fname AND $bridge_lname ) {
    $mysqli->query("UPDATE `users` SET `fname` = '" . $bridge_fname . "',`lname` = '" . $bridge_lname . "' WHERE `users`.`username` = '" . $bridge_username . "'");
}

请检查IF结构,尽早返回使代码更具可读性。

答案 3 :(得分:0)

您是否正在检查会话是否已启动。我看到你不断提到数据已传递给会话。可能想要设置它以确保它被处理。

尝试检查会话变量是否已创建,如果没有重定向则返回注册/登录页面。

作为示例...如果会话未注册,它将移动到不同的脚本或位置。

session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");

您可以通过回显会话上的isset上的内容来进一步测试会话以继续测试。同样,我会将您的代码分解为最基本的形式,以了解正在发生的事情。可能还需要查看先前的页面代码以查看发生的情况。

在网上发现了另一个可能帮助你的例子。 http://www.phpeasystep.com/phptu/6.html