This should work
[php]$stmt = $con->prepare(“INSERT INTO form_corpo_test VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)”);
if (!$stmt) {
$error = $stmt->errorInfo();
echo 'PDO error: ’ . $error[2] . ‘(’ . $stmt->errorCode() . ‘)’;
} else {
$stmt->execute(array(
$_POST[‘company’], $_POST[‘phone’], $_POST[‘website’], $_POST[‘messagefr’], $_POST[‘messageen’], $str, $_POST[‘profession’], $_POST[‘manufacturiers_stand’], $_POST[‘percent_quebec’], $_POST[‘percent_canada’], $_POST[‘percent_usa’], $_POST[‘percent_autre’], $_POST[‘bt_export’], $_POST[‘bt_export_souhaite’], $_POST[‘bt_prod_verts’], $_POST[‘bt_new_prod’], $_POST[‘name’], $_POST[‘email’], $_POST[‘resource_phone’], $_POST[‘personne_ressource’], $_POST[‘backup_name’], $_POST[‘backup_email’], $_POST[‘backup_phone’]
));
if (!$stmt) {
$error = $stmt->errorInfo();
echo 'PDO error: ’ . $error[2] . ‘(’ . $stmt->errorCode() . ‘)’;
} else {
echo 'Insert OK';
}
}[/php]
Notice how messy it is though. I would suggest you consider using a wrapper class for the DB.
Save this as db.php
[php] <?php
class DB {
/**
*
* PDO connection
* @var PDO
*/
private $pdoConn = null;
/**
* Default charset
*
* @var string
*/
private $charset = 'utf8';
/**
* Class constructor
*/
public function __construct() {
$this->_initDb();
}
/**
* Get PDO database connection
*
* @return
*/
public function getPDOConn() {
return $this->pdoConn;
}
/**
* Init db connection based on config
*/
private function _initDb() {
try {
$this->pdoConn = new \PDO('mysql:dbname=' . DB_NAME . ';host=' . DB_HOST . ';charset=' . $this->charset, DB_USER, DB_PASS);
$this->pdoConn->exec("set names utf8");
$this->pdoConn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$this->pdoConn->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
} catch (PDOException $e) {
// Here you should do some error handling
$errormessage = "Database error occured at: " . strftime("%Y-%m-%d %H:%M:%S") . "\">\n";
$errormessage .= "[DB] _initDb: could not get PDO connection: " . $e->getMessage() . "\n";
echo '<pre>' . $errormessage . '</pre>';
return;
}
}
/**
* Executes parametarized query
* @param string $query
* @param array $params
* @param string $fetch_method
*/
public function query($query, $params = [], $fetch_method = 'OBJ') {
$stmt = $this->pdoConn->prepare($query);
try {
$result = $stmt->execute($params);
} catch (PDOException $e) {
// Here you should do some error handling
$errormessage = "\tDatabase error occured at: " . strftime("%Y-%m-%d %H:%M:%S") . "\">\n";
$errormessage .= "\tQuery: " . $query . "\n";
$errormessage .= "\tError code: " . $e->getCode() . "\n";
$errormessage .= "\tError message: " . $e->getMessage() . "\n";
echo '<pre>' . $errormessage . '</pre>';
}
if ($result) {
$querybit = explode(" ", $query);
if (trim($querybit[0]) == 'SELECT') {
$ret = $stmt->fetchAll(constant('PDO::FETCH_' . strtoupper($fetch_method)));
} else {
return array(TRUE);
}
}
return !empty($ret) ? $ret : null;
}
/**
* Get last inserted id
*
* @return integer
*/
public function getLastInsertedId() {
return $this->pdoConn->lastInsertId();
}
/**
* Wrapper for mysql_real_escape_string
*
* @param string $string
* @return string
*/
protected function _escape($string) {
return mysql_real_escape_string($string);
}
}[/php]
Then you can use your db like this:
[php]<?php
define(‘DB_HOST’, ‘localhost’);
define(‘DB_USER’, ‘test’);
define(‘DB_PASS’, ‘test’);
define(‘DB_NAME’, ‘test’);
require_once (‘db.php’);
$db = new DB();
$db->query(‘INSERT INTO form_corpo_test VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)’, array(
$_POST[‘company’], $_POST[‘phone’], $_POST[‘website’], $_POST[‘messagefr’], $_POST[‘messageen’], $str, $_POST[‘profession’], $_POST[‘manufacturiers_stand’], $_POST[‘percent_quebec’], $_POST[‘percent_canada’], $_POST[‘percent_usa’], $_POST[‘percent_autre’], $_POST[‘bt_export’], $_POST[‘bt_export_souhaite’], $_POST[‘bt_prod_verts’], $_POST[‘bt_new_prod’], $_POST[‘name’], $_POST[‘email’], $_POST[‘resource_phone’], $_POST[‘personne_ressource’], $_POST[‘backup_name’], $_POST[‘backup_email’], $_POST[‘backup_phone’]
));[/php]