/    Sign up×
Community /Pin to ProfileBookmark

PHP question concerning redirection under specific conditions

Hi I have a .php page called: index.php?id=101
(where the ?id=101 is an example)

The page then GETs the id and pulls the information from a row in a database where the ?id= is equalled to the id in the id column in the database.

Whats an easy way to redirect the page if someone fills in a ?id= that is NOT in the id column in the database?

Thanks already

to post a comment
PHP

23 Comments(s)

Copy linkTweet thisAlerts:
@saeMay 29.2007 — when you do your SELECT query from the database you can specify what to do if an error occurs (i.e. ID not found)
Copy linkTweet thisAlerts:
@spliffauthorMay 29.2007 — can anyone give me an example script for the answer above?
Copy linkTweet thisAlerts:
@spliffauthorMay 29.2007 — i tried

[code=php] or die (header("www.bla.com") ) ;[/code]

this didnt work.

please help
Copy linkTweet thisAlerts:
@MrCoderMay 29.2007 — [code=php]
$result = mysql_query("SELECT..................");

if(mysql_num_rows($result) === 0)
{
header("Location: http://www.blahblah.com");
die();
} else {
/*
Other code
*/
}
[/code]
Copy linkTweet thisAlerts:
@spliffauthorMay 29.2007 — [code=php]
$result = mysql_query("SELECT..................");

if(mysql_num_rows($result) === 0)
{
header("Location: http://www.blahblah.com");
die();
} else {
/*
Other code
*/
}
[/code]
[/QUOTE]




i tried this:

[code=php]<?php

mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());

$id=$_GET['id'];

$result = mysql_query("SELECT * FROM table WHERE id='$id'") or die(mysql_error());

$row = mysql_fetch_array( $result );

if(mysql_num_rows($result) === 0)
{
header("Location: http://www.blahblah.com");
die();
} else {

echo $row['example'];

}




?>[/code]


but it renders a blank page if i enter an id which is not in the database.

please help more ?
Copy linkTweet thisAlerts:
@MrCoderMay 29.2007 — [code=php]
<?php
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());

$id = (int)$_GET['id']; // Typecast this to avoid SQL injection

$result = mysql_query("SELECT * FROM table WHERE id='".$id."'") or die(mysql_error());

if(mysql_num_rows($result) === 0)
{
header("Location: http://www.blahblah.com");
die();
} else {
$row = mysql_fetch_array( $result ); // This line was causing the error since there was no row to fetch.

echo $row['example']; // Do you mean to use mysql_fetch_assoc() above?
}
?>
[/code]
Copy linkTweet thisAlerts:
@spliffauthorMay 29.2007 — I am not a PHP bigshot so I have no idea what the difference is between mysql_fetch_array and mysql_fetch_assoc

i will try the above, thanks
Copy linkTweet thisAlerts:
@spliffauthorMay 29.2007 — [code=php]
<?php
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());

$id = (int)$_GET['id']; // Typecast this to avoid SQL injection

$result = mysql_query("SELECT * FROM table WHERE id='".$id."'") or die(mysql_error());

if(mysql_num_rows($result) === 0)
{
header("Location: http://www.blahblah.com");
die();
} else {
$row = mysql_fetch_array( $result ); // This line was causing the error since there was no row to fetch.

echo $row['example']; // Do you mean to use mysql_fetch_assoc() above?
}
?>
[/code]
[/QUOTE]



this still gives a blank page when the id is not in the database instead of referring to another page...
Copy linkTweet thisAlerts:
@saeMay 29.2007 — does it work when you use an ID that does exist? The reason I ask is because you have single and double quotes around your $id in the query. So it's looking for "5" (i.e. a string) and not 5 (an int) and why the triple ===? should only be == ??
Copy linkTweet thisAlerts:
@svidgenMay 29.2007 — [code=php]
$row = mysql_fetch_array( $result ); // This line was causing the error since there was no row to fetch.

echo $row['example']; // Do you mean to use mysql_fetch_assoc() above?
[/code]
[/QUOTE]


It should indeed be mysql_fetch_assoc().

[B]mysql_fetch_array() [/B]will return the row as an array with integer indexes, either in the order the fields appear in the database (if you're using *) or the order in which you name the fields (please correct me if I'm wrong on that point).

