dongyan0629 2015-04-29 11:00
浏览 32
已采纳

两个错误可能与Php PDO有关

I am new to PDO so be gentle. I have created a connection to my DB wanting to pull users from my table (which works) and have the ability to click on that user and take them to a page based on the users ID (which works) but when you visit that page you are presented with the following errors:

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

Here are my scripts:

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 (this is the single view)

<?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条回答 默认 最新

  • dou4064 2015-04-29 11:04
    关注

    you're using users.php?id then after using the GET array $_GET['user_id'].

    both should match.

    either you do users.php?user_id or change the array to $_GET['id'].

    also make sure that the array is an int including your column type.

    Otherwise change your query to

    ("select * from users where user_id = '$user'");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding