gpt4 book ai didi

PHP PDO 插入数据

转载 作者:太空宇宙 更新时间:2023-11-03 12:31:37 30 4
gpt4 key购买 nike

<分区>

我是 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;
?>

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com