I am working on a project where I need to insert data into a database, but I keep getting this error: Fatal error: Uncaught PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column '$name' in 'field list' in /opt/lampp/htdocs/ramen.php:27 Stack trace: #0 /opt/lampp/htdocs/ramen.php(27): PDOStatement->execute(Array) #1 {main} thrown in /opt/lampp/htdocs/ramen.php on line 27
here is line 27:
$stmt-> execute(array(':content' => $_POST['content']));
Here is that section of code:
if(isset($_POST['content'])){
$name = $_SESSION['name'];
echo $name;
$sql = 'INSERT INTO notes_ (Text, User) VALUES (:content, $name);';
$stmt = $pdo->prepare($sql);
$stmt->execute(array(':content' => $_POST['content']));
}
Change your quotes from single to double. You can inject your variable into double quoted strings.
if(isset($_POST['content'])){
$name = $_SESSION['name'];
echo $name;
$sql = "INSERT INTO notes_ (Text, User) VALUES (:content, $name);";
$stmt = $pdo->prepare($sql);
$stmt->execute(array(':content' => $_POST['content']));
}
But it could be more better if
if(isset($_POST['content'])){
$name = $_SESSION['name'];
echo $name;
$sql = 'INSERT INTO notes_ (Text, User) VALUES (:content, :name);';
$stmt = $pdo->prepare($sql);
$stmt->execute(array(':content' => $_POST['content'], ':name' => $name));
}