[英]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. 好的,上面的代码在这种情况下仅允许我访问特定
row
,并从中获取我可能需要的必要信息。 But what I want to know is, what if I want to give that question a URL? 但是我想知道的是,如果我想给该问题提供一个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.
例如,当我在Stackoverflow上发布我的问题时,很可能会将其保存在数据库中,并采用ID或类似的名称,因此社区成员和我可以在以后参考。 This will allow everyone to click on 'this' question because it will have a specific ID/reference.
这将使每个人都可以单击“此”问题,因为它将具有特定的ID /参考。 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?
同样,我在MySQL表中确实有用于唯一行/问题的特定ID,但是如何访问具有该特定ID的URL? Ie 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. 例如,我上面说明的代码来自
savedisc.php
,它只是将用户通过表单将以简单文本形式获取的讨论保存到MySQL表中。 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
. 我当然不希望直接转到
savedisc.php
来查看用户的讨论,但是考虑到实际问题的信息确实与primary key ID=115521
相关联,所以也许类似于savedisc.php?id=115521
东西。 Will I have to make another field or something? 我需要另辟field径吗?
Maybe I am doing something wrong in the code below? 也许我在下面的代码中做错了什么? This comes from
savedisc.php
: 这来自
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. 基本上您已经有了答案-通过说?ID = 115521,您将一个值传递给了PHP脚本。 This is what you get by refering to $_GET['ID'], ie the way of linking the URL to the database field.
这是通过引用$ _GET ['ID']获得的,即将URL链接到数据库字段的方式。 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: 不太了解问题,但是如果要在发布后获取新创建的ID并将用户重定向到该ID,请使用mysql_insert_id:
http://php.net/manual/en/function.mysql-insert-id.php http://php.net/manual/zh/function.mysql-insert-id.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.