0

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>

Edited by NoUserNameHere

3
Contributors
8
Replies
11
Views
4 Years
Discussion Span
Last Post by LastMitch
0

try:

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

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';
}
}
?>

Edited by LastMitch: grammer

0

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.

Edited by NoUserNameHere

0

@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();  
} 
?>

Edited by LastMitch: grammer

0

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

Thank you.

Edited by NoUserNameHere

0

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); 
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.