【发布时间】:2014-06-11 17:58:52
【问题描述】:
我被卡住了,我无法修复我遇到的这个错误,我认为可能是我的数据库选项搞砸了
这是将数据放入表单的地方
<form name = "quoted" method="post" onsubmit="get_action(this);">
<input id = "poster" type="text" name="poster" required="required" placeholder = "Credited Individual."> <br>
<textarea class = "actual_quote" name = "actual_quote" required="required" placeholder = "Write the question here!"></textarea><br><br><br>
<div class = "checkboxes" required="required">
<h3 style = "margin-top:-20px;">Please select one catagory that the quote falls into.</h3>
<label for="x"><input type="radio" name="x" value="Inspirational" id = "inspirational.php" checked="checked" /> <span>Inspirational</span></label><br>
<label for="x"><input type="radio" name="x" value="Funny" id = "funny.php" /> <span>Funny</span> </label><br>
<label for="x"><input type="radio" name="x" value="OutofContext" id = "outofcontext.php"/> <span>OutofContext</span></label>
</div>
<input id = "submit1" name="submit1"s type="submit"><br>
</form>
这是将其放入数据库的 php
<?php
$db_name = 'submissions';
$db_user = 'root';
$db_pass = '';
$db_host = 'localhost';
try {
$db = new PDO('mysql:host = localhost;dbname=submissions', $db_user, $db_pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
$actual_quote = (isset($_POST['actual_quote']) ? $_POST['actual_quote'] : null);
$poster = (isset($_POST['poster']) ? $_POST['poster'] : null);
$sql = "INSERT INTO data (actual_quote, poster) VALUES ( :actual_quote, :poster)";
$query = $db->prepare($sql);
$query->execute(array(':actual_quote'=>$actual_quote, ':poster'=>$poster));
?>
( ! ) 致命错误:未捕获的异常 'PDOException' 带有消息 'SQLSTATE[23000]:完整性约束违规:C:\wamp\www\Quotr\webweb2.php 中的 1048 列 'actual_quote' 不能为空113(!)PDOException:SQLSTATE [23000]:完整性约束违规:1048列'actual_quote'在C:\ wamp \ www \ Quotr \ webweb2.php中的第113行调用堆栈中不能为空 时间记忆功能位置 1 0.0015 257160 {main}( ) ..\webweb2.php:0 2 0.0206 267672 执行 ( ) ..\webweb2.php:113
当我在数据库中单击 null 并单击表单上的提交按钮时,错误消失并且数据显示在数据库中,但是没有值,那里什么都没有
谁能告诉我哪里出了问题以及如何解决这个问题,如果它在代码中,我个人认为数据库有问题,这里有一些图片。
(忽略最后一张图片的前3个,最后一张是我所在的位置)
【问题讨论】:
-
旁注:去掉
name="submit1"s中"之后的s -
当 $_POST['actual_quote'] 未设置时,您将 $actual_quote 设置为空......但在数据库端,您已将此字段设置为不允许为空。要么在数据库上允许 NULL,要么在 $_POST['actual_quote'] 未设置时发送空字符串(两个单引号)。现在......为什么 $_POST['actual_quote'] 没有设置......这是一个不同的问题。