PHP脚本问题

时间:2011-03-24 07:43:37

标签: php mysql php-5.3

我无法使if ($register_user_name != $check_name){工作,嵌套的if语句可以正常工作。

<?php
//al the default code to connect to mysql
include("../sql_information.php");

class infos{

    //the var that will store my sql query
    private $db;

    function login_details($queryresult) {

        //this runs the imported script
        $this->db = new MySQLDatabase();
        $register_user_name = $_POST['register_user_name'];
        $register_password = $_POST['register_password'];
        $confirm_password = $_POST['confirm_password'];
        //This is my mysql search query
        $sql = "SELECT user_name FROM `data_manager`.`user_accounts`;";
        //the result of the query is stored in this var
        $queryresult = $this->db->query($sql);

        //I dont know what I did here
        if ($result = $this->db->fetchArray($queryresult)) {

        $answer = $result['id'];

        $check_name = in_array($register_user_name, $answer);

        if ($register_user_name != $check_name){
            if ($register_password == $confirm_password) {
                $sql = "INSERT INTO `data_manager`.`user_accounts` (`id`, `user_name`, `password`) VALUES (NULL, '$register_user_name', '$register_password');";
                mysql_query($sql);
                echo "Registration Complete !";
            } else {
                print "The password you entered does not match, please retry.";
            }
        } else {
            print "This username is already being used !";
        }
    }
    }
?>

6 个答案:

答案 0 :(得分:0)

$register_user_name是一个字符串,$check_name是布尔值。

此外,通过此行$check_name = in_array($register_user_name, $answer);,您正在检查数组$register_user_name中的$answer是否为{0}}。但$ answer不是一个数组,它就像一个整数?

答案 1 :(得分:0)

in_array()函数返回布尔值。因此$check_name将为TRUE或FALSE,而$register_user_name似乎是一个字符串。

UPD。

    //This is my mysql search query
    $sql = "SELECT user_name FROM `data_manager`.`user_accounts`;";
    //the result of the query is stored in this var
    $queryresult = $this->db->query($sql);

    // get result array
    if ($result = $this->db->fetchArray($queryresult)) {

    $answer = $result['id']; 

    $check_name = in_array($register_user_name, $answer);

我根本不明白你的代码是如何工作的; $result应该是array('user_name' => 'some username')。尝试获取“id”键必须生成警告。将非数组$answer变量放入in_array()函数也无效......

答案 2 :(得分:0)

$register_user_name=$_POST['register_user_name']; // $register_user_name now contains a string
[..]
$check_name = in_array($register_user_name, $answer); //$check_name now contains a bool
if ($register_user_name != $check_name){ // if (string_thingy != bool_thingy) {

你要比较两个彼此无关的变量。

答案 3 :(得分:0)

将非空字符串与布尔值进行比较将始终返回true,因为字符串将转换为TRUE。

答案 4 :(得分:0)

您正在以错误的方式执行现有的用户名检查。您必须使数据库执行此检查,而不是您的代码。

$errors = array();
$register_password = $_POST['register_password'];
$confirm_password = $_POST['confirm_password'];
//escape string going to the query
$register_user_name = mysql_real_escape_string($_POST['register_user_name']);

$sql = "SELECT 1 FROM data_manager.user_accounts WHERE user_name='$register_user_name'";
$queryresult = $this->db->query($sql);
if ($this->db->fetchArray($queryresult)) {
  $errors[] = "Username already taken";
}
if ($register_password !== $confirm_password) {
  $errors[] = "Passwords mismatch";
}
if (!$errors) {
  $register_password = mysql_real_escape_string($register_password);
  $sql = "INSERT INTO `data_manager`.`user_accounts` (`id`, `user_name`, `password`) 
                          VALUES (NULL, '$register_user_name', '$register_password')";
  mysql_query($sql);
  header("Location: cpanel.php");
  exit;
} else {
  foreach ($errors as $err) echo $err;
}
?>

答案 5 :(得分:-3)

你不能这样做......它只是无效:

if ($result = $this->db->fetchArray($queryresult)) {

我想说,你需要把它放在它周围,就像这样:

if ( ($result = $this->db->fetchArray($queryresult)) ) {

如果它能解决您的问题,请告诉我: - )