I have 2 columns under sales table in SQL as below
id | itemid |
---|---|
1 | 31 |
1 | 32 |
1 | 33 |
1 | 34 |
I need through the PHP to check by the id, if the $_SESSION['id'] has any item number in the second column then tell the user that the item already exists, otherwise add this item to DB .. I did do fat the below code but it doesn't work, it will print both conditions if statement
<?php
session_start();
include "connect.php";
$access = isset($_SESSION["userid"]);
if ($access) {
$itemid = filter_input(INPUT_GET, "itemid", FILTER_VALIDATE_INT);
if($itemid !== null && $itemid !== false){
$SQL = $dbh->prepare("SELECT * FROM sales where userid = ?");
if($SQL->execute([$_SESSION["userid"]])){
if ($SQL->rowCount() > 0){
while($row = $SQL->fetch()){
if((int)$row['itemid'] === $itemid){
echo "item is exist";
}else{
echo "add it to the table";
}
}
}
}
}
}