I know this type of question as probably been asked a couple of times, but they all have something to do with laravel, in my case this is vanilla php with no framework.
First of all change your connection like this: replace codes in Database.php with the following codes and read comments in codes.
$host = 'localhost';
$db = 'nairobi';
$user = 'admin';
$pass = '123456';
$charset = 'utf8mb4'; // Always set charset for database
$port = '3308'; //Your port can be 3306 or 3307
$dsn = "mysql:host=$host;dbname=$db;port=$port;charset=$charset";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try {
$dbh = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
Now your codes! you are using prepare statements which makes your codes safe enough.
Check if url to Database.php
file is correct and change yours to this
require_once "./library/Database.php";
if (isset($_POST['submit'])) {
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$reference_code = $_POST['reference_code'];
//You have a `TABLE` name call members so you dont need `TABLE` its causing problem
//Your query should look like this
$sql = 'INSERT INTO members (first_name, last_name, email, reference_code) VALUES (:first_name, :last_name, :email, :reference_code)';
$stmt = $dbh->prepare($sql);
$stmt->execute([$first_name, $last_name, $email, $reference_code]);
//I removed array here which you dont need, you can directly add fields in execute.
$stmt->closeCursor();
//Use `closeCursor()` to free your connection instead unset or close.
}
Final make sure you created a database. make sure you created a table call members in database.
if you have done that all, your codes will work without any problem.
HTML form should look like this, you need to add multi encrypt into form if you want to upload images.
Datatable should look like this :
CREATE TABLE IF NOT EXISTS `members` (
`member_id` int(8) NOT NULL AUTO_INCREMENT,
`first_name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`last_name` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL,
`email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`reference_code` int(11) NOT NULL,
PRIMARY KEY (`member_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;