两个错误可能与Php PDO有关

时间:2015-04-29 11:00:26

标签: php pdo

我是PDO的新手,所以要温柔。我已经创建了一个与我的数据库的连接,希望从我的表(可以工作)中提取用户,并且能够点击该用户并根据用户ID将其带到页面(这有效)但是当您访问该页面时出现以下错误:

Notice: Undefined variable: user_id in C:\MAMP\htdocs\dashboardR v2.0\users.php on line 10

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

以下是我的脚本:

userList.php

<?php require_once '../db_con.php'; 

try{
    $results = $db->query('SELECT * FROM users');

}catch(Exception $e) {
    echo $e->getMessage();
    die();
}

$users = $results->fetchAll(PDO::FETCH_ASSOC);

?>

<ol>
<?php
    foreach($users as $user){
        echo 
        '<li>
        <a href="users.php?id='.$user["user_id"].'">'.$user["emailAddress"].'</a>
        ' .$user["firstname"].'
        ' .$user["lastname"].'
        </li>';
    }

?>
</ol>

users.php(这是单一视图)

<?php require_once '../db_con.php'; 

if(!empty($_GET['user_id'])){
    $user = $_GET['user_id'];
}

try{
    $results = $db->query('select * from users where user_id = '.$user);

} catch(Exception $e) {
    echo $e->getMessage();
    die();
}

$user = $results->fetch(PDO::FETCH_ASSOC);

?>

<h3><i class="fa fa-user"></i></span> <?php echo $user['firstname'] . ' ' . $user['lastname']; ?>
    <a href="newMember.php"><span class="newUserBtn" title="Add New User"><i class="fa fa-user-plus"></i></span></a>
</h3>  

1 个答案:

答案 0 :(得分:2)

您在使用GET数组users.php?id后使用$_GET['user_id']

两者都应匹配。

您执行users.php?user_id或将数组更改为$_GET['id']

还要确保数组是int,包括您的列类型。

否则将您的查询更改为

("select * from users where user_id = '$user'");