0

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?

4
  • Enable error_reporting. That $DBH->(…) syntax is invalid. And you can't bind parameters before $STH is declared. Check the manual php.net/manual/en/pdo.prepare.php for proper examples. Commented Jun 10, 2015 at 12:10
  • I've added error_reporting(E_ALL); and it still shows nothing in the page. Why $DBH->(...) is invalid? Commented Jun 10, 2015 at 12:12
  • Don't include your username and password on forums. Commented Jun 10, 2015 at 12:13
  • @chris85 they are not real, those are just placeholders Commented Jun 10, 2015 at 12:13

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. I'm currently switching from mysqli to pdo and was reading the tutorial i posted in my question, guess it's not the best one out there.
To be honest, I always have a nauseating feeling creeping up on me when people are using all-uppercase variable names and all-lowercase variable names mixed in one codebase. Try reading the documentation on the php.net pages first: link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.