I am new with PDO and I am trying to insert data into a table.
This is the table I have:
CREATE TABLE `Message` (
`ID` INT( 8 ) NOT NULL AUTO_INCREMENT ,
`DateTime` DATETIME NOT NULL ,
`SmsSid` VARCHAR( 34 ) NOT NULL ,
`AccountSid` VARCHAR( 34 ) NOT NULL ,
`From` VARCHAR( 12 ) NOT NULL ,
`To` VARCHAR( 12 ) NOT NULL ,
`Body` VARCHAR( 160 ) NOT NULL ,
`FromCity` VARCHAR( 50 ) NULL ,
`FromState` VARCHAR( 50 ) NULL ,
`FromZip` VARCHAR( 50 ) NULL ,
`FromCountry` VARCHAR( 50 ) NULL ,
`ToCity` VARCHAR( 50 ) NULL ,
`ToState` VARCHAR( 50 ) NULL ,
`ToZip` VARCHAR( 50 ) NULL ,
`ToCountry` VARCHAR( 50 ) NULL ,
`ConversationNumber` INT( 4 ) NOT NULL ,
PRIMARY KEY ( `ID` )
) ENGINE = MYISAM
This is the PHP Code where I'm trying to insert into the table. What is the proper way to do this insert statement? prepare() query() exec()?
I am able to insert into a less complex table by using this similar code but not this one above. I'm unsure what exactly is causing the problem, syntax, column types, auto increment, datetime? Is there something with this table that I'm not handling right in the insert query? Also am I handling exceptions/error handling correctly to see the proper error messages I'd need to help debug?
<?php
try
{
$connectionString = new PDO("mysql:host=xxxx;dbname=xxxx;","xxxx","xxxx");
}
catch(PDOException $e)
{
echo 'Connection failed'.$e->getMessage();
}
$DateTime = "NOW()";
$SmsSid = "abcdef";
$AccountSid = "abcdef";
$FromWho = "abcdef";
$To = "abcdef";
$Body = "abcdef";
$FromCity = "abcdef";
$FromState = "abcdef";
$FromZip = "abcdef";
$FromCountry = "abcdef";
$ToCity = "abcdef";
$ToState = "abcdef";
$ToZip = "abcdef";
$ToCountry = "abcdef";
$ConversationNumber = "abcdef";
try
{
$executeQuery = $connectionString->prepare("INSERT INTO Message (SmsSid,AccountSid,`From`,To,Body,FromCity,FromState,FromZip,FromCountry,ToCity,ToState,ToZip,ToCountry,ConversationNumber) VALUES (:SmsSid,:AccountSid,:FromWho,:To,:Body,:FromCity,:FromState,:FromZip,:FromCountry,:ToCity,:ToState,:ToZip,:ToCountry,:ConversationNumber)");
$executeQuery->execute(array(':SmsSid'=>$SmsSid,':AccountSid'=>$AccountSid,':FromWho'=>$FromWho,':To'=>$To,':Body'=>$Body,':FromCity'=>$FromCity,':FromState'=>$FromState,':FromZip'=>$FromZip,':FromCountry'=>$FromCountry,':ToCity'=>$ToCity,':ToState'=>$ToState,':ToZip'=>$ToZip,':ToCountry'=>$ToCountry,':ConversationNumber'=>$ConversationNumber));
}
catch(PDOException $e)
{
echo 'Query failed'.$e->getMessage();
}
$connectionString = null;
?>