【问题标题】:Stuck at this error: Incorrect integer value: '' for column '____' at row 1遇到此错误:不正确的整数值:第 1 行的列 '____' 的''
【发布时间】:2015-07-29 11:31:12
【问题描述】:

当我提交表单并使用此脚本将数据插入数据库时​​,我收到上述错误...有什么想法吗?

        //Include connect file to make a connection to test_cars database
        include("prototypeconnect.php");


        $proCode            =       $_POST["code"];
        $proDescr           =       $_POST["description"];
        $proManu            =       $_POST["manufacturer"];
        $proCPU             =       $_POST["cost_per_unit"];
        $proWPU             =       $_POST["weight_per_unit"];
        $proBarCode         =       $_POST["bar_code"];
        $proIngredients     =       $_POST["ingredients_list"];
        $proAllergens       =       $_POST["allergens_contains"];
        $proMayAllergens    =       $_POST["allergens_may_contain"];

        //Insert users data in database
        $sql = "INSERT INTO prodb.simplex_list 
                   code, description, manufacturer, 
                   cost_per_unit, weight_per_unit, bar_code,
                   ingredients_list, allergens_contains,
                   allergens_may_contain) 
                VALUES 
                  ( '$proCode', '$proDescr' , '$proManu', 
                   '$proCPU' , '$proWPU' , '$proBarCode', 
                   '$proIngredients' , '$proAllergens', 
                   '$proMayAllergens')";

        //Run the insert query

        if (!mysql_query($sql)) {
            echo mysql_error();
        }

    ?>

更新:我删除了 id 插入,因为它们是自动递增的,我从你的答案中了解到,不需要对 null 进行编码,mysql 会照顾 AI。谢谢大家!

【问题讨论】:

  • 在插入查询中从 $id 中删除单引号。还要检查filed是否是db中的整数类型,然后不要传递单引号。
  • @DishaV。实际上我的 id 是自动递增的,所以我删除了它......仍然是同样的错误
  • 如果有任何问题,如果您可以在尝试插入 /update 等之前回显完整的 sql,我发现这很有帮助。然后在您的 mySql 客户端应用程序(heidi 等)中尝试该命令目录 - 只是一个想法
  • 显然您只是在学习,我建议您从学习mysqli_PDO 数据库访问扩展开始。 mysql_ 扩展已失效,并已从即将发布的 PHP7 中完全删除。不幸的是,网络上的大多数教程都使用 mysql_ 扩展,但从现在开始你只需要完全忽略它们
  • @vard 哇!我应该总是使用 mysqli_escape_string 所以当从表单使用 POST 时..

标签: php mysql


【解决方案1】:

查询需要像:-

 $sql = "INSERT INTO prodb.simplex_list 
         (code, description, manufacturer, 
          cost_per_unit, weight_per_unit, 
          bar_code, ingredients_list, allergens_contains, 
          allergens_may_contain) 
         VALUES ('$proCode', '$proDescr', '$proManu',
                 '$proCPU','$proWPU', '$proBarCode', 
                 '$proIngredients', '$proAllergens', 
                 '$proMayAllergens')";

注意:- 请停止使用mysql_*。使用mysqli_*PDO。此外,这仅在 id 字段必须自动递增时才有效。

【讨论】:

  • 嗨,请从插入中删除 id。因为你没有通过prodid。
  • 嗨,不需要使用id。删除该字段。以及删除 VALUES() 中的空“”参数。像这样,$sql = "INSERT INTO prodb.simplex_list (code, description, manufacturer, cost_per_unit, weight_per_unit, bar_code, ingredients_list, allergens_contains, allergens_may_contain) VALUES ('".$proId."' , '".$proCode."' , '".$proDescr."' , '".$proManu."' , '".$proCPU."' ,'".$proWPU."' , '".$proBarCode."' , '".$ proIngredients."', '".$proAllergens."', '".$proMayAllergens."')";
  • @AK-Sonu 为什么我应该停止使用 mysql_* 而不是 mysqli?导致更多错误说我缺少参数...
  • 因为现在mysql_* 已被正式弃用。
  • 希望你不介意我从参数列表中编辑了'$proId' ,,并删除了所有不必要的连接,并对其进行了格式化,以便于阅读
猜你喜欢
  • 1970-01-01
  • 2017-03-25
  • 2014-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多