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!

Dani AI

Generated

The undefined variable notices happen because $topic is never guaranteed to be set. In PHP, $_GET is always defined as an array, so checking the array itself does not tell you whether a specific key exists. You must test the key and handle the else path.

Example pattern you can drop into both pages:

$topic = '';
if (isset($_GET['id'])) {
    $topic = $_GET['id'];
} elseif (isset($_POST['topic'])) {
    $topic = $_POST['topic'];
}
if ($topic === '') {
    die('Missing topic.');
}

When redirecting, build the URL safely and stop execution:

header('Location: http://localhost/San%20Antonio/test.php?id=' . urlencode($topic));
exit;

Also note that mysql_query($search) returns a resource even when 0 rows match. To decide if the topic exists, check the row count:

$res = mysql_query("SELECT 1 FROM `$tablename` WHERE topic = '" . mysql_real_escape_string($topic) . "' LIMIT 1");
$exists = $res && mysql_num_rows($res) > 0;

Important design and security notes:

  • Do not create a new table per topic or use user input as a table name. Normalize: a topics table and a comments table with topic_id is safer and far easier to maintain.
  • Migrate from the old mysql_* API to mysqli or PDO with prepared statements to prevent SQL injection and for forward compatibility.

Docs: superglobals and $_GET (php.net), header() (php.net), mysql_num_rows() behavior (php.net), mysqli/PDO (php.net, https://www.php.net/manual/en/book.pdo.php), SQL injection basics (https://owasp.org/www-community/attacks/SQL_Injection).

Recommended Answers

All 7 Replies

In line 12 you should remove the !

I changed line 12 like u said but it still isn't working.

Well mate... I don't know where to start..
You are trying to do strange things with your code :)

the notices you posted are just alerts, they don't stop the code from runnig...
Those notices exist because you are not creating the var $topic before that point on the code...
Please paste this on line 6 "$topic = false;" and this on line on line 10 "if( empty( $topic ) ){ echo 'no topic';die();}" on the insertcomment.php

by the way you should convert the topic to a sef like name ;)

I hope that code is not for a real site / production site :)

best regards

I changed line 12 like u said but it still isn't working.

"isn't working" does not convey anything useful!

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.

$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...

$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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.