Php get id from url
Ok I am stumped so bad. I am using a form to insert a topic and comment into a table and if the topic is new it creates a new table specific to that topic. So I pass the topic name through the url and Im trying to retrieve it in another page. I see the id passed in the url and I try getting it using $topic =$_GET['id']; . I use the topic to insert the into another table.
Here are the different pages. They are all butchered up with me trying to solve this problem.
inserttopic.php
</head>
<body>
<?php
include 'config2.php';
include 'opendb.php';
include 'dbntable.php';
if(!isset($_GET)) {
$topic =$_GET['id'];
echo "Get". $topic ;
}else{
$topic=$_POST['topic'];
echo "Post". $topic ;
}
$comment=$_POST['comment'];
$username=$_POST['username'];
$timestamp= date("g:ia");
if (!$conn)
{
die('Could not connect: ' . mysql_error());
}
//below experimental-search for topic in column.
//If no match create new table, if match ask user to be more specific or comment on current topic.
$search = "SELECT * FROM $tablename WHERE topic = '$topic' ORDER BY city ASC";
$yes=mysql_query($search);
if($yes)
{
echo "Topic already exists. Please be more specific with your topic.";
mysql_query("INSERT INTO $topic (comment, username, timestamp)
VALUES ('$comment','$username','$timestamp')");
header( 'Location: http://localhost/San%20Antonio/test.php?id='.$topic.'' ) ;
}else{
$create="CREATE TABLE $topic (
id INT(30) NOT NULL AUTO_INCREMENT PRIMARY KEY,
comment VARCHAR(1000),
username VARCHAR(30),
timestamp VARCHAR(30))";
mysql_query($create);
mysql_query("INSERT INTO $tablename (topic, comment, username, timestamp)
VALUES ('$topic','$comment','$username','$timestamp')");
mysql_query("INSERT INTO $topic (comment, username, timestamp)
VALUES ('$comment','$username','$timestamp')");
header( 'Location: http://localhost/San%20Antonio/test.php?id='.$topic.'' ) ;
Echo "this is the topic = ".$topic;
};
echo $topic;
?>
</body>
</html>
Here's the URL thats passed
http://localhost/San%20Antonio/test.php?id=Kobe
insertcomment.php
<?php
include 'config2.php';
include 'opendb.php';
include 'dbntable.php';
if(isset($_GET['id'])){
$topic =$_GET['id'];
}
$comment=$_POST['comment'];
$username=$_POST['username'];
$timestamp= date("g:ia");
if (!$conn)
{
die('Could not connect: ' . mysql_error());
}
//below experimental-search for topic in column.
//If no match create new table, if match ask user to be more specific or comment on current topic.
echo "Topic already exists. Please be more specific with your topic.";
mysql_query("INSERT INTO $topic (comment, username, timestamp)
VALUES ('$comment','$username','$timestamp')");
echo "This is the topic = ".$topic;
?>
</body>
</html>
Errors
Notice: Undefined variable: topic in C:\wamp\www\metrocritix\San Antonio\insertcomment.php on line 33
Notice: Undefined variable: topic in C:\wamp\www\metrocritix\San Antonio\insertcomment.php on line 35
Please help!
RazorRamon
Junior Poster in Training
74 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
In line 12 you should remove the !
pritaeas
Posting Expert
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
I changed line 12 like u said but it still isn't working.
RazorRamon
Junior Poster in Training
74 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
I changed line 12 like u said but it still isn't working.
"isn't working" does not convey anything useful!
evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
Yea I know the coding is kinda jacked up. It's just the end result of trial and error... Lots of errors lol. I'll keep y'all posted on the progress.
RazorRamon
Junior Poster in Training
74 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
$topic is shows up as blank.. even though the id is in the url... I dont know why its not retrieving it from the url...
RazorRamon
Junior Poster in Training
74 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
$topic is shows up as blank.. even though the id is in the url... I dont know why its not retrieving it from the url...
post the url and how you do retrieve the id!
evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392