开发者

How to give a user's question a URL based on it's ID in MySQL, so one may access it in the future?

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 开发者_StackOverflow社区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.


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?


Don't quite understand the problem, but if you want to get the newly created ID after posting and redirect the user to that, use mysql_insert_id:

http://php.net/manual/en/function.mysql-insert-id.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