I'm working on a university project using MySQL and PHP, a number of errors have been occurring. The first of which was being unable to connect to the database which I've resolved by inputting the database's name. This is supposed to be a registration form for a website. The data is now not being stored in the database, but I cannot see what is causing this - or there is another error occurring which I am unaware of. Connecting to the database:
<?php
$db = new mysqli('(database name)', 'localhost', 'root', '', '09');
if($db->connect_errno > 0) {
die('Unable to connect to database [' . $db->connect_error . ']');
}
?>
Registration:
require_once '../../includes/db-connect.php';
//get data from POST
$fname = filter_var($_POST['fname']);
$lname = filter_var($_POST['lname']);
$dob = filter_var($_POST['dob']);
$email = filter_var($_POST['email']);
$password = filter_var($_POST['password']);
$confirmpassword = filter_var($_POST['confirmPassword']);
$sql = 'INSERT INTO users ' .
'(`fname`, `lname`, `dob`, `email`, `password`)' .
'VALUES ' .
'("' . $db->escape_string($fname) .
'", "' . $db->escape_string($lname) .
'", "' . $db->escape_string($dob) .
'", "' . $db->escape_string($email) .
'", "' . $db->escape_string($password) .'")';
$result = mysqli_query($db, $sql);
//if the user is successfully entered into the table, redirect to login page
if($result) {
header('Location: /ct4009_2/user/login/login.php');
die();
}
die('There was an error inserting user data into the database');
?>
Finally, the sql query I run to propagate database tables:
CREATE TABLE `users` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`email` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`profilePic` varchar(255) NOT NULL,
`fname` varchar(255) NOT NULL,
`lname` varchar(255) NOT NULL,
`dob` date NOT NULL,
`deleted` INT DEFAULT 0
);
CREATE TABLE `posts` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`poster_id` INT NOT NULL,
`date` varchar(255) NOT NULL,
`post_body` varchar(1000) NOT NULL,
`img_path` varchar(255),
`comments` varchar(1000),
`likes` varchar(1000),
`deleted` INT DEFAULT 0
);