【问题标题】:sql 'incorrect integer value' messagesql'不正确的整数值'消息
【发布时间】:2011-07-21 06:40:29
【问题描述】:

任何人都可以看到为什么我在尝试提交时收到以下错误吗?我知道整个 null vs '' 的事情,但我没有任何 '' 应该导致这种情况。 mySQL 新手,所以任何帮助都会很棒。

第 1 行的列“二”的整数值不正确

<?php

$db_host = "localhost";
$db_username = "TEST";

$db_pass = "example";

$db_name = "questions";

@mysql_connect("$db_host","$db_username","$db_pass") or die ("Could not connect to MySQL");
@mysql_select_db("$db_name") or die ("No database");


if ($_POST['parse_var'] == "contactform"){


$oneField = $_POST['one'];
$twoField = $_POST['two'];
$threeField = $_POST['three'];
$noneField = $_POST['none'];
$surveyField = $_POST['survey'];
$questionField = $_POST['questions'];
$guestField = $_POST['guest'];
$homeField = $_POST['home'];


$selected_radio = $_POST['radio'];
  if ($selected_radio == 'one') 
  {
  $oneField = '1';
  }

  else if ($selected_radio == 'two') 
  {
  $twoField = '1';
  }

  else if ($selected_radio == 'three') 
  {
  $threeField = '1';
  }

  else if ($selected_radio == 'none') 
  {
  $noneField = '1';
  }


if($surveyField == "yes"){
$surveyField = "1";
 } 
if($mysqlField == "yes"){
$mysqlField = "1";
 } 
if($guestbookField == "yes"){
$guestbookField = "1";
 } 
if($homeField == "yes"){
$homeField = "1";
 } 

$sqlUpdate = mysql_query("INSERT INTO questiondata (one, two, three, none, survey, 
       question, guest, home)
 VALUES  ('$oneField','$twoField','$threeField','$noneField','$surveyField','$questionField','$guestField','$homeField')")
 or die (mysql_error());
}
?>

【问题讨论】:

  • 尝试删除插入语句中的',并将所有字段初始化为NULL(大小写无关)。

标签: php mysql null


【解决方案1】:

您正在尝试INSERT,其中一个整数参数为空,即:

INSERT INTO questiondata (one, two) VALUES ('1','')

除非onetwo 是字符串字段,否则这将不起作用。
修改您的代码,以便初始化空变量:

$oneField = isset($_POST['one']) ? $_POST['one'] : 0;
$twoField = isset($_POST['two']) ? $_POST['two'] : 0;

【讨论】:

    【解决方案2】:

    列“二”是一个整数列,显然您正试图在其中插入一个空字符串。

    检查空字符串,并将它们替换为另一个值或 null(如果该列可以为空)

    $twoField = $_POST['two'];
    if (empty($twoField))
    {
      $twoField = 0;
    }
    

    PS:我建议其他领域也这样做。

    【讨论】:

      猜你喜欢
      • 2021-07-14
      • 1970-01-01
      • 2018-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-08
      • 2020-05-28
      • 1970-01-01
      相关资源
      最近更新 更多