Home > Software design >  Values are not inserting into the table (this only happens when I include the $token variable so I a
Values are not inserting into the table (this only happens when I include the $token variable so I a

Time:02-10

Here is my index.php code:

class DB {
    private static function connection() {
        $servername = "localhost";
        $username = "root";
        $password = "";
        $dbname="social";

        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        return $conn;
    }
    public static function query($query, $params =array()) {
        $statement=self::connection()->prepare($query);
        $statement->execute($params);
        
        if (explode(' ', $query)[0] == 'SELECT') {
            $data = $statement->fetchAll();
            return $data;
        }
    }
}

And here is my actlogin.php code:

include('index.php');


if(isset($_POST['login'])) {
 $username = $_POST['username'];
 $password = $_POST['password'];

if (DB::query('SELECT username FROM users WHERE username=:username', array(':username'=>$username))){
    
    if (password_verify($password, DB::query('SELECT password FROM users WHERE username=:username', array(':username'=>$username))[0]['password'])){
        echo 'Logged in!';
        
        $cstrong = True;
        $token = bin2hex(openssl_random_pseudo_bytes(64, $cstrong));
        echo $token;
        $user_id = DB::query('SELECT id FROM users WHERE username=:username', array(':username'=>$username))[0]['id'];
        DB::query('INSERT INTO login_tokens (token, user_id) VALUES (:token, :user_id)', array(':token'=>$token, ':user_id'=>$user_id));
        
        
    }else{
        echo 'Incorrect password!';
    }
    
} else {
    echo 'User not found!';
}

}

I created a table in Sequel Pro called login_tokens with three columns: id, token, and user_id. Token has a unique key. I then made a relationship with the user_id column in the login_tokens table to the id column in the users table. I am following a tutorial and can't seem to figure out what I missed.

CodePudding user response:

DB::query() returns an array, so you're assigning an array to $user_id. You need to index the array to get the userid from the results. So you need to do

$rows = DB::query('SELECT id FROM users WHERE username=:username', array(':username'=>$username))[0]['id'];
if (!empty($rows)) {
    $user_id = $rows[0]['id'];
}

You could also just avoid the problem entirely by getting the userid in the INSERT query:

DB::query('INSERT INTO login_tokens (token, user_id) 
    SELECT :token, id
    FROM users
    WHERE username = :username', array(':token'=>$token, ':usename'=>$username));

CodePudding user response:

For selects is query method and for insert-modify is execute

$slq = 'INSERT INTO login_tokens (token, user_id) VALUES (:token, :user_id)';
$stmt = $pdo->prepare($slq);
$stmt->bindValue(':token', $token);
$stmt->bindValue(':user_id', $user_id);
$stmt->execute();
  • Related