Jump to content

Checking if a mySQL row exists or not?


uramagget

Recommended Posts

I'm making a Content Management System, so I want the system to check if a page (?id=pagename here) exists or not. If it doesn't, return a 404 error. If not, serve the page. Also, I would like some help in stopping people from entering blank URLs (?id=_blank_stuff_here).

 

Here is what I'm using for the querystring:

 

 

<?
/*
Script Name: query.inc.php
Date: June 03, 2007
Description: ?id= URLs. Eww.....
Version: 0.1
Change Log:
Version 0.1 (Original Version):
*/


//Use GET to find query
$id=mysql_real_escape_string($_GET['id']);
//Use mySQL to select the query
$sql="SELECT * FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>

Link to comment
Share on other sites

<?
/*
Script Name: query.inc.php
Date: June 03, 2007
Description: ?id= URLs. Eww.....
Version: 0.1
Change Log:
Version 0.1 (Original Version):
*/


//Use GET to find query
if (isset($_GET['id']) && !empty(trim($_GET['id'])) {
    $id=mysql_real_escape_string($_GET['id']);
    //Use mySQL to select the query
    $sql="SELECT * FROM $tbl_name WHERE id='$id'";
    $result=mysql_query($sql);
    if (mysql_num_rows($result) > 0)
         $rows=mysql_fetch_array($result);
    else
         show_404();
}else {
     show_404();
}

function show_404() {
     header("404 PAGE NOT FOUND");
     include('404.html');
     die();
}
?>

 

Something like that?

Link to comment
Share on other sites

[code]

<?php

/*

Script Name: query.inc.php

Date: June 03, 2007

Description: ?id= URLs. Eww.....

Version: 0.1

Change Log:

Version 0.1 (Original Version):

*/

 

 

//Use GET to find query

$_GET['id'] = (isset($_GET['id'']))?trim($_GET['id']):'';

if (!empty($_GET['id'])) {

    $id=mysql_real_escape_string($_GET['id']);

    //Use mySQL to select the query

    $sql="SELECT * FROM $tbl_name WHERE id='$id'";

    $result=mysql_query($sql);

    if (mysql_num_rows($result) > 0)

        $rows=mysql_fetch_array($result);

    else

        show_404();

}else {

    show_404();

}

 

function show_404() {

    header("404 PAGE NOT FOUND");

    include('404.html');

    die();

}

?>

[/code]

 

I know it is the first if statement and is probably due to the empty www.php.net/empty  try the above if the same error report it back.

Link to comment
Share on other sites

<?php
/*
Script Name: query.inc.php
Date: June 03, 2007
Description: ?id= URLs. Eww.....
Version: 0.1
Change Log:
Version 0.1 (Original Version):
*/


//Use GET to find query
$_GET['id'] = (isset($_GET['id']))?trim($_GET['id']):'';

if (!empty($_GET['id'])) {
    $id=mysql_real_escape_string($_GET['id']);
    //Use mySQL to select the query
    $sql="SELECT * FROM $tbl_name WHERE id='$id'";
    $result=mysql_query($sql);
    if (mysql_num_rows($result) > 0)
         $rows=mysql_fetch_array($result);
    else
         show_404();
}else {
     show_404();
}

function show_404() {
     header("404 PAGE NOT FOUND");
     include('404.html');
     die();
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.