Menu
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
[code=php] or die (header("www.bla.com") ) ;[/code]
[code=php]
$result = mysql_query("SELECT..................");
if(mysql_num_rows($result) === 0)
{
header("Location: http://www.blahblah.com");
die();
} else {
/*
Other code
*/
}
[/code]
[code=php]
$result = mysql_query("SELECT..................");
if(mysql_num_rows($result) === 0)
{
header("Location: http://www.blahblah.com");
die();
} else {
/*
Other code
*/
}
[/code]
[/QUOTE]
[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]
[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]
[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]
[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]
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]
[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]
[code=php]$refer = header("Location: http://www.blahblah.com");
$result = mysql_query("SELECT * FROM table WHERE id='$id'") or die($refer);[/code]
[code=php]
error_reporting(E_ALL);
[/code]
[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]
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]
0.1.9 — BETA 6.2