我想用PDO插入到MySQL表中。 代碼:PDO插入成功但不是?
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 @[email protected]@CHARACTER_SET_CLIENT */;
/*!40101 SET @[email protected]@CHARACTER_SET_RESULTS */;
/*!40101 SET @[email protected]@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 [email protected]_CHARACTER_SET_CLIENT */;
/*!40101 SET [email protected]_CHARACTER_SET_RESULTS */;
/*!40101 SET [email protected]_COLLATION_CONNECTION */;
'00000' 顯然意味着成功,但它不應該被打印,如果它是成功的。數據庫中沒有插入任何內容。我確定這是一個錯字,但我找不到它。
SQL語句:
INSERT INTO inbox_unread (date, body, to, from) VALUES ('2013-01-04 23:40:58', 'test', 'will', 'will')
您是否嘗試使用try/catch塊來捕獲真正的錯誤而不是if/else?在你的代碼的最後部分? – inhan
或者只是用'PDO :: ERRMODE_WARNING'啓用正常的錯誤報告。 – mario
try/catch完全失敗。當我使用它時沒有任何反應。 – will