我全神贯注地寻找解决方案,却找不到任何解决方案。我检查了我的代码,似乎无法找到任何错误。
try
{
$handle = new PDO("mysql:dbname=" . DATABASE . ";host=" . SERVER, USERNAME, PASSWORD);
$handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$handle->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
catch (Exception $e)
{
trigger_error($e->getMessage(), E_USER_ERROR);
exit;
}
$sendData = $handle->prepare("INSERT INTO 'posts' (body, user, comments, likes, username, datetime) VALUES(:body, :userid, 'none', 'none', :username, :datetime)");
$sendData->bindParam(':body',$this->body);
$sendData->bindParam(':userid',$this->userID);
$sendData->bindParam(':username',$this->username);
$sendData->bindParam(':datetime',$this->datetime);
$sendData->execute();
我确定代码在它到达“bindParam”部分之前就停止了。它在准备电话后立即停止。
编辑:事实证明错误发生在$ handle部分。我在其他地方声明了句柄,并没有使用“global”将其添加到此函数中。我觉得很蠢。
答案 0 :(得分:0)
尝试:
try {
$db = new PDO('mysql:host=localhost;dbname=DATEBASE;charset=utf8', 'USER', 'PASSWORD');
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $ex) {
echo "something";
die();
}
$body = $_POST['body'];
$userID = $_POST['userID'];
$username = $_POST['username'];
$datetime = $_POST['datetime'];
$none = "none";
$sendData = $sb->prepare("INSERT INTO `posts` (body, user, comments, likes, username, datetime) VALUES(:body, :userid, :none, :none, :username, :datetime)");
$sendData->bindValue(':body', $body);
$sendData->bindValue(':none', $none);
$sendData->bindValue(':userid', $userID);
$sendData->bindValue(':username', $username);
$sendData->bidnValue(':datetime', $datetime);
$sendData->execute();
<强>编辑:强>
`posts` instead 'posts'