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>