我遇到麻烦的方式,我尝试了一些不同的方法,但似乎没有任何效果。
当我拥有它时,如下面的代码,它可以工作,但它不会插入用户名数据。
我收到错误页面,上面写着“解析错误:语法错误,意外T_STRING”
<?php
include "connect.php"; //connection string
print "<link rel='stylesheet' href='style.css' type='text/css'>";
print "<table class='maintables'>";
print "<tr class='headline'><td>Post a message</td></tr>";
print "<tr class='maintables'><td>";
if(isset($_POST['submit']))
{
$name=$_POST['name'];
$yourpost=$_POST['yourpost'];
$subject=$_POST['subject'];
if(strlen($name)<1)
{
print "You did not type in a name."; //no name entered
}
else if(strlen($yourpost)<1)
{
print "You did not type in a post."; //no post entered
}
else if(strlen($subject)<1)
{
print "You did not enter a subject."; //no subject entered
}
else
{
$thedate=date("U"); //get unix timestamp
$displaytime=date("F j, Y, g:i a");
//we now strip HTML injections
$subject=strip_tags($subject);
$name=strip_tags($name);
$yourpost=strip_tags($yourpost);
$insertpost="INSERT INTO forumtutorial_posts(author,title,post,showtime,realtime,lastposter) values('$name','$subject','$yourpost','$displaytime','$thedate','$name')";
mysql_query($insertpost) or die("Could not insert post"); //insert post
print "Message posted, go back to <A href='index.php'>Forum</a>.";
}
}
else
{
print "<form action='post.php' method='post'>";
print "<input type='hidden' name='name' value='<? echo("$_SESSION[usr_name]");?>' size='20'><br>";
print "Subject:<br>";
print "<input type='text' name='subject' size='20'><br>";
print "Your message:<br>";
print "<textarea name='yourpost' rows='5' cols='40'></textarea><br>";
print "<input type='submit' name='submit' value='submit'></form>";
}
print "</td></tr></table>";
?>
如果我这样做是为了让用户按照自己的意愿添加用户名,那么它可以工作,但我希望它能像我们系统会话中的代码一样工作。
还有另外一种方法吗?谢谢!
答案 0 :(得分:1)
您提供的细节不多,但我在此处看到至少一个语法错误:
print "<input type='hidden' name='name' value='<? echo("$_SESSION[usr_name]");?>' size='20'><br>";
你在字符串和变量之间缺少有效字符:
print "<input (...) echo("$_SESSION[usr_name]"); (...)";
^^ ^^
但是想要猜测你想要什么,我打赌你基本上在PHP代码中包含PHP代码,并且你做错了。
这样做:
print "<input type='hidden' name='name' value='$_SESSION[usr_name]' size='20'><br>";
甚至更干净:
print '<input type="hidden" name="name" value="' . $_SESSION[usr_name] . '" size="20"><br>';