违反完整性约束:1048栏'建议'不能为空

时间:2014-11-22 23:12:55

标签: php sql prepared-statement

每当我尝试按下提交而不在我的网站上的建议框中输入任何内容时,该网站应该拒绝SQL建议数据库中的空白框并显示错误,但是当按下确定时,它会很快显示错误'Integrity constraint violation: 1048 Column 'Suggestion' cannot be null' in /home/u611142741/public_html/suggestions.php:41'

这是我的PHP代码:

$conn=mysql_connect("localhost", "u611142741_list", "REDACTED"); 
mysql_select_db("u611142741_sugge", $conn);

$db = new PDO("mysql:host=localhost;dbname=u611142741_sugge;","u611142741_list","REDACTED");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// If the form has been submitted
if (trim($_POST["suggestion543"]) == "") {
  echo "Error";
  echo '<script language="javascript">';
  echo 'alert("Invalid entry! Try Again.")';
  echo '</script>';
  echo '<script>';
  echo 'setTimeout(function(){ window.location="http://musicoro.tk/suggestion-form/" }, 500)';
  echo '</script>';
} else {
  $suggestion = $_POST['suggestion543'];
  $ip = $_SERVER['REMOTE_ADDR'];
  echo "Thank you for submitting your suggestion!";
  echo '<script>';
  echo 'setTimeout(function(){ window.location="http://musicoro.tk/suggestion-form/" }, 2000)';
  echo '</script>';
};



// Build an sql statment to add the student details
$stmt = $db->prepare('INSERT INTO suggestions

(`Suggestion`, `IP Address`) VALUES

(:suggestion, :ip)');

$stmt->bindParam(':suggestion', $suggestion, PDO::PARAM_STR);
$stmt->bindParam(':ip', $ip, PDO::PARAM_STR);
try {
        $stmt->execute();
}
catch(PDOException $e) {
        echo $e->getMessage();
}

$result = mysql_query($sql,$conn);


// close connection 
mysql_close($conn);

这被放入我的错误日志中:

[23000]: Integrity constraint violation: 1048 Column 'Suggestion' cannot be null' in /home/u611142741/public_html/suggestions.php:41
Stack trace:
#0 /home/u611142741/public_html/suggestions.php(41): PDOStatement->execute()
#1 {main}
  thrown in /home/u611142741/public_html/suggestions.php on line 41
[22-Nov-2014 22:59:06 UTC] PHP Fatal error:  Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'Suggestion' cannot be null' in /home/u611142741/public_html/suggestions.php:41
Stack trace:
#0 /home/u611142741/public_html/suggestions.php(41): PDOStatement->execute()
#1 {main}
  thrown in /home/u611142741/public_html/suggestions.php on line 41

有什么想法吗?

0 个答案:

没有答案