我是 PDO 的新手,我正在尝试将数据插入表中。
这是我的表:
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
这是我试图插入表格的 PHP 代码。执行此插入语句的正确方法是什么?准备()查询()执行()?
我可以通过使用这个类似的代码而不是上面的这个代码来插入一个不太复杂的表。我不确定究竟是什么导致了问题、语法、列类型、自动增量、日期时间?这个表有什么我没有在插入查询中处理的吗?我是否也正确处理异常/错误处理以查看我需要帮助调试的正确错误消息?
<?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;
?>