Apparently I don’t know how to insert a row into a MySQL data base using PDP and I would like to know what I am missing
This PHP PDO script does not insert data into a database. I would like to know why. It does not error out but the data base is not populated and the return value is apparently undefined.
Below is the table:
CREATE TABLE animals (
Id INT(11) NOT NULL AUTO_INCREMENT,
animal_type VARCHAR(32) DEFAULT NULL,
animal_name VARCHAR(32) DEFAULT NULL,
PRIMARY KEY (`Id`)
) ENGINE=MYISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;
Below is the PHP script:
<?php
/* mysql hostname */
$hostname = 'localhost';
/* mysql username */
$username = 'root';
/* mysql password */
$password = 'secret';
/* mysql database name */
$dbname = 'site';
try {
$dbh = new PDO("mysql:host=$hostname;$dbname", $username, $password);
/* echo a message saying we have connected */
echo ('Connected to database <br />');
} catch (PDOException $e) {
echo $e->getMessage();
echo ('<br />');
}
echo ('<br />');
try {
/* INSERT data */
$count = 0;
$count = $dbh->exec("INSERT INTO animals(animal_type, animal_name) VALUES ('kiwi' 'roy'");
/* echo the number of affected rows */
echo ("Number of rows inserted = " . $count . '<br />');
/* close the database connection */
$dbh = null;
} catch (PDOException $e) {
echo $e->getMessage();
echo ('<br />');
}
Your insight would be appreciated.