PHP - 通过url id识别用户

时间:2015-11-04 12:20:27

标签: php mysql

我有3个按钮来排名我的用户。 AdminMember以及禁止它们的按钮。所以现在我想用$_GET的“url-id”来识别它们。但是当我这样做时,我得到一个错误:

  

解析错误:语法错误,意外''(T_ENCAPSED_AND_WHITESPACE),   期待标识符(T_STRING)或变量(T_VARIABLE)或数字   (T_NUM_STRING)。

我该如何解决?我感谢你的一切帮助!

<?php
 include_once('connection.php');
 if (isset($_POST['ban'])) { 
  $sql = "UPDATE
          t_user_info
        SET
          user_level = 3
        WHERE 
          id = $_GET['id']";  

$query = $conn->prepare($sql);
$query ->execute(array('user_level' => $user_level));

session_unset();
session_destroy();
header('Location: /PHP/index.php?page=ban');
}
if (isset($_POST['admin_btn'])) { 
 $sql = "UPDATE
          t_user_info
        SET
          user_level  = 1
        WHERE 
          id       = $_GET['id']";  

$query = $conn->prepare($sql);
$query ->execute();
header('Location: /PHP/index.php?page=admin');

}

if (isset($_POST['member_btn'])) { 
$sql = "UPDATE
          t_user_info
        SET
          user_level  = 2
        WHERE 
          id       = $_GET['id']";   

$query = $conn->prepare($sql);
$query ->execute();
header('Location: /PHP/index.php?page=member');

}

?>

1 个答案:

答案 0 :(得分:0)

您的更新查询存在语法错误,并且您没有绑定正确的值。试试这个:

<?php
include_once('connection.php');
if (isset($_POST['ban'])) { 
    $sql = "UPDATE
            t_user_info
            SET user_level = 3
            WHERE id = :id";

    $query = $conn->prepare($sql);
    $query ->execute(array(':id'=>$_GET['id']));

    session_unset();
    session_destroy();
    header('Location: /PHP/index.php?page=ban');
    exit;
}

if (isset($_POST['admin_btn'])) { 
    $sql = "UPDATE
            t_user_info
            SET user_level  = 1
            WHERE id = :id";

    $query = $conn->prepare($sql);
    $query ->execute(array(':id'=>$_GET['id']));
    header('Location: /PHP/index.php?page=admin');
    exit;
}

if (isset($_POST['member_btn'])) { 
    $sql = "UPDATE t_user_info
            SET user_level  = 2 
            WHERE id = :id";

    $query = $conn->prepare($sql);
    $query ->execute(array(':id'=>$_GET['id']));
    header('Location: /PHP/index.php?page=member');
    exit;
}

?>