You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PHP预处理语句的HTML表单丢失$_POST数据问题排查

解决SQLSTATE[23000]: 1048 Column 'title' cannot be null错误

Hey there, let's work through this issue together—this is one of the most common gotchas when building form-to-database submission flows, so we can track it down quickly.

First, let's break down what the error means: your database's title column is set to NOT NULL (it won't accept empty/NULL values), but when you submit the form, the backend is trying to insert a NULL value for title. Here's the step-by-step troubleshooting plan:

1. Double-check your HTML form's name attribute

This is the #1 culprit 90% of the time. Make sure your title input has the correct name="title" attribute—your backend uses the name attribute to pull values from the POST request, not the id attribute.

Wrong examples (missing or misspelled name):

<!-- ❌ 没有name属性,后端拿不到值 -->
<input type="text" id="title">

<!-- ❌ name拼写错误,后端找的是'title'但这里是'tilte' -->
<input type="text" id="title" name="tilte">

Correct example:

<!-- ✅ 正确的name属性,同时加required做前端基础验证 -->
<label for="news-title">新闻标题</label>
<input type="text" id="news-title" name="title" required>

Adding the required attribute also prevents users from submitting an empty title in the first place, reducing unnecessary backend errors.

2. Verify your backend is correctly receiving the title value

Next, debug the data your backend actually gets when the form submits. For example, if you're using PHP, add this line at the top of your submission handler:

var_dump($_POST); // 输出所有POST数据,看看有没有'title'键和对应的值

If title doesn't show up in the output, go back to step 1—your form's name attribute is wrong. If it does show up but the value is empty, check if you have any filtering/cleaning code that's accidentally wiping it out (like over-aggressive trim() combined with a check that sets empty values to NULL).

3. Check your database table and query for mismatches

  • Confirm your database table's title column is named exactly title (not news_title or something else).
  • Make sure your INSERT query uses the correct column name and binds the right value. Using prepared statements is not just safer (prevents SQL injection), but also avoids typos in parameter binding:

Example of a safe prepared statement (PHP with PDO):

// 先验证title存在且不为空
if (!isset($_POST['title']) || trim($_POST['title']) === '') {
  echo "请填写新闻标题";
  exit;
}

// 预处理插入语句
$stmt = $pdo->prepare("INSERT INTO news (title, content, publish_date) VALUES (:title, :content, NOW())");
$stmt->bindParam(':title', $_POST['title']);
$stmt->bindParam(':content', $_POST['content']);
$stmt->execute();

4. Rule out frontend JavaScript interference

If you're using JavaScript to handle form submission (like preventing default and sending an AJAX request), make sure you're actually including the title value in the data you send to the backend. For example, in fetch:

const form = document.querySelector('form');
form.addEventListener('submit', async (e) => {
  e.preventDefault();
  const formData = new FormData(form);
  // 确认formData里有title
  console.log(formData.get('title'));
  const response = await fetch('/submit-news', {
    method: 'POST',
    body: formData
  });
});

Start with steps 1 and 2—those will almost always uncover the issue. Once you fix the missing/misspelled name attribute or the backend data handling, the error should disappear.

内容的提问来源于stack exchange,提问作者Jack Anyon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:45:02