Page 1 of 1

problem with php code

Posted: Sat Sep 26, 2009 1:23 pm
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

Re: problem with php code

Posted: Sat Sep 26, 2009 2:33 pm
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.

Re: problem with php code

Posted: Sat Sep 26, 2009 3:14 pm
by Mirge
Beware SQL injection too.

Re: problem with php code

Posted: Sat Sep 26, 2009 3:24 pm
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

Re: problem with php code

Posted: Sat Sep 26, 2009 3:40 pm
by jackpf
You can't send anything before headers if you're not using OB.

Re: problem with php code

Posted: Sat Sep 26, 2009 3:48 pm
by mayanktalwar1988
what sholud i do to get over this warninig....?

Re: problem with php code

Posted: Sat Sep 26, 2009 4:19 pm
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.

Re: problem with php code

Posted: Sat Sep 26, 2009 10:24 pm
by mayanktalwar1988
ok..incredibly well documented :lol:

Re: problem with php code

Posted: Sat Sep 26, 2009 10:54 pm
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

Re: problem with php code

Posted: Sat Sep 26, 2009 11:08 pm
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