Radu033 Radu033 - 3 months ago 10
MySQL Question

Display a message if column doesn't exist?

I'm building an APP, and it creates some urls based on id.

For example the edit item link opens this kind of url website.com/update_item.php?uid=1

If I change the id in url with an item id that was not created by me and I don't have access to edit it, it will display the following message:
"You don't have access to edit this item!"

The question is, how can I display the same message to id's that do not exist in the database? How can I check either an id is or is not registered in the database?

Currently if I type in an id that does not exist it displays a blank page.

Here is the code

$list_id = $_GET["uid"];
$l_title = "SELECT * FROM list_items WHERE `id`='$list_id'" ;
$query = $pdo->prepare($l_title);
$query->execute();
$list = $query ->fetchAll();


The if code

if ($_SESSION['FBID'] == $item['user_id']) {
....


Where
$_SESSION['FBID'
] is the logged in with facebook user and the
$item['user_id']
is the id of the user who created the list.

Answer

Check the query results, if result is empty, display message

$list_id = $_GET["uid"];
$l_title = "SELECT * FROM list_items WHERE `id`='$list_id'" ;
$query = $pdo->prepare($l_title);
$query->execute();
$list = $query ->fetchAll();
if(!$list || empty($list)){
   echo "Your message";
}

The if code

if ($_SESSION['FBID'] == $item['user_id']) { 
.................
Comments