I have this error when I am trying to insert data into a database:
Fatal error: Call to a member function prepare() on a non-object in /home/cabox/workspace/views/logged_in.php on line 10
Here is my code:-
<?php
include "db_connect.php";
if(isset($_POST['submit'])){
$title = $_POST['title'];
$content = $_POST['content'];
$sql = "INSERT INTO `posts`(`title`, `content`) VALUES (:title,:content)";
$query = $db->prepare($sql);
$query->execute(array(
':title' => $title,
':content' => $content,
));
}
?>
And here is my database connection:
<?php
$username = "root";
$password = "";
$host = "localhost";
$db = mysql_connect($host, $username, $password)
or die("Unable to connect to MySQL");
?>
try use
global $db;
after include if you already create the database connection on file db_connect.php for php version > 5 .use PDO or mysqli