Php 如何为用户发布的问题提供唯一的url/id?

Php 如何为用户发布的问题提供唯一的url/id?,php,mysql,Php,Mysql,有一个名为discussion.php的表单,用户将在该表单中填写他/她的问题/讨论,然后将其发布到savedisc.php。一些savedisc.php如下所示: $message = $_POST['message']; $title = $_POST['title']; $represents = $_POST['represents']; //connect to database //save the content of discussion/question into the

有一个名为
discussion.php
的表单,用户将在该表单中填写他/她的问题/讨论,然后
将其发布到
savedisc.php
。一些
savedisc.php
如下所示:

$message = $_POST['message'];
$title = $_POST['title'];
$represents = $_POST['represents'];

//connect to database

//save the content of discussion/question into the database for future use
$sql="INSERT INTO Discussion (Message, Title, Type)
VALUES
('$message','$title','$represents')";

//Display user's question/discussion again
echo $message . "<br />";
echo $title . "<br />";
echo $represents . "<br />";
while($row = mysql_fetch_array($result))
  {
  echo "<a href='wbcomm.php?id={$row['id']}' >{$row['Title']}</a><br />";
  }
在这里之前,一切都很顺利。然而,从这里开始,我试图做的是,当用户通过上述代码单击问题/讨论标题时,我希望他/她被引导到
wbcomm.php?id=1
,其中
id=1
表示问题/讨论的唯一id。
wbcomm.php
中的一些代码如下:

if (isset($_GET['id']))
{
//connect to db

$wbid  = mysql_real_escape_string($_GET['id']);
    $sql = "SELECT * FROM Discussion WHERE id = '$wbid' LIMIT 1";
    $res = mysql_query($sql);
    if (mysql_num_rows() > 0) {
        $discussion = mysql_fetch_object($res);
        //display member's question here:
  echo $discussion['id'] . "<br />";
  echo $discussion['Title'] . "<br />";
  echo $discussion['Type'] . "<br />";
  echo $discussion['Message'] . "<br />";
    }
    else {
        // discussion does not exist with ID
    }
}
if(isset($\u GET['id']))
{
//连接到数据库
$wbid=mysql\u real\u escape\u字符串($\u GET['id']);
$sql=“从讨论中选择*,其中id='$wbid'限制1”;
$res=mysql\u查询($sql);
if(mysql\u num\u rows()>0){
$discussion=mysql\u fetch\u对象($res);
//在此处显示成员的问题:
echo$discussion['id']。“
”; echo$讨论['Title']。“
”; echo$discussion['Type']。“
”; echo$discussion['Message']。“
”; } 否则{ //ID为的讨论不存在 } }
然而,由于某种原因,结果是空白的。也就是说,问题/讨论甚至没有出现。我做错了什么?我的程序正确吗


谢谢。

您的ID列是一个自动递增的int类型,因此您不需要将其加引号或转义。当然,你应该测试它是否是数字。

在你的
wb.php
中,你创建了一个指向
wbcomm.php
的链接,但是你没有传递讨论的ID,所以你的
$wbid
将是空的。您需要将ID与链接一起传递,如下所示:

$message = $_POST['message'];
$title = $_POST['title'];
$represents = $_POST['represents'];

//connect to database

//save the content of discussion/question into the database for future use
$sql="INSERT INTO Discussion (Message, Title, Type)
VALUES
('$message','$title','$represents')";

//Display user's question/discussion again
echo $message . "<br />";
echo $title . "<br />";
echo $represents . "<br />";
while($row = mysql_fetch_array($result))
  {
  echo "<a href='wbcomm.php?id={$row['id']}' >{$row['Title']}</a><br />";
  }
while($row=mysql\u fetch\u array($result))
{
回声“
”; }
使用此SQL
mysql\u num\u rows($res)>0
帮助您进行调试您应该考虑测试$res并显示任何错误消息(mysql\u error())