duanpanhuo0618 2010-07-29 00:34
浏览 31

如何根据MySQL中的ID为用户提问一个URL,以便将来可以访问它?

For some reason, I just can't seem to get my head around this concept. Any advice/hints will be appreciated. As of now, I have this code:

if(isset($_GET['ID'])) {//Check if ID exists in $_GET
  $ID = mysql_real_escape_string($_GET['ID']); //prevent sql injection
  $resc = mysql_query("SELECT Message, Title, Type FROM Discussion WHERE ID='$ID' LIMIT 1");//Query the db for record

if(mysql_num_rows($resc) == 1) { //make sure the question exists
        $ques = mysql_fetch_assoc($resc); //convert mysql resource into array that can be used throughout script
                               }
                       }

Ok, so the above code simply allows me to access a particular row in this case, and from it, acquire the necessary information I may need. But what I want to know is, what if I want to give that question a URL? For example, when I post my(this) question on Stackoverflow, it will most likely be saved in a database, and will adopt an ID or something similar, so members of the community and I can reference to it at a later time. This will allow everyone to click on 'this' question because it will have a specific ID/reference. Similarly, I do have a specific ID in the MySQL table for the unique rows/questions, but how can I access a URL with that specific ID? I.e. be directed to that actual question when I click on it.
For example, the code that I have illustrated above comes from savedisc.php, which is simply saving a user's discussion acquired by a form in simple text into a MySQL table. So at a later time, when I do decide to access this discussion, how can I 'click' on it. I of course don't want to be directed to savedisc.php to see the user's discussion, but maybe something like savedisc.php?id=115521, considering that the actual question's information is indeed associated with the primary key ID=115521. Will I have to make another field or something?

Maybe I am doing something wrong in the code below? This comes from savedisc.php:

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

$sql="INSERT INTO Discussion (ID, Message, Title, Type)
VALUES
('','$message','$title','$represents')";

Thank you.

  • 写回答

2条回答 默认 最新

  • dqwh0109 2010-07-29 00:37
    关注

    You basically have your answer already - by saying ?ID=115521, you pass on a value to your PHP script. This is what you get by refering to $_GET['ID'], ie the way of linking the URL to the database field. Or am I getting you wrong?

    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效