php 如何从 url 获取 ID 值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8122101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:00:04  来源:igfitidea点击:

How do I get the ID value from the url?

phpmysqlforumsreply

提问by Mitchell Verbeek

I am trying to make my own forums using a tutorial. I have already fixed quite a lot of errors and I have 1 left to do.

我正在尝试使用教程制作自己的论坛。我已经修复了很多错误,我还有 1 个要做。

When you post a reply on a thread it will save your reply in a MySQL database. You reply will have an int that matches with the thread id you replied on, called question_id.

当您在线程上发布回复时,它会将您的回复保存在 MySQL 数据库中。您的回复将有一个与您回复的线程 ID 匹配的 int,称为 question_id。

I cant get my code to get the thread id from the URL bar.

我无法让我的代码从 URL 栏中获取线程 ID。

These are the 2 lines of code that should do the work:

这些是应该完成工作的两行代码:

$id = $_GET['id'];

$sql2="INSERT INTO $tbl_name(question_id, a_id, a_name, a_email, a_answer, a_datetime)VALUES('$id', '$Max_id', '$a_name', '$a_email', '$a_answer', '$datetime')";

And this is the entire file add_answer.php

这是整个文件 add_answer.php

<?php

$host="XXXXXX"; // Host name
$username="XXXXXX"; // Mysql username
$password="XXXXXX"; // Mysql password
$db_name="XXXXXX"; // Database name
$tbl_name="XXXXXX"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Get value of id that sent from hidden field
$id = $_GET['id'];
var_dump($id);

// Find highest answer number.
$sql="SELECT MAX(a_id) AS Maxa_id FROM $tbl_name WHERE question_id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);

// $result=mysql_query($sql) or die(mysql_error());

// add + 1 to highest answer number and keep it in variable name "$Max_id". if there no answer yet set it = 1
if ($rows) {
    $Max_id = $rows['Maxa_id']+1;
}
else {
    $Max_id = 1;
}

// get values that sent from form
$a_name=$_POST['a_name'];
$a_email=$_POST['a_email'];
$a_answer=$_POST['a_answer'];

$datetime=date("d/m/y H:i:s"); // create date and time

// Insert answer
$sql2="INSERT INTO $tbl_name(question_id, a_id, a_name, a_email, a_answer, a_datetime)VALUES('$id', '$Max_id', '$a_name', '$a_email', '$a_answer', '$datetime')";
$result2=mysql_query($sql2) or die(mysql_error());

if($result2)
{
    echo "Successful<BR>";
    echo "<meta http-equiv='refresh' content='4;url=index.php?content=main_forum' />";

    // If added new answer, add value +1 in reply column
    $tbl_name2="forum_question";
    $sql3="UPDATE $tbl_name2 SET reply='$Max_id' WHERE id='$id'";
    $result3=mysql_query($sql3);

}
else {
    echo "An error as occured. Please report this to administrator. ID: FAA01.";
}

mysql_close();
?>

回答by David Houde

your $_GET[id] should be sanitized before touching mysql with it. As it stands right now you are in very real danager of an attack on your database. I suggest using PDO

你的 $_GET[id] 在接触 mysql 之前应该被清理。就目前而言,您的数据库受到攻击的危险非常大。我建议使用PDO

It sucks to re-learn something, but if you are learning, its better to do it right.

重新学习一些东西很糟糕,但如果你正在学习,最好把它做对。

As for your question...I don't know. I would try echo $sql2; and make sure the query looks right. Also print_r($_GET) to see your get array.

至于你的问题……我不知道。我会尝试 echo $sql2; 并确保查询看起来正确。还 print_r($_GET) 以查看您的 get 数组。

