Page 1 of 1

errors in my script

Posted: Fri Jul 24, 2009 11:05 am
by mayanktalwar1988

Code: Select all

<?php
if (!empty($_POST)) {
$host = "localhost";
$user = "root";
$pass = "";
$db = "ask";
$id=$_POST['cat_id'];
$con = mysql_connect($host,$user,$pass);
mysql_select_db($db,$con);
 
if (mysql_query("INSERT INTO topics (title,post,author,mail,date,cat_id) VALUES ('".$_POST['title']."','".$_POST['post']."','".$_POST['name']."','".$_POST['mail']."','".time()."','".$_POST['cat_id']."')")) {
header('location:topic.php?id="$id"');
 
 
} else {
echo "Error!";
}
 
mysql_close($con);
}
?>


this line header('location:topic.php?id="$id'');
from above code is giving problem
when the control arrive to this statement the browser address changes to this http://localhost/customo/topic.php?id="$id"
instead of of something like this http://localhost/customo/topic.php?id=1 which i want.....whats the error

Re: errors in my script

Posted: Fri Jul 24, 2009 11:42 am
by andyhoneycutt

Code: Select all

header('location:topic.php?id="$id"');
Should be:

Code: Select all

header("location:topic?id=$id");
You're trying to put an interpreted string inside a literal in a way, I must say, I have not seen before :)

-Andy

Re: errors in my script

Posted: Fri Jul 24, 2009 12:07 pm
by mayanktalwar1988
andyneycutt my friend i also tried that before postimg my problem in thisforum..it still come http://localhost/customo/topic.php?id=.1.
now why the dots are coming in the link...and it not worked for me

Re: errors in my script

Posted: Fri Jul 24, 2009 12:18 pm
by andyhoneycutt
mayanktalwar1988 wrote:now why the dots are coming in the link...and it not worked for me
I'm not sure. You'll need to track down on the page that posts to this one where the $id is entered/created to see why it has dots in it. It would appear that it's either coming from your input or from how you are handling the creation of this variable.

-Andy

Re: errors in my script

Posted: Fri Jul 24, 2009 1:02 pm
by mayanktalwar1988

Code: Select all

?php
if (isset($_GET['id'])){
 
$host = "localhost";
$user = "root";
$pass = "";
$db = "ask";
 
$con = mysql_connect($host,$user,$pass);
mysql_select_db($db,$con);
 
$id = $_GET['id'];
echo "<a href=\"create_topic.php?id=$id\">New topic</a>";
echo "<table border='0' width='100%'>";
 
$sql = mysql_query("SELECT * FROM topics WHERE cat_id='".$id."'");
while($row = mysql_fetch_array($sql)) {
echo "<tr><td>".htmlentities($row['title'])."</td></tr>
<tr><td>".str_replace("\n","<br />",htmlentities($row['post']))."</td></tr>
<tr><td><a href='mailto:".$row['mail']."'>".htmlentities($row['author'])."</a></td></tr>
<tr><td>".date("j/n - y",$row['date'])."</td></tr>";
}
 
 
 
echo "</table>";
 
mysql_close($con);
} else {
echo "invalid usage!";
}
?>
the above is the file to which i am linking my problem script.what is the problem with the previous script

Re: errors in my script

Posted: Fri Jul 24, 2009 1:46 pm
by andyhoneycutt
Would it be possible for you to show me the code where $_GET['id'] is originally set? If you can trace it back to the point of origin, I imagine I will be much better able to help you.

Thanks much,
Andy

Re: errors in my script

Posted: Fri Jul 24, 2009 2:45 pm
by mayanktalwar1988
index.php

Code: Select all

<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "ask";
 
$con = mysql_connect($host,$user,$pass);
mysql_select_db($db,$con);
 
//echo "<a href='create_topic.php'>New topic</a>";
echo "<table border='0' width='100%'>
<tr><td>#</td><td>forum</td><td>topics</td><td>Posts</td><td>last post by</td></tr>";
 
$sql = mysql_query("SELECT * FROM category ORDER BY cat_id ");
while($row = mysql_fetch_array($sql)) {
$replies = mysql_num_rows(mysql_query("SELECT * FROM replies WHERE topic_id='".$row['id']."'"));
 
echo "<tr><td>".$row['cat_id']."</td><td><a href='topic.php?id=".$row['cat_id']."'>".htmlentities($row['category_topic'])."</a></td><td>".$replies."</td><td>".date("j/n - y",$row['created'])."</td></tr>";
}
 
echo "</table>";
mysql_close($con);
?>
 

the above is the index .php


Code: Select all

 
<?
$id=$_GET['id'];
echo'<table>';
echo"<form action='add_topic.php' method='post'>
<tr><td>Name</td><td>:</td><td><input type='text' id='name' name='name' /></td></tr>
<tr><td>Email</td><td>:</td><td><input type='text' id='mail' name='mail' /></td></tr>
<tr><td>Title</td><td>:</td><td><input type='text' id='title' name='title' /></td></tr>
<tr><td valign='top'>Post</td><td valign='top'>:</td><td><textarea id='post' name='post' rows='7'></textarea></td></tr>
<tr><td> </td><td> </td><td><input type='submit' value='Post Topic' /> <input type='reset' value='Reset Fields' /></td></tr>
<input type='hidden' id='cat_id' name='cat_id' value=\".$id.\" />
 
</form>";
echo'</table>';
?>
and the above is the file create.php from which i am sendind the data to the script add_topic.php

add_topic.php

Code: Select all

<?php
if (!empty($_POST)) {
$host = "localhost";
$user = "root";
$pass = "";
$db = "ask";
$id=$_POST['cat_id'];
$con = mysql_connect($host,$user,$pass);
mysql_select_db($db,$con);
 
if (mysql_query("INSERT INTO topics (title,post,author,mail,date,cat_id) VALUES ('".$_POST['title']."','".$_POST['post']."','".$_POST['name']."','".$_POST['mail']."','".time()."','".$_POST['cat_id']."')")) {
header("location:topic.php?id=$id");
 
 
} else {
echo "Error!";
}
 
mysql_close($con);
}
?>
so the problem i s in the above script





and below is the file to which i wana redirect from add_topic.php

Code: Select all

 
<?php
if (isset($_GET['id'])){
 
$host = "localhost";
$user = "root";
$pass = "";
$db = "ask";
 
$con = mysql_connect($host,$user,$pass);
mysql_select_db($db,$con);
 
$id = $_GET['id'];
echo "<a href=\"create_topic.php?id=$id\">New topic</a>";
echo "<table border='0' width='100%'>";
 
$sql = mysql_query("SELECT * FROM topics WHERE cat_id='".$id."'");
while($row = mysql_fetch_array($sql)) {
echo "<tr><td>".htmlentities($row['title'])."</td></tr>
<tr><td>".str_replace("\n","<br />",htmlentities($row['post']))."</td></tr>
<tr><td><a href='mailto:".$row['mail']."'>".htmlentities($row['author'])."</a></td></tr>
<tr><td>".date("j/n - y",$row['date'])."</td></tr>";
}
 
 
 
echo "</table>";
 
mysql_close($con);
} else {
echo "invalid usage!";
}
?>
...now help

Re: errors in my script

Posted: Fri Jul 24, 2009 2:56 pm
by andyhoneycutt
From create.php:

Code: Select all

echo"<form action='add_topic.php' method='post'>
<tr><td>Name</td><td>:</td><td><input type='text' id='name' name='name' /></td></tr>
<tr><td>Email</td><td>:</td><td><input type='text' id='mail' name='mail' /></td></tr>
<tr><td>Title</td><td>:</td><td><input type='text' id='title' name='title' /></td></tr>
<tr><td valign='top'>Post</td><td valign='top'>:</td><td><textarea id='post' name='post' rows='7'></textarea></td></tr>
<tr><td> </td><td> </td><td><input type='submit' value='Post Topic' /> <input type='reset' value='Reset Fields' /></td></tr>
<input type='hidden' id='cat_id' name='cat_id' value=\".$id.\" />
 
</form>";
Should be written:

Code: Select all

echo "<form action='add_topic.php' method='post'>
<tr><td>Name</td><td>:</td><td><input type='text' id='name' name='name' /></td></tr>
<tr><td>Email</td><td>:</td><td><input type='text' id='mail' name='mail' /></td></tr>
<tr><td>Title</td><td>:</td><td><input type='text' id='title' name='title' /></td></tr>
<tr><td valign='top'>Post</td><td valign='top'>:</td><td><textarea id='post' name='post' rows='7'></textarea></td></tr>
<tr><td> </td><td> </td><td><input type='submit' value='Post Topic' /> <input type='reset' value='Reset Fields' /></td></tr>
<input type='hidden' id='cat_id' name='cat_id' value=\"$id\" />
 
</form>";
You were escaping the quotes, which is correct, but assuming that it was breaking out of the string, which is incorrect. No need to concatenate inside the string.

-Andy

Re: errors in my script

Posted: Fri Jul 24, 2009 3:11 pm
by mayanktalwar1988
thank you andy for your support and quick reply ...that worked..the whole prob was in other file...thanks :D that is from where the dots are coming in

Re: errors in my script

Posted: Fri Jul 24, 2009 3:12 pm
by andyhoneycutt
No problem! Glad to help :)