如何插入连接的用户的数据库ID?

时间:2015-03-08 12:50:28

标签: php mysql

我正在尝试向数据库插入已连接的当前用户名的ID,如何从查询中获取用户名的id? 我试图将新行插入数据库,并需要连接的用户的ID。

<?php
     require 'connect.inc.php';
     require 'core.inc.php';
    ?>
    <?php

    $username = "";
    if(isset($_POST['username'])&&isset($_POST['password'])){
        $username = $_POST['username'];
        $password = $_POST['password'];
        if(!empty($username)&&!empty($password)){

        $query = "SELECT `id` FROM `auth_user` WHERE `username`='$username' AND `password`='$password'";
        echo $query;
        if($query_run = mysqli_query($connect, $query)){
            $query_num_rows = mysqli_num_rows( $query_run);

        if($query_num_rows==0){
            echo 'Invalid username/password combination.';
        }else if ($query_num_rows==1){
         $_SESSION['username']=$username; 
         $_SESSION['password']=$password;
         $_SESSION['id']=$query_num_rows;

         header('Location: main.php');

        }

        }

        }else{
            echo 'You must supply a username and password.';

        }

    }

    if(isset($_POST["Submit"])){
        if(isset($_POST["ch1"])){
        setcookie("unm",$_POST["username"],time()+3600);
        setcookie("pwd",$_POST["password"],time()+3600);
        }
        }
    ?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
    Username: <input type="text" name="username"><br><br>
    Password: <input type="password" name="password"><br><br>
    <input type="submit" value="Login"><br><br>
    <input type= "checkbox" name="ch1" value="ch" />Remember username and password <br><br>
    <a href="removename_password.php">Remove username and password</a><br><br>

2 个答案:

答案 0 :(得分:0)

如上所述,如果ID存储所选记录的ID:

if ( $row = mysql_fetch_assoc( $query_run )) {

    $userID = $row["ID"];

}

答案 1 :(得分:0)

根据我得到的结果: 假设您有一个名为session_logged_in的列,用于将当前日期和时间存储在unix时间戳中 $ query =&#34; SELECT id FROM auth_user WHERE username =&#39; $ username&#39;和password =&#39; $ password&#39; ORDER BY session_logged_in DESC LIMIT 0,1&#34 ;;