从URL获取id; PHP,MySQL

时间:2014-01-13 22:08:50

标签: php mysql

嗨,我一直很好奇如何获取像php这样的URL中的id?pid = 4并在sql的更新语句中使用它。嗯,继承我的代码,但由于未定义的变量id,该值在url中,因此无法使其工作。

my function.php

function update_spot(){

$id=$_GET[pid];
if (isset($_POST['update'])){
$sql="UPDATE reports SET date_time_started='$_POST[date1]' ,
    date_time_finished=   '$_POST[date2]',
    barangay='$_POST[brgy]',
    street= '$_POST[street]',
    owner='$_POST[owner]', 
cause='$_POST[cause]',
motive='$_POST[motive]',
firfighter='$_POST[firefighter]'. 
civilian='$_POST[civilian]', 
ifirefighter='$_POST[ifirefighter]',
icivilian='$_POST[icivilian]',
occupancy='$_POST[occupancy]',
ed='$_POST[ed]',
alarm='$_POST[alarm]'

where id='".$id."' ";

 if (!mysql_query($sql)){ die('Error: ' . mysql_error()); } ?>
<script type='text/javascript'>alert('sucessful changed try it next time you log 

in.');window.location='view_inbox.php';</script>    <?php
}

}

似乎我无法在网址中获取ID。我的网址以php?pid = 5

的形式显示

4 个答案:

答案 0 :(得分:0)

应该只是

$id = $_GET['pid'];

答案 1 :(得分:0)

在$ _GET数组访问中使用引号

$pid = $_GET['pid'];

此外,您正在混合$ _GET和$ _POST。您应该使用其中一种,具体取决于您的表单方法(GET或POST)。

您还想要更改访问它的所有区域。 IE

barangay = '$_POST[brgy]';

这应该是,以及之后的所有其他行

barangay = $_POST['brgy'];

答案 2 :(得分:0)

$pid = isset($_GET['pid']) ? 0 : intval($_GET['pid']) //to avoid problems in this case

答案 3 :(得分:0)

@Up,在上面的示例中它仍然可以工作,但会导致php警告说未定义的php常量将被假定为字符串。

我会转储get global并查看变量是否存在,例如var_dump($ _ GET);