I am having issues trying to insert information into the database. When I run the database connection in a different file it works fine on its own. But when I do the ->prepare
it writes the PHP, starting from that point, out to the screen. Then I moved my PDO connection to the register.php file. That didn't help either. The only thing that changed is that now it writes out the PHP to screen from ->setAttribute
onwards.
Here is the PHP code I use in register.php:
<?php
//include '../database_connect/connect.php';
PDO::getAvailableDrivers();
$db = new PDO('mysql:host=127.0.0.1;dbname=local_db', 'root', '');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$dob = $_POST['dob'];
//$_POST['day'] . $_POST['month'] . $_POST['year'];
//$final_dob = date('d-m-Y', strtotime($dob));
try {
$sql_query = 'INSERT INTO user_data (username, password, email, first_name, last_name) VALUES (?,?,?,?,?)';
$query = $db->prepare($sql_query);
} catch (PDOException $e)
echo $e->getMessage();
?>
This is what I used in the commented connect.php and it didn't write PHP out to the browser.
<?php
// Get available driver, check if it's installed
//print_r(PDO::getAvailableDrivers());
$db = new PDO('mysql:host=127.0.0.1;dbname=local_db', 'root', '');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
/*try {
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo $e->getMessage();
} */
?>
I know that the password is not hashed, but this problem hit me before starting to work that out.