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
    Any PHP book mentioning using the **deprecated** `ext/mysql` family of functions isn't worth using. Throw it in the recycle bin. Don't pass it on, it's trash. – Charles Apr 01 '14 at 02:14
  • learn to code php <-- that that in google. Your book is outdated. If you see anything suggesting mysql_query it is trash. Forget it and move to something else, that's the old way. The new way is mainly with PDO. (some might still refer to ADODB which is still valid) – Jack M. Apr 01 '14 at 02:17
  • Thanks guys, will bin the book in that case. – Aeryn Romeo Apr 01 '14 at 02:23

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,673
  • 4
  • 36
  • 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,789
  • 18
  • 24