php - PDO 插入不起作用

标签 php mysql pdo

我不知道我做错了什么。它不工作。

我对这个很感兴趣。我想不通。

$sql="INSERT INTO hr_daily_claim(date,
                                 site,
                                 from,
                                 to,
                                 rental,
                                 vehicle,
                                 claim_id,
                                 parking,
                                 beverages,
                                 others) 

                          VALUES(:date,
                                 :site,
                                 :from,
                                 :to,
                                 :rental,
                                 :vehicle,
                                 :claim_id,
                                 :parking,
                                 :beverages,
                                 :others)";
$stmt = $db->prepare($sql);
$stmt->execute(array(
                     ':date'     => $date, 
                     ':site'     => $site, 
                     ':from'     => $from, 
                     ':to'       => $to,
                     ':rental'   => $rental,
                     ':vehicle'  => $vehicle,
                     ':claim_id' => $cliamId,
                     ':parking'  => $parking,
                     ':beverages'=> $beverages,
                     ':others'   => $others    ));

请有人帮助我。 它没有给我任何错误。但受影响的行数 = 0;根本不插入。

下面是表结构

 `id` int(11) NOT NULL AUTO_INCREMENT,
  `claim_id` varchar(45) DEFAULT NULL,
  `date` varchar(10) DEFAULT NULL,
  `site` varchar(45) DEFAULT NULL,
  `from` varchar(45) DEFAULT NULL,
  `to` varchar(45) DEFAULT NULL,
  `rental` int(11) DEFAULT NULL,
  `vehicle` int(11) DEFAULT NULL,
  `parking` int(11) DEFAULT NULL,
  `beverages` int(11) DEFAULT NULL,
  `others` int(11) DEFAULT NULL,
  `timestamp` timestamp NULL DEFAULT CURRENT_TIMESTAMP,

最佳答案

from, to 都是reserved words在 MySQL 中。你需要用反引号把它包起来。

$sql="INSERT INTO hr_daily_claim(
                `date`,
                `site`,
                `from`,                //<-------- This
                `to`,                  //<-------- This
                `rental`, 
                `vehicle`,
                `claim_id`,
                `parking`,
                `beverages`,
                `others`
                        ) 

旁注:':claim_id' => $cliamId, 中的 $cliamId 变量可能有拼写错误,应该读作 ':claim_id ' => $claimId,,所以一定要检查一下,因为这些变量没有发布在你的问题中。

如果一个失败,您的整个查询将失败。另外需要注意的是 $claimId$claimid 是不一样的。变量区分大小写。

关于php - PDO 插入不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22881089/

相关文章:

php - 如何通过 PDO 在 Mysql 查询中使用变量

php - 没有缓存的谷歌云存储公共(public)访问

mysql - 我如何查询这三个特定表以获取具有内部连接的信息?

php - 使用 PHP 将 unix 时间戳插入 mySQL 整数字段 (INT) 的最佳方法?

php - 使用 PHP PDO 插入表 - 卡在execute() 语句上

MySQL MATCH() AGAINST() 与 REGEXP 匹配整个单词

php - 检索Mysql数据失败...为什么?

php - 迁移后 Codeigniter session 不起作用

php - 我将如何组织数据以向用户发送许多电子邮件?

php - 将本地数据库更改为在线页面空白时