PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
<?php
$id = $_GET['id'];
$q="Select * from tbl_restaurantinfo WHERE id = '".$id."'";
$rs=mysql_query($q) or die(mysql_error());
$total_rows = mysql_num_rows($rs);
If (mysql_num_rows($rs)==0)
header('Location:http://www.google.com');
Else
echo "This is a valid entry";
?>
I'm having an issue with the above code. If the query returns 0 results I need it to redirect to another page.
Right now I'm receiving the following error.
[text]Warning: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/prod/ccmenus/comments.php:5) in /Applications/XAMPP/xamppfiles/htdocs/prod/ccmenus/comments.php on line 53 [/text]
Not sure why it's having an issue so any help would be greatly appreciated.
Well, the problem is exactly what the error message is telling you. You cannot send a header if any output at all has already been sent to the browser - not even a blank space. Something on line 5 of your comments.php file has already sent something to the browser. You need to look and see what it is.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<?php include("../include/SEO.php"); ?>
<title>Capital City Menus – Springfield, IL - Restaurant Menus, Restaurant Maps, Restaurant Locations</title>
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
<link href="../styles/default.css" rel="stylesheet" type="text/css" />
<link href="../styles/table_style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php include("../include/opendbconnection.php"); ?>
<div id="header">
</div>
<div id="menu">
<ul>
<li<?PHP If(($category=="All")or($category==""))echo"class='active'";?>><a href="../ccmenus/menuview.php?category=All">All</a></li>
<li<?PHP If($category=="American")echo"class='active'";?>><a href="../ccmenus/menuview.php?category=American">American</a></li>
<li<?PHP If($category == "Asian")echo"class='active'";?>><a href="../ccmenus/menuview.php?category=Asian">Asian</a></li>
<li<?PHP If($category == "Italian")echo"class='active'";?>><a href="../ccmenus/menuview.php?category=Italian">Italian</a></li>
<li<?PHP If($category == "Mexican")echo"class='active'";?>><a href="../ccmenus/menuview.php?category=Mexican">Mexican</a></li>
<li<?PHP If($category == "Pizza")echo"class='active'";?>><a href="../ccmenus/menuview.php?category=Pizza">Pizza</a></li>
<li<?PHP If($category=="Steak-Seafood")echo"class='active'";?>><a href="../ccmenus/menuview.php?category=Steak-Seafood">Steak & Seafood</a></li>
<li<?PHP If ($category == "About") echo "class='active'";?>><a href="../ccmenus/menuview.php?category=About">About</a></li>
</ul>
</div>
<div id="content">
<div id="colOne">
</div>
<div id="colTwo">
<!--This is just a simple test to see if I can pull the ID-->
<p>The query string is: <?php echo $_SERVER['QUERY_STRING']; ?></p>
<!--This is a test to see If I can pull the name of the ID above-->
<?php
$id = $_GET['id'];
$q="Select * from tbl_restaurantinfo WHERE id = '".$id."'";
$rs=mysql_query($q) or die(mysql_error());
while ($row=mysql_fetch_array($rs))
{
echo "<td class='contact'><b>" . $row['display_name'] . "</b>";
}?>
<br />
<!--This is a test to see if the row ID is valid. If not, it will redirect to homepage.-->
<?php
$id = $_GET['id'];
$q="Select * from tbl_restaurantinfo WHERE id = '".$id."'";
$rs=mysql_query($q) or die(mysql_error());
$total_rows = mysql_num_rows($rs);
If (mysql_num_rows($rs)==0)
echo "This is a invalid entry.";
Else
echo "This is a valid entry";
?>
</div>
<div id="colThree">
<?php include ("../include/colthree.php"); ?>
</div>
<div id="footer">
<center><?php include("../include/footer.php"); ?></center>
</div>
<br />
<center><a href="http://www.dreamhost.com/green.cgi">
<img border="0" alt="Green Web Hosting! This site hosted by DreamHost."
src="https://secure.newdream.net/green1.gif" height="32" width="100" /></a></center>
</body>
</html>
This is what you are sending to the browser:
[syntax]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />[/syntax]
You can't send headers after you have started to send the html for the web page.
I see a lot of scripts on this forum which attempt to use header('Location: ....) as a routine programming tool, and I don't like it. Sending a location header is slow and clumsy; and often there are more elegant and economic solutions available. OK, rant over.
In your case, if a user types in some random id, and the database query returns no results, why not simply display a message that no records have been found and why don't they try again? Sending the user off to google means sending him/her away from your site, and generally this is not what web design is about!
Google was just for testing. In reality it would send them back to the homepage of my site.
The reason why I want the redirect is because the commenting software I will be using looks at the URL and determines what set of comments to load.
If a user screws around in the URL field and enters a invalid ID number the commenting software will treat it as a new URL and create a new comment section for it.
Yup - that's what you need to do. Another of my rants is about doing the logic first and the webpage second. I hate it when I see html and php mixed up together in the same script.