I made this PHP page to let people follow each other it takes the following user id from the table users and when the logged-in user presses follow, the action should be saved in followers table.
followers table
id int
user_id int
follower-id int
users table
email varchar
id int
username varchar
password varchar
I am getting this error
:Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'follower' in 'field list'' in C:\socialnetwork\classes\DB.php:12 Stack trace: #0 C:socialnetwork\classes\DB.php(12): PDOStatement->execute(Array) #1 C:\Users\Karim\Desktop\socialnetwork\profile.php(17): DB::query('SELECT follower...', Array) #2 {main} thrown in C:socialnetwork\classes\DB.php on line 12
Thank you for your time.
<?php
include('./classes/DB.php');
include('./classes/Login.php');
$username = "";
if (isset($_GET['username'])) {
if (DB::query('SELECT username FROM users WHERE username=:username', array(':username'=>$_GET['username']))) {
$username = DB::query('SELECT username FROM users WHERE username=:username', array(':username'=>$_GET['username']))[0]['username'];
if (isset($_POST['follow'])) {
$userid = DB::query('SELECT id FROM users WHERE username=:username', array(':username'=>$_GET['username']))[0]['id'];
$followerid = Login::isLoggedIn();
if (!DB::query('SELECT follower-id FROM `followers` WHERE user_id=:user_id', array(':user_id'=>$userid))) {
DB::query('INSERT INTO followers VALUES (null, :user_id, :follower-id)', array(':user_id'=>$userid, ':follower-id'=>(int)$followerid));
} else {
echo 'Already following!';
}
}
} else {
die('User not found!');
}
}
?>
<h1><?php echo $username; ?>'s Profile</h1>
<form action="profile.php?username=<?php echo $username; ?>" method="post">
<input type="submit" name="follow" value="Follow">
</form>
Hyphens, even though not disallowed, can cause problems in column names.
In order to make it work you have to enclose the name of the column with back-ticks:
SELECT `follower-id` FROM
As MySQL documentation confirms:
Permitted characters in unquoted identifiers:
ASCII: [0-9,a-z,A-Z$_] (basic Latin letters, digits 0-9, dollar, underscore)
Extended: U+0080 .. U+FFFF
Permitted characters in quoted identifiers include the full Unicode Basic Multilingual Plane (BMP), except U+0000:
ASCII: U+0001 .. U+007F
Extended: U+0080 .. U+FFFF
ASCII code for dash -
is 45, which is within the "quoted identifiers" ASCII range