I am testing a form and it gives me that error on line 21 that says:
$stat1 = $db->prepare("insert into about values(?,?,?,?,?)");
The code is like this:
<?php
$host = 'localhost';
$db = 'form';
$user = 'root';
$dbpass = '';
if(isset($_POST['save'])){
$id = uniqid();
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$username = $_POST['username'];
$password = md5($_POST['password']);
$title = $_POST['title'];
$description = $_POST['description'];
$sites = $_POST['sites'];
$category = $_POST['category'];
$stat1 = $db->prepare("insert into about values(?,?,?,?,?)");
$stat1->bindParam(1, $id);
$stat1->bindParam(2, $name);
$stat1->bindParam(3, $email);
$stat1->bindParam(4, $phone);
$stat1->bindParam(5, $address);
$stat1->execute();
$stat2 = $db->prepare("insert into account values(?,?,?)");
$stat2->bindParam(1, $id);
$stat2->bindParam(2, $username);
$stat2->bindParam(3, $password);
$stat2->execute();
$stat3 = $db->prepare("insert into website values(?,?,?,?,?)");
$stat3->bindParam(1, $id);
$stat3->bindParam(2, $title);
$stat3->bindParam(3, $description);
$stat3->bindParam(4, $sites);
$stat3->bindParam(5, $category);
$stat3->execute();
header('Location: save.php');
}
?>
I have PHP 7 and I test it on localhost
The problem is that you're not instantiating the connection.
Try doing it like this.
I have put two configurations that will save you a lot of headaches:
$charset
to avoid strange character problems$arrOptions
with the configuration of two options that will allow: a correct handling of errors (without this, in some cases your password could be revealed in the error log); avoid emulated prepares, which might allow SQL injection via emulated prepared queries.This would be the connection code:
Or like this:
In either case, you will have
$db
an instance of the correctly configured connection.