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 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应