0

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.

Stefan
  • 43
  • 1
  • 8
  • It sounds like your webserver isn't configured correctly, so it's not running `register.php` as a PHP script, it's just treating it as HTML. So `->` is ending the ` – Barmar Mar 12 '15 at 08:12
  • I am running XAMPP, but the thing I do not understand is then why is the connect.php running without any trouble? And I also have a query that grabs information from the database and outputs it to the browser. Those are working fine, but this is not. What can I do to fix it? – Stefan Mar 12 '15 at 08:20
  • Are they in the same directory? If not, maybe there's a `.htaccess` file in the directory of `register.php` that's affecting it differently from `connect.php`. – Barmar Mar 12 '15 at 08:21
  • I moved the files to a different folder, still the same problem. Now the other files are getting PHP written out to the browser as well. And I couldn't find a .htaccess file in the folders. – Stefan Mar 12 '15 at 08:28
  • It's definitely something wrong with your server configuration. Unfortunately, that's not my area of expertise, which is programming, not server config. – Barmar Mar 12 '15 at 08:29
  • Are you sure your files have `.php` extensions and not `.php.txt` , because it happened to me once long ago, where I being silly upload a `.php.txt` file. – Nepal12 Mar 12 '15 at 08:43
  • http://stackoverflow.com/questions/9305680/apache-virtual-host-not-parsing-php might help you debugging. – Nepal12 Mar 12 '15 at 08:45
  • @Nepal12 Yes, I double checked and all the files are .php extension. – Stefan Mar 12 '15 at 08:48

1 Answers1

-1

I found the problem. I don't know the reason, but when I pressed the submit button in my index.php, it redirected me to E://{file_path} and not localhost/{file_path}. Now it is running okay. Thanks for everybody's help.

Stefan
  • 43
  • 1
  • 8