Click to See Complete Forum and Search --> : PHP question concerning redirection under specific conditions
spliff
05-29-2007, 08:29 AM
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
when you do your SELECT query from the database you can specify what to do if an error occurs (i.e. ID not found)
spliff
05-29-2007, 09:02 AM
can anyone give me an example script for the answer above?
spliff
05-29-2007, 09:39 AM
i tried
or die (header("www.bla.com") ) ;
this didnt work.
please help
MrCoder
05-29-2007, 09:47 AM
$result = mysql_query("SELECT..................");
if(mysql_num_rows($result) === 0)
{
header("Location: http://www.blahblah.com");
die();
} else {
/*
Other code
*/
}
spliff
05-29-2007, 09:54 AM
$result = mysql_query("SELECT..................");
if(mysql_num_rows($result) === 0)
{
header("Location: http://www.blahblah.com");
die();
} else {
/*
Other code
*/
}
i tried this:
<?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'];
}
?>
but it renders a blank page if i enter an id which is not in the database.
please help more ;)
MrCoder
05-29-2007, 10:09 AM
<?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?
}
?>
spliff
05-29-2007, 10:29 AM
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
MrCoder
05-29-2007, 10:57 AM
http://uk.php.net/manual/en/function.mysql-fetch-array.php
http://uk3.php.net/manual/en/function.mysql-fetch-assoc.php
spliff
05-29-2007, 02:57 PM
<?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?
}
?>
this still gives a blank page when the id is not in the database instead of referring to another page...
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 == ??
svidgen
05-29-2007, 03:47 PM
$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?
It should indeed be mysql_fetch_assoc().
mysql_fetch_array() 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).
mysql_fetch_assoc() 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.
spliff
05-29-2007, 04:47 PM
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 == ??
I have no superior PHP knowledge, i used the code that Mr Coder suggested. It DID work when I used an ID that existed...
Sheldon
05-30-2007, 02:49 AM
<?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;
}
?>
spliff
05-30-2007, 05:00 AM
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
spliff
05-30-2007, 05:08 AM
isnt there something you can do using this:
$refer = header("Location: http://www.blahblah.com");
$result = mysql_query("SELECT * FROM table WHERE id='$id'") or die($refer);
MrCoder
05-30-2007, 05:24 AM
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.
error_reporting(E_ALL);
Error reporting details (http://uk3.php.net/manual/en/function.error-reporting.php)
spliff
05-30-2007, 05:29 AM
Yes I did:
<?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");}
?>
This worked fine. Header forwarding is also working fine on other pages...
spliff
05-30-2007, 05:40 AM
got it to work
THANKS FOR ALL YOUR IMPUT
MrCoder
05-30-2007, 06:07 AM
What was wrong with it?
aj_nsc
05-30-2007, 07:20 AM
Deleted
spliff
05-30-2007, 04:47 PM
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 ;)
mynetworksolut
05-30-2007, 06:23 PM
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 (http://mynetworksolution.com)
MrCoder
05-30-2007, 06:50 PM
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 (http://mynetworksolution.com)
A. He got it working.
B. That would not work as requested anyway.