rixlinux
rixlinux

Reputation: 199

$_GET['id']; if value wrong

I need to print a message if the $_GET[id] is not in database or do this code:

Header('Location:index.php');

Example: If the people enter in this URL: /index.php?id=100 if there is no page "100" do:

Header('Location:index.php');

Upvotes: 0

Views: 514

Answers (1)

jrn.ak
jrn.ak

Reputation: 36619

Roughly:

<?php
    $imageid = (isset($_GET['img_id']) && is_numeric($_GET['img_id'])) ? (int)$_GET['img_id'] : false;
    if ($imageid) {
        $sql = "SELECT * FROM images WHERE imageid='$imageid';";
        $result = mysql_query($sql);
        if ($result) {
            // imageid exists
            my_image_display_function($result);
        } else {
            // imageid does not exist
            header("Location: index.php");
        }
    }
?>

Update: Edited to more closely match OP's table/variable names.

Upvotes: 4

Related Questions