problem with php code

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!

Moderator: General Moderators

Post Reply
mayanktalwar1988
Forum Contributor
Posts: 133
Joined: Wed Jul 08, 2009 2:44 am

problem with php code

Post by mayanktalwar1988 »

location.php

Code: Select all

 
<?
include("mysql_connect.php");
$id = $_GET['id'];
$sql = mysql_query("SELECT * FROM topics WHERE id='".$id."'");
if($row = mysql_fetch_array($sql))
{
header("location:{$row['webpage']}");
 
}
else
{
echo"<h1>error</h1>";
}
mysql_close($con);
?>
 
the above file is not linking to the page pointed by this header("location:{$row['webpage']}");
the one of the link to above file is http://localhost/pro/location.php?id=42 there exist a corresponding address in webpage field of table topic ..i used facebook.com..as expected it must have taken me to facebook.com but it did nothing like that where is the prob
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: problem with php code

Post by Ollie Saunders »

the one of the link to above file is http://localhost/pro/location.php?id=42 there exist a corresponding address in webpage field of table topic ..i used facebook.com..as expected it must have taken me to facebook.com but it did nothing like that where is the prob
Do you mean to say the redirect isn't occurring?

Possibly, try:

Code: Select all

header("Location: {$row['webpage']}");
Actually, no, that probably won't make any difference. Put

Code: Select all

ini_set('display_errors', true); error_reporting(E_ALL);
at the top of the script to see — I think — a "header's already sent" error. Correct that error — Google is your friend — and then your code should work.
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: problem with php code

Post by Mirge »

Beware SQL injection too.
mayanktalwar1988
Forum Contributor
Posts: 133
Joined: Wed Jul 08, 2009 2:44 am

Re: problem with php code

Post by mayanktalwar1988 »

add at the very top means before<? or after<?.....i tried html before <? it give this warninig

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\pro\location.php:3) in C:\xampp\htdocs\pro\location.php on line 10
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: problem with php code

Post by jackpf »

You can't send anything before headers if you're not using OB.
mayanktalwar1988
Forum Contributor
Posts: 133
Joined: Wed Jul 08, 2009 2:44 am

Re: problem with php code

Post by mayanktalwar1988 »

what sholud i do to get over this warninig....?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: problem with php code

Post by Ollie Saunders »

I wrote:Correct that error — Google is your friend — and then your code should work.
Correcting this error is incredibly well documented.
mayanktalwar1988
Forum Contributor
Posts: 133
Joined: Wed Jul 08, 2009 2:44 am

Re: problem with php code

Post by mayanktalwar1988 »

ok..incredibly well documented :lol:
mayanktalwar1988
Forum Contributor
Posts: 133
Joined: Wed Jul 08, 2009 2:44 am

Re: problem with php code

Post by mayanktalwar1988 »

<?

Code: Select all

 
include("mysql_connect.php");
$id =$_GET['id'];
$sql=mysql_query("SELECT * FROM topics WHERE id='".$id."'");
if($row=mysql_fetch_array($sql))
echo"HI";
else
echo"not i";
mysql_close($con);
 
?> 
 
i tried the above code to check whether if block which cantains location(header: function is executing or not..but else block execute instead of if echoing not i..i have 8 files with the same include file
mysql_connect.php

Code: Select all

 
<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "ask";
 
$con=mysql_connect($host,$user,$pass);
mysql_select_db($db,$con);
?>
infact i also have the same type of functionality i am trying to acheive with location.php
i have add_topic.php

Code: Select all

<?php
if (!empty($_POST)) {
 
$id=$_POST['cat_id'];
include("mysql_connect.php");
if (mysql_query("INSERT INTO topics (title,post,author,mail,date,cat_id,webpage) VALUES ('".$_POST['title']."','".$_POST['post']."','".$_POST['name']."','".$_POST['mail']."','".time()."','".$_POST['cat_id']."','".$_POST['webpage']."')")) {
header("location:topic.php?id=$id");
 
 
} 
else 
{
echo "Error!";
}
 
mysql_close($con);
}
?>
it work fine


i also have
add_reply.php

Code: Select all

<?php
if (!empty($_POST)) {
include("mysql_connect.php");
 
if (mysql_query("INSERT INTO replies (topic_id,post,author,mail,date,cat_id) VALUES ('".$_POST['topic_id']."','".$_POST['post']."','".$_POST['name']."','".$_POST['mail']."','".time()."','".$_POST['cat_id']."')")) 
{
header("location:show_reply.php?topic_id={$_POST['topic_id']}&cat_id={$_POST['cat_id']}");
} else {
echo "Error!";
}
 
mysql_close($con);
} else {
echo "Invalid usage!";
}
?>
it also work fine then why not the location work fine which have the same thing like these files
i also ommitted white spces...and there is not a single output occuring before header function still not working and after checking if block which contain header function is not executing
mayanktalwar1988
Forum Contributor
Posts: 133
Joined: Wed Jul 08, 2009 2:44 am

Re: problem with php code

Post by mayanktalwar1988 »

i made one discovery right now
this code

Code: Select all

<?php
include("mysql_connect.php");
if (isset($_GET['id']))
echo"ok";
else
echo"not get";
$id =$_GET['id'];
$sql=mysql_query("SELECT * FROM topics WHERE id='".$id."'");
if($row=mysql_fetch_array($sql))
echo"HI";
else
echo"not i";
mysql_close($con);
 
?>
this code
print not getnot ithis means infact even the $_GET is not functioning properly...i have verything in place now why this not going into its place
Post Reply