简体   繁体   English

id在PHP的2个页面之间传递

[英]id passing between 2 pages in PHP

I've been working on a way to get a variable passed from one page to another. 我一直在研究一种将变量从一页传递到另一页的方法。 It's for my exam and I need help with it as my teacher said I could use the internet for help. 这是我的考试,我需要帮助,因为老师说我可以使用互联网来寻求帮助。 This link that I've created to pass the variable isn't working as the student id or email is suppose to be passed into the link. 我创建的用于传递变量的链接无法正常工作,因为学生ID或电子邮件应该传递给该链接。 This is the relative code: 这是相对代码:

echo "<table border=1><th colspan=7>Placed Orders</th></tr><tr><td>Student ID or Email</td><td>Name</td><td>Phone Number</td><td>Room Number</td><td>Order</td><td>Approval</td><td>&nbsp;</td></tr>";
while ($row=mysql_fetch_array($result)) {
    $id = $row["StudentIDorEmail"];
    $name = $row["Name"];
    $phone = $row["Phone"];
    $roomnumber = $row["RoomNumber"];
    $order = $row["Orders"];
    $approval = $row["Approved"];
    echo "<tr><td align='center'>" . $id . "</td><td align='center'>" . $name . "</td><td align='center'>" . $phone . "</td><td align='center'>" . $roomnumber . "</td><td align='center'>" . $order . "</td><td align='center'>" . $approval . "</td><td align='center'><a href='approve.php?id=".$id."'>Fill Order</a></td></tr>";
}
echo "</table>";

I'm trying to pass the variable to the approve.php page so that I can make that row get set to 1 and then when it changes to one, it gets deleted from the database. 我试图将变量传递到approve.php页面,以便可以将该行设置为1,然后将其更改为1时,将其从数据库中删除。 It's been causing me to keep erroring and I don't know what is wrong. 这一直导致我不断出错,而且我不知道出什么问题了。 Here is the other part of relative code for the page that it's suppose to be passed to and from: 这是应该传递给页面的相对代码的另一部分:

$id = $_GET["StudentIDorEmail"];
$query = "UPDATE tracker WHERE StudentIDorEmail=$id";
if (mysql_query($query, $con)) {
$query1 = "DELETE FROM tracker WHERE Approved=1";
if(mysql_query($query1, $con)) {
    echo "Order was filled successfully!";
} else {
    echo "Error: " . mysql_error();
}

} }

Please help because this is the requirements for my exam and I've almost finished it. 请帮忙,因为这是我考试的要求,我已经快完成了。

Your link is 'approve.php?id=".$id."' 您的链接是'approve.php?id=".$id."'

This means you need to access $_GET['id'] , not $_GET['StudentIDorEmail'] . 这意味着您需要访问$_GET['id'] ,而不是$_GET['StudentIDorEmail']

For future reference, try var_dump($_GET) to see what variables are there. 为了将来参考,请尝试var_dump($_GET)看看那里有什么变量。

EDIT: You should learn about SQL injection. 编辑:您应该了解有关SQL注入。

XKCD

In this case, you can use $id = intval($_GET['id']) to ensure you only get numbers. 在这种情况下,可以使用$id = intval($_GET['id'])来确保仅获取数字。

This is really easy, 真的很简单

<a href='approve.php?id=".$id."'>

Change this to: 更改为:

<a href='approve.php?StudentIDorEmail=".$id."'>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM