PHP未定义变量
我在第28行收到$topic_title的“Undefined variable”,数据被插入数据库,没有任何问题,但我无法在页面上显示主题标题 旁注:我一直在阅读一本PHP书籍,根据他们使用的代码,我收到了许多错误(Sam's Teach Yourself,2002…尽管可能会修改)。这条路值得继续走下去吗PHP未定义变量,php,html,Php,Html,我在第28行收到$topic_title的“Undefined variable”,数据被插入数据库,没有任何问题,但我无法在页面上显示主题标题 旁注:我一直在阅读一本PHP书籍,根据他们使用的代码,我收到了许多错误(Sam's Teach Yourself,2002…尽管可能会修改)。这条路值得继续走下去吗 <?php //check for required fields from the form if ((!$_POST['topic_owner']) || (!$_POST['t
<?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>
您在任何时候都不能声明主题标题,请尝试$topic\u title=$\u POST['topic\u title']
Post变量不自动可用。您在任何时候都不能声明主题标题,请尝试$topic\u title=$\u Post['topic\u title']
Post VAR不自动可用。$topic\u title
不存在
新代码:
<?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>
$topic\u title
不存在
新代码:
<?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>
任何提到使用不推荐的ext/mysql
函数系列的PHP书籍都不值得使用。把它扔进回收站。不要把它传下去,它是垃圾。学会编写php谢谢伙计们,在这种情况下会把书扔掉的。任何提到使用不推荐的ext/mysql
函数系列的php书都不值得使用。把它扔进回收站。不要把它传下去,它是垃圾。学会编写php代码谢谢各位,那样的话,我会把书扔掉的。