tags:

views:

52

answers:

3

Hello,

How do i validate $_GET thats the number coming from correct source.

My url look like : index.php?page=items&catID=5

When users put something like 3 which is doesn't exist on catID. I want it to display error message.

$catID = intval($_GET["catID"]);

if($catID) {
    $checkSQL = mysql_query("SELECT * FROM category WHERE category_type='2'");
    while($checkROW = mysql_fetch_array($checkSQL)) {
    $checkCAT != $checkROW["categoryID"];
    echo "err msg";
    }

This i can come up so far but it doesn't working as it fire error msg even in correct page.

Thank you

A: 

Oh, I see. The first line inside the while loop should have an "if":

    while ($checkROW = mysql_fetch_array($checkSQL)) {
        if ($checkCAT != $checkROW["categoryID"])
            echo "err msg";
wallyk
I was wrong pasted the code. There was IF in my original code. However it isn't working.
A: 

It looks like you'll be wanting to use mysql_fetch_assoc(), rather than mysql_fetch_array().

Chris
A: 

wallk makes a good point, there is a missing if. but if i read this correctly, wouldn't something along the lines of this be more what you are going for? Right now the line:

if($catID) {

is actually only checking if catID (or, catID from the $_GET) is non-zero (not false). My guess if you are looking to check if catID is the categoryID returned from SQL?

$catID = intval($_GET["catID"]);

checkcat($catID);

function checkcat($check_category) {
    $checkSQL = mysql_query("SELECT * FROM category WHERE category_type='2'");
    while($checkROW = mysql_fetch_array($checkSQL)) {
        if ( $check_category != $checkROW["categoryID"] ) {
            echo "err msg";
        } else { 
            echo "not an error message";
        }
    }
}

Expounding on what you are looking for, how about something like this then?

$catID = ($_GET["catID");

if ( !is_numeric($catID) ) {
    echo "Not a numeric category!"
} else {
    $checkSQLQuery = "SELECT * FROM category WHERE categoryID = '{$catID}' AND category_type='2'"
    $resultSQL = mysql_query($checkSQLQuery, $db);
    /* NOTE!:  Guessing on what your database resouce 
    pointer is - it isn't included in the origin snippet.
    Although, the last opened should be used by default if 
    this is left out. */
    if ( mysql_num_rows($resultSQL) < 1 ) {
        echo "Error message, category ID not found" 
    } else {
        echo "Found it!"
    }
}
J.T.Sage
this produce error message "err msg" on correct catID too. It shouldn't display error message.and the error message produced thrice in non correct pages.
can you run that SQL query on it's own and post the output? perhaps there is something we are missing there. does it return multiple items?
J.T.Sage
Updated my example to use sql to check rather than the PHP
J.T.Sage
Slick work. Thanks.
No problem. enjoy.
J.T.Sage