Can't insert data via PDO using associative array

93 Views Asked by At

That's how I'm trying to do it:

database.php:

<?php

    $host = "host";
    $user = "user";
    $pass = "password";
    $dbname = "database";

    try {
        # MySQL with PDO_MYSQL
        $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
        $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

    }
    catch(PDOException $e) {
        echo $e->getMessage();
    }

    ?>

file that must insert data:

<?php 
include 'database.php'; 
$_POST['name'] = 'test';
$_POST['message'] = 'test';
$_POST['date'] = 'test';

var_dump($_POST);

if(isset($_POST['name']) && isset($_POST['message'])){
    $data = array( 
        'name' => $_POST['name'], 
        'shout' => $_POST['message'], 
        'date' => $_POST['date']
    );

    $STH->bindParam(':name', $_POST['name']);
    $STH->bindParam(':message', $_POST['message']);
    $STH->bindParam(':date', $_POST['date']);

    try {
        $STH = $DBH->prepare("INSERT INTO shouts (name, message, date) value (:name, :message, :date)");
        $STH->execute($data);
    }
    catch(PDOException $e) {
        echo $e->getMessage();
    }
}

?>

According to this http://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059 it should accept associative array. But instead it does nothing and says that I have an error in this line:

$STH = $DBH->("INSERT INTO shouts (name, message, date) value (:name, :message, :date)");

What can be causing it, and how I can actually use an associative array to insert data into MySQL database using PDO?

1

There are 1 best solutions below

2
On BEST ANSWER

Hmmmm... There are some flaws I can see:

database.php

$host = "us-cdbrbababababableardb.net";
$user = "b9bababababefc";
$pass = "9c4ababab";
$dbname = "heroku_aabababab49";
$DBH = null;

try {
    # MySQL with PDO_MYSQL
    $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
    $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

}
catch(PDOException $e) {
    echo $e->getMessage();
}

?>

file that must insert data

<?php 
include 'database.php'; 
$_POST['name'] = 'test';
$_POST['message'] = 'test';
$_POST['date'] = 'test';

var_dump($_POST);

if(isset($_POST['name']) && isset($_POST['message'])){
    $data = array( 
        'name' => $_POST['name'], 
        'shout' => $_POST['message'], 
        'date' => $_POST['date']
    );

    try {
        // NOTICE: prepare() used and VALUES instead of VALUE
        $STH = $DBH->prepare("INSERT INTO shouts (name, message, date) values (:name, :message, :date)");

        $STH->bindParam(':name', $_POST['name']);
        $STH->bindParam(':message', $_POST['message']);
        $STH->bindParam(':date', $_POST['date']);

        // NOTICE: $data has allready been supplied by bindParam()
        $STH->execute();
    }
    catch(PDOException $e) {
        echo $e->getMessage();
    }
}

?>

These are just the first few things I could find wrong with the code... Try reading up on the PDO tutorials and functions for better info on what they would need as input.