开发者

PHP script issue

开发者 https://www.devze.com 2023-02-19 04:38 出处:网络
I cant get if ($register_user_name != $check_name){ to work, the nested if staement work properly though.

I cant get if ($register_user_name != $check_name){ to work, the nested if staement work properly though.

<?php
//al the defaul开发者_高级运维t 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 !";
        }
    }
    }
?>


You are doing existing username check wrong way. You have to make database to do this check, not your code.

$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;
}
?>


$register_user_name is a string, $check_name is boolean.

Also, by this line $check_name = in_array($register_user_name, $answer); you are checking thenever a $register_user_name is in array $answer. But $answer is not an array, it is something like an integer?


$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) {

you're comparing two variables that have nothing to do with each other.


Comparing a non empty string with a boolean will always return true as the string will be converted to TRUE.


in_array() function returns boolean value. So $check_name will be TRUE or FALSE, while $register_user_name seems to be a string.

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);

I dont understand how your code work at all :) $result should be an array('user_name' => 'some username'). Trying to get 'id' key must generate a warning. Putting non-array $answer variable to in_array() function isn't valid too...


You can't do this.. It is simply just not valid:

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

I would say, that you need to put () around it, like this:

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

Let me know if it solves your problem :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消