gpt4 book ai didi

php - PDO 插入成功但没有?

转载 作者:行者123 更新时间:2023-11-29 01:22:39 25 4
gpt4 key购买 nike

我正在尝试使用 PDO 插入到 MySQL 表中。代码:

public function sendMail($to, $body) {
/* this all works
*
error_reporting(E_ALL);
ini_set('display_errors', '1');
$stmt = $this->database->prepare("SELECT * FROM users");
$stmt->execute();
$fetch = $stmt->fetchAll();
print_r($fetch);
*/

$stmt = $this->database->prepare("INSERT INTO
inbox_unread (date, body, to, from) VALUES (:date, :body, :to, :from)");
echo $stmt->bindParam(":date", date("Y-m-d H:i:s"));
echo $stmt->bindParam(":body", $body);
echo $stmt->bindParam(":to", $to);
echo $stmt->bindParam(":from", $_SESSION['username']);
if ($stmt->execute()) {
echo "yes";
} else {
print_r($this->database->errorInfo());
}
}

这是我得到的输出:

1111Array ( [0] => 00000 [1] => [2] => )

数据库:

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `sandbox`
--

-- --------------------------------------------------------

--
-- Table structure for table `inbox_unread`
--

CREATE TABLE IF NOT EXISTS `inbox_unread` (
`mail_id` int(11) NOT NULL AUTO_INCREMENT,
`date` datetime NOT NULL,
`body` text NOT NULL,
`to` varchar(255) NOT NULL,
`from` varchar(255) NOT NULL,
PRIMARY KEY (`mail_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

'00000' 表面上是成功的意思,但是成功了应该不会打印。没有任何内容被插入到数据库中。我确定这是一个打字错误,但我找不到它。

SQL语句:

INSERT INTO inbox_unread (date, body, to, from) VALUES ('2013-01-04 23:40:58', 'test', 'will', 'will')

最佳答案

您的查询中有一个保留字:

   $stmt = $this->database->prepare("INSERT INTO 
inbox_unread (date, body, to, `from`) VALUES (:date, :body, :to, :from)");

Stackoverflow 语法高亮器实际上使它显而易见。对于这样的通用列名,最好将它们全部放在反引号中。

关于php - PDO 插入成功但没有?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14166433/

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