douaonong7807 2014-10-16 05:12
浏览 42
已采纳

更新并从数据库表中选择

I have two database tables, "user" which have 3 columns (id,[auto increment] username and pinch) The other table is "pins" which has only one column (scratches) MY TABLES

USER
Id       username      Pinc


1         Josh             

2         Angela         

3          Chika 


PINS
scratches


123456


234515

124564

I want a situation when a user submit his password via a form, it will check in the pins table to know whether such data exist, if it does, it will update the pinc column of my user table with the form post and log in with it. If it doesn't exist in the pins table it will give an error "sorry the pin does not exist." MY CODE

$sql = "SELECT * FROM    pins WHERE scratches = '" .' $user_password '. "';";
$query = $this->db_connection->query($sql);
if ($query->num_rows== 0){
   $this->errors[] = "Sorry, that PIN does not exist.";
} elseif ($query->num_rows== 1) {  
    $sql = "UPDATE user ".
      "SET pinc = $user_password ".
      "WHERE user_name = $user_name" ;
    $query_new_user_insert = $this->db_connection->query($sql);

    $sql = "SELECT  user_name, pinc 
              FROM user
              WHERE user_name = '" . $user_name . "' ;";
    $result_of_login_check = $this->db_connection->query($sql);
    // if this user exists
    if ($result_of_login_check->num_rows == 1) {
        // get result row (as an object)
        $result_row = $result_of_login_check->fetch_object();
        $_SESSION['user_name'] = $result_row->user_name;
        $_SESSION['user_login_status'] = 1;
    } else {
       $this->errors[] = "Wrong password. Try again.";
    }
} else {
    $this->errors[] = "This user does not exist.";
}
} else {
   $this->errors[] = "Database connection problem.";
}
}
}

when i run the code, i get "sorry pin does not exist." Can someone tell me whats wrong with it?

  • 写回答

3条回答 默认 最新

  • douyouqian8550 2014-10-16 05:16
    关注

    You are using singe quete ''around $userpassword means, so You are passing as string Change your first query like this

    $sql = "SELECT * FROM    pins WHERE scratches = '".$user_password."';";
    

    And your second query as also wrong. as i am assuming $username is a string, You need to wrap it around single quete '' like this

    $sql = "UPDATE user ".
       "SET pinc = '$user_password' ".
       "WHERE user_name = '$user_name'" ;//You are missing single quete here if username is a string
    

    I dont know this is also problem or not, But have extra space in your third query. for your $username variable after contanitation (after point . . ). If that does not work remove it like this

    $sql = "SELECT  user_name, pinc 
                  FROM user
                  WHERE user_name = '".$user_name."' ;";
    

    **

    This is your full code should look like

    **

    $sql = "SELECT * FROM    pins WHERE scratches = '".$user_password ."';";
    $query = $this->db_connection->query($sql);
    if ($query->num_rows== 0){
       $this->errors[] = "Sorry, that PIN does not exist.";
    } elseif ($query->num_rows== 1) {  
        $sql = "UPDATE user SET pinc ='".$user_password."' WHERE user_name ='".$user_name."'" ;
        $query_new_user_insert = $this->db_connection->query($sql);
    
    
    
        $sql = "SELECT  user_name, pinc FROM user WHERE user_name = '".$user_name."' ;";
    
    
        $result_of_login_check = $this->db_connection->query($sql);
        // if this user exists
        if ($result_of_login_check->num_rows == 1) {
            // get result row (as an object)
            $result_row = $result_of_login_check->fetch_object();
            $_SESSION['user_name'] = $result_row->user_name;
            $_SESSION['user_login_status'] = 1;
        } else {
           $this->errors[] = "Wrong password. Try again.";
        }
    } else {
        $this->errors[] = "This user does not exist.";
    }
    } 
    }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分