回答by Niet the Dark Absol

  1. Does $_GET['id']exist? As in, is it actually present in the URL? What is the exact URL used? Double-check there aren't any uppercase letters in the way, since you appear to mix cases a lot (tip: only use lower case letters in code)

  2. Learn about addslashesand mysql_real_escape_string, otherwise someone WILL reduce your server to a melted wreck.

  3. You have redundant error checking.

    $result2=mysql_query($sql2) or die(mysql_error());
    // ...
    if( $result2) {...} else {...}
    
  1. 是否$_GET['id']存在?如,它实际上存在于 URL 中吗?使用的确切网址是什么?仔细检查没有任何大写字母,因为您似乎经常混合大小写(提示:仅在代码中使用小写字母)

  2. 了解addslashesmysql_real_escape_string,否则有人会将您的服务器减少为熔化的残骸。

  3. 你有多余的错误检查。

    $result2=mysql_query($sql2) or die(mysql_error());
    // ...
    if( $result2) {...} else {...}
    

That last bit is redundant, since if $result2is false then it's already died.

最后一点是多余的,因为如果$result2是假的,那么它已经死了。

回答by álvaro González

You have to debug one thing at a time! You say:

你必须一次调试一件事!你说:

I cant get my code to get the thread id from the url bar.

我无法让我的代码从 url 栏中获取线程 id。

so the first place to look at is your browser's location bar, where the URL is. Why care about the next hundred lines of code if the first one is not right? If your thread ID is 999 and you want this code to fetch such ID:

所以首先要查看的是浏览器的地址栏,即 URL 所在的位置。如果第一个不正确,为什么要关心接下来的一百行代码?如果您的线程 ID 是 999 并且您希望此代码获取此类 ID:

$id = $_GET['id'];

... you need to see id=999in the URL, after the ?symbol (which is mandatory) and before the #symbol (which is optional).

...您需要id=999在 URL 中看到,在?符号之后(这是强制性的)和#符号之前(这是可选的)。

If ID is not here, you should be getting a notice:

如果 ID 不在此处,您应该会收到通知:

Notice: Undefined index: id

注意:未定义索引:id

If you don't, you haven't configured PHP properly. The simplest way is to find your php.inifile, set these directives:

如果没有,则说明您没有正确配置 PHP。最简单的方法是找到你的php.ini文件,设置这些指令:

error_reporting = E_ALL
display_errors = On

... and restart your web server.

...并重新启动您的网络服务器。

Finally, learn to use var_dump().

最后,学习使用var_dump()

回答by scube

If you form is sent by postyou should get the id with:

如果您的表格是由post您发送的,您应该获得以下 ID:

$id = $_POST['id'];

回答by MANZAR HUSSAIN

If you want to get value from URL then you have to use PHP $_GET[ ]method to get the information or value. important Not:If you don't have images in database then insert some images in database using this tutorial.

如果你想从 URL 获取值,那么你必须使用 PHP $_GET[]方法来获取信息或值。 重要不:如果您在数据库中没有图像,则使用本教程在数据库中插入一些图像。

You can see the below code and link to get more about

您可以查看以下代码和链接以获取更多信息

PHP $_GET Method$_GET method is a PHP super global method. Which is use to parse the browser URL and scan & retrieve the given parameter or information by id.

PHP $_GET 方法$_GET 方法是 PHP 超级全局方法。它用于解析浏览器 URL 并通过 id 扫描和检索给定的参数或信息。

Follow the below link to get more information about this tutorials.

按照以下链接获取有关此教程的更多信息。

https://www.campuslife.co.in/Php/how-to-get-id-from-one-page-to-another-page.php

https://www.campuslife.co.in/Php/how-to-get-id-from-one-page-to-another-page.php

page ID codefor first page

第一页的页面 ID 代码

<a href="display.php?myid=<?php echo $img_id; ?>" target="new window">
    <img src="<?php echo $img_id; ?>" title="Nature Image" />
</a>

$_GET methodcode for second page

第二页的$_GET 方法代码

$id = $_GET["myid"];

Follow the below link to get information step by step tutorial.

按照以下链接获取信息分步教程。

https://www.campuslife.co.in/Php/how-to-get-id-from-one-page-to-another-page.php

https://www.campuslife.co.in/Php/how-to-get-id-from-one-page-to-another-page.php