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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)