I'm trying to get the hang of PDO and I can't seem to get this to work. I'm tyring to insert some data into a databse, but nothing shows up when I click submit. What am I doing wrong?

<?php
require 'connect.php';

if (isset($_POST['username'], $_POST['password'])) {
$username = $_POST['username'];
$password = $_POST['password'];

$insert = "INSERT INTO `register` (`username`, `password`) VALUES(:username, :password)";

if (!empty($username) && !empty($password)) {
$query_run = $db->prepare($insert);
$query_run->execute( array(':username' => $username, ':password' => $password) );
} else {
    echo 'Error';
}

}
?>

<form action="index.php" method="POST">
    <input type="text" name="username"/>
    <input type="password" name="password"/>
    <input type="submit" value="Submit"/>
</form>
Member Avatar for diafol

try:

if (isset($_POST['username']) && isset($_POST['password'])) {

Still getting the problem. Is there something wrong with my query?

Member Avatar for diafol

Looks ok to me.

Member Avatar for LastMitch

I'm trying to get the hang of PDO and I can't seem to get this to work. I'm tyring to insert some data into a databse, but nothing shows up when I click submit. What am I doing wrong?

Are you connected to the DB with using PDO?

Read and try this:

http://www.daniweb.com/web-development/php/code/435142/using-phppdo-with-error-checking

Instead of this:

<?php
require 'connect.php';
if (isset($_POST['username'], $_POST['password'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$insert = "INSERT INTO `register` (`username`, `password`) VALUES(:username, :password)";
if (!empty($username) && !empty($password)) {
$query_run = $db->prepare($insert);
$query_run->execute( array(':username' => $username, ':password' => $password) );
} else {
echo 'Error';
}
}
?>

Try this I change it a little:

<?php
require 'connect.php';
if (isset($_POST['username'], $_POST['password'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$insert = "INSERT INTO register (username, password) VALUES ('username', 'password')";
if (!empty($username) && !empty($password)) {
$query_run = $db->prepare($insert);
$query_run->execute();
} else {
echo 'Error';
}
}
?>

This is what my connect.php page looks like:

<?php
$db = new PDO('mysql:host = localhost; dbname = test', 'root', '');
if($db) {
    echo 'Connected';
}
?>

This is that my db looks like:
http://i.imgur.com/ui6SV.png

I am still getting the same problem after changing the code.

Member Avatar for LastMitch

@NoUserNameHere

I am still getting the same problem after changing the code.

Instead of this:

<?php
$db = new PDO('mysql:host = localhost; dbname = test', 'root', '');
if($db) {
echo 'Connected';
}
?>

Try this (I change the connection a little):

<?php
try { 
# MySQL with PDO_MYSQL  
$db = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);  
}
catch(PDOException $e) {  
echo $e->getMessage();  
} 
?>

Ah, that worked! Are double quotes required for mysql:host=$host;dbname=$dbname?

Thank you.

Member Avatar for LastMitch

Ah, that worked! Are double quotes required for mysql:host=$host;dbname=$dbname?

No, it's works single or double qoutes.

I think it must be the space issue.

You have your db like this

$db = new PDO('mysql:host = localhost; dbname = test', 'root', '');

I figure it should closer like this:

$db = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass); 
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.