0

I'm receiving an 'Undefined variable' for $topic_title on line 28, the data is inserted into the database without any issue but I can't get the topic title to display on the page.

Side note: I've been working through a PHP book and have received many errors based on the code they've used (Sam's Teach Yourself, 2002... though it may be revised). Is it worth continuing down this route?

<?php
//check for required fields from the form
if ((!$_POST['topic_owner']) || (!$_POST['topic_title'])
    || (!$_POST['post_text'])) {
    header("Location: addtopic.html");
    exit;
}

//connect to server and select database
$conn = mysql_connect("localhost", "aaron", "pass")
or die(mysql_error());
mysql_select_db("test",$conn) or die(mysql_error());

//create and issue the first query
$add_topic = "insert into forum_topics values ('', '$_POST[topic_title]',
     now(), '$_POST[topic_owner]')";
mysql_query($add_topic,$conn) or die(mysql_error());

//get the id of the last query
$topic_id = mysql_insert_id();

//create and issue the second query
$add_post = "insert into forum_posts values ('', '$topic_id',
     '$_POST[post_text]', now(), '$_POST[topic_owner]')";
mysql_query($add_post,$conn) or die(mysql_error());

//create nice message for user
$msg = "<P>The <strong>$topic_title'</strong> topic has been created.</p>";
?>
<html>
<head>
    <title>New Topic Added</title>
</head>
<body>
<h1>New Topic Added</h1>
<?php print $msg; ?>
</body>
</html>

2 Answers2

3

You at no point declare topic title try $topic_title = $_POST['topic_title']

Post vars are not automatically available.

Daniel Williams
  • 8,477
  • 4
  • 33
  • 47
1

$topic_title doesn't exist.

New code:

<?php
//check for required fields from the form
if ((!$_POST['topic_owner']) || (!$_POST['topic_title'])
    || (!$_POST['post_text'])) {
    header("Location: addtopic.html");
    exit;
}

//connect to server and select database
$conn = mysql_connect("localhost", "aaron", "pass")
or die(mysql_error());
mysql_select_db("test",$conn) or die(mysql_error());

//create and issue the first query
$add_topic = "insert into forum_topics values ('', '$_POST[topic_title]',
     now(), '$_POST[topic_owner]')";
mysql_query($add_topic,$conn) or die(mysql_error());

//get the id of the last query
$topic_id = mysql_insert_id();

//create and issue the second query
$add_post = "insert into forum_posts values ('', '$topic_id',
     '$_POST[post_text]', now(), '$_POST[topic_owner]')";
mysql_query($add_post,$conn) or die(mysql_error());

//create nice message for user
$msg = "<P>The <strong>".$_POST['topic_title']."</strong> topic has been created.</p>";
?>
<html>
<head>
    <title>New Topic Added</title>
</head>
<body>
<h1>New Topic Added</h1>
<?php print $msg; ?>
</body>
</html>
Companjo
  • 1,769
  • 18
  • 22