[B]mysql_fetch_assoc() [/B]will return the row as an associative array, wherein each value is referenced by the column name or the name given to the column in the query.

Hope that helps.
Copy linkTweet thisAlerts:
@spliffauthorMay 29.2007 — does it work when you use an ID that does exist? The reason I ask is because you have single and double quotes around your $id in the query. So it's looking for "5" (i.e. a string) and not 5 (an int) and why the triple ===? should only be == ??[/QUOTE]

I have no superior PHP knowledge, i used the code that Mr Coder suggested. It DID work when I used an ID that existed...
Copy linkTweet thisAlerts:
@SheldonMay 30.2007 — [code=php]<?php
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());

$redirect = "http://www.blahblah.com";

if(!empty($_GET['id']) and is_numeric($_GET['id'])){ // Typecast this to avoid SQL injection
id = $_GET['id'];
$result = mysql_query("SELECT * FROM table WHERE id='{$id}'") or die(mysql_error());


if(mysql_num_rows($result) == 0){
header("Location: ".$redirect);
die;
}else{
while($row = mysql_fetch_assoc($result)){ // This line was causing the error since there was no row to fetch.
echo($row['example']); // Do you mean to use mysql_fetch_assoc() above?
}
}
}else{
header("Location: ".$redirect);
die;
}
?>[/code]
Copy linkTweet thisAlerts:
@spliffauthorMay 30.2007 — I assume you meant to place $ in front of id, because if you do that then this code works if you fill in a correct ID.

HOWEVER once again it renders a blank page when you fill in a non existant ID rather than forwarding it to www.blahblah.com ....

Thanks
Copy linkTweet thisAlerts:
@spliffauthorMay 30.2007 — isnt there something you can do using this:

[code=php]$refer = header("Location: http://www.blahblah.com");

$result = mysql_query("SELECT * FROM table WHERE id='$id'") or die($refer);[/code]
Copy linkTweet thisAlerts:
@MrCoderMay 30.2007 — Have you done any of the following?

Replace the header() commands with a basic echo to see if the code is ever being executed.

Create a blank page with only the header() command on it to make sure forwarding is working correctly.

Check your PHP log files to see if errors are being logged.

Add the following line to the top of your document.
[code=php]
error_reporting(E_ALL);
[/code]

Error reporting details
Copy linkTweet thisAlerts:
@spliffauthorMay 30.2007 — Yes I did:

[code=php]<?php
mysql_connect("localhost", "admin", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());

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

$result = mysql_query("SELECT * FROM table WHERE id='$id'") or die(mysql_error());

if(mysql_num_rows($result) == 0)
{
echo("NO");

}

else {echo("YES");}

?>[/code]


This worked fine. Header forwarding is also working fine on other pages...
Copy linkTweet thisAlerts:
@spliffauthorMay 30.2007 — got it to work

THANKS FOR ALL YOUR IMPUT
Copy linkTweet thisAlerts:
@MrCoderMay 30.2007 — What was wrong with it?
Copy linkTweet thisAlerts:
@aj_nscMay 30.2007 — Deleted
Copy linkTweet thisAlerts:
@spliffauthorMay 30.2007 — Im not sure what was entirely wrong. I read somewhere that you need a Header function before the HTML tag, I changed that and it still didnt work. I changed something else (but cant really remember what) and it worked ?
Copy linkTweet thisAlerts:
@mynetworksolutMay 30.2007 — Try this! It will work charming!

if($id=="")

header("Location: http://www.YourDomain.com/emptyid.php");


And on that emptyid.php, add this content:

This record has not been found. Please select another item. Go back.

=================

helping others is improving yourself

MyNetworkSolution.com
Copy linkTweet thisAlerts:
@MrCoderMay 30.2007 — Try this! It will work charming!

if($id=="")

header("Location: http://www.YourDomain.com/emptyid.php");


And on that emptyid.php, add this content:

This record has not been found. Please select another item. Go back.

=================

helping others is improving yourself

MyNetworkSolution.com[/QUOTE]


A. He got it working.

B. That would not work as requested anyway.
×

Success!

Help @spliff spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...