Why I can't set the vamsrid key in this code,is it a problem in the code.If it is,how can i set both drid and vamsrid key?

signinprocess.php

<?php
session_start();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include("configure.php");
if(isset($_POST["signin"]))
{
    $dID=$_POST["driverid"];
    $pas=$_POST["passwd"];
    $sql="SELECT * from driver WHERE driver_id=:dID || ID=:dID";
    $query=$con->prepare($sql);
    $query->bindParam(':dID',$dID,PDO::PARAM_STR);
    $query->execute();
    $numRows=$query->rowCount();
    if($numRows==1)
    {
        $row=$query->fetch(PDO::FETCH_ASSOC);
        foreach ($rows in $row){
        $_SESSION['drid']=$row->ID;
        }
        $_SESSION['vamsrid']=$row['driver_id'];
        if(password_verify($pas,$row['Password']))
        {
            $_SESSION["login_sess"]="1";
            $_SESSION["login_mail"]=$row['Gmail'];
            header("location:dashboard.php");
        }
        else
        {
            header("location:signin.php?loginerror=".$login);
        }
    }
    else
    {
        header("location:signin.php?loginerror=".$login);
    }
}
?>

You are only fetching one row so there is nothing to foreach over. You are also trying to loop over $rows (plural) which does not exist. You are trying to access $row as an object in one place ($row->ID) and as an array in others ($row['driver_id']).

Also, depending on the name of a button to be submitted in order for your code to work will completely fail in certain cases. You need to check the REQUEST METHOD instead. You need to kill the script after header redirects. Do not create variables for nothing.

Before it was like
$_SESSION[‘drid’]=$row[‘ID’];
$_SESSION[‘vamsrid’]=$row[‘driver_id’];

and the code was showing vamsrid did not exists?I don’t know why it wasn’t working

Start with simple and see if you get the results you expect.

$sql = "SELECT * FROM driver WHERE driver_id = ? OR ID = ?";
$query = $con->prepare($sql);
$query->execute([$_POST["driverid"], $_POST["driverid"]]);

$results = $query->fetchAll(PDO::FETCH_ASSOC);
var_dump($results);

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.