0

我有一个包含 20 个字段的配方表,通过一个表格,我已经填写了 19 个。但是出现插入失败错误 - “列数与第 1 行的值计数不匹配”。我没有填写的唯一一列是自动增量主键值(我猜应该自己处理),所以我不确定为什么会发生这个错误。将不胜感激任何帮助。

这是php:

<!DOCTYPE html>
<html>

<?php
  require_once 'login_news.php'; 
  include ('newscss.php');
  $conn = new mysqli($hn, $un, $pw, $db);
  if ($conn->connect_error) die($conn->connect_error);


if (isset($_POST['delete']) && isset($_POST['uniqno']))
  {
    $uniqno   = get_post($conn, 'uniqno');
    $query  = "DELETE FROM recipes WHERE uniqno='$uniqno'";
    $result = $conn->query($query);
    if (!$result) echo "DELETE failed: $query<br>" .
      $conn->error . "<br><br>";
  }


//var_dump($_FILES['imagine']);
if (isset($_POST['btn'])){
    $recregion  = get_post($conn, 'recregion');
    $recpostdate     = get_post($conn, 'recdate'); 
    $recpostdate     = date('Y-m-d H:i:s', strtotime($recpostdate));
    $rectitle   = get_post($conn, 'rectitle');
    $recsummary   = get_post($conn, 'recsummary');
    $reccontributor   = get_post($conn, 'reccontributor');
    $ingredcontent     = get_post($conn, 'ingredcontent');
    $prepsubhead1   = get_post($conn, 'prepsubhead1');
    $prepcontent1 = get_post($conn, 'prepcontent1');
    $prepsubhead2     = get_post($conn, 'prepsubhead2');
    $prepcontent2   = get_post($conn, 'prepcontent2');
    $prepsubhead3     = get_post($conn, 'prepsubhead3');
    $prepcontent3   = get_post($conn, 'prepcontent3');  
    $prepsubhead4     = get_post($conn, 'prepsubhead4');
    $prepcontent4   = get_post($conn, 'prepcontent4');  
    $prepsubhead5     = get_post($conn, 'prepsubhead5');
    $prepcontent5   = get_post($conn, 'prepcontent5');      

     if($_FILES['imagine']['error'] != UPLOAD_ERR_NO_FILE){
            $filetmp = $_FILES["imagine"]["tmp_name"];
                $recpicname = $_FILES["imagine"]["name"];
                $recpictype = $_FILES["imagine"]["type"];
                $recpicpath = "images/".$recpicname;

                move_uploaded_file($filetmp, $recpicpath);          

        $query    = "INSERT INTO recipes VALUES" .
          "('$recregion', '$recpostdate', '$rectitle', '$recsummary', '$reccontributor', '$recpicname', '$recpicpath', '$recpictype', 
          '$ingredcontent', '$prepsubhead1', '$prepcontent1', '$prepsubhead2', '$prepcontent2', '$prepsubhead3', 
          '$prepcontent3', '$prepsubhead4', '$prepcontent4', '$prepsubhead5', '$prepcontent5')";
        $result   = $conn->query($query);

        if (!$result) echo "INSERT failed: $query<br>" .
          $conn->error . "<br><br>";
     }
     else{

        $query    = "INSERT INTO recipes VALUES" .
          "('$recregion', '$recpostdate', '$rectitle', '$recsummary', '$reccontributor', '$ingredcontent', 
          '$prepsubhead1', '$prepcontent1', '$prepsubhead2', '$prepcontent2', '$prepsubhead3', 
          '$prepcontent3', '$prepsubhead4', '$prepcontent4', '$prepsubhead5', '$prepcontent5')";
        $result   = $conn->query($query);

        if (!$result) echo "INSERT failed: $query<br>" .
          $conn->error . "<br><br>";

     }   
  }

?>
 <form action="recipes.php" method="post" enctype="multipart/form-data"><pre>
            Recipe's Region <input type="text" name="recregion">
            Date of Posting <input type="text" name="recdate">
                RecipeTitle <input type="text" name="rectitle">
                Summary <textarea rows="5" cols="60" name="recsummary"></textarea>
            Contributor <input type="text" name="reccontributor">  
         Ingredient Content <textarea rows="15" cols="60" name="ingredcontent"></textarea>
       Preparation Subhead1 <input type="text" name="prepsubhead1">
       Preparation Content1 <textarea rows="15" cols="60" name="prepcontent1"></textarea>
       Preparation Subhead2 <input type="text" name="prepsubhead2">
       Preparation Content2 <textarea rows="15" cols="60" name="prepcontent2"></textarea>
       Preparation Subhead3 <input type="text" name="prepsubhead3">
       Preparation Content3 <textarea rows="15" cols="60" name="prepcontent3"></textarea>    
       Preparation Subhead4 <input type="text" name="prepsubhead4">
       Preparation Content4 <textarea rows="15" cols="60" name="prepcontent4"></textarea>     
       Preparation Subhead5 <input type="text" name="prepsubhead5">
       Preparation Content5 <textarea rows="15" cols="60" name="prepcontent5"></textarea>

            <input type="file" name="imagine">
           <input type="submit" name="btn" value="Upload Image & ADD RECIPE RECORD">          
  </pre></form>



<?php


  function get_post($conn, $var)
  {
    return $conn->real_escape_string($_POST[$var]);
  }

  ?>
  </html>

这是完整的错误消息:

INSERT failed: INSERT INTO recipes VALUES('Assam', '2000-12-12 00:00:00', 'Wingnuts', '最好的吃掉你心脏的方法', 'Walia', 'firstfood.jpg', 'images/firstfood.jpg', 'image/jpeg', '水\r\npaper\r\nnuts\r\nbarberries\r\n竹笋', 'Wash', '彻底清洗\r\n拍干', '干炒'、'在煎锅上加热\r\n加香料\r\n放凉'、'加水'、'放入雨水中\r\n搅拌'、'盐'、'加喜马拉雅海盐'、'唱歌', '举起盘子\r\n唱得好') 列数与第 1 行的值数不匹配

这是表结构: 在此处输入图像描述

4

3 回答 3

0

查询中缺少 uniqno 列,这就是它抛出错误的原因 列数不匹配。代替

 $query    = "INSERT INTO recipes VALUES" .
      "('$recregion', '$recpostdate', '$rectitle', '$recsummary', '$reccontributor', '$ingredcontent', 
      '$prepsubhead1', '$prepcontent1', '$prepsubhead2', '$prepcontent2', '$prepsubhead3', 
      '$prepcontent3', '$prepsubhead4', '$prepcontent4', '$prepsubhead5', '$prepcontent5')";

 $query    = "INSERT INTO recipes VALUES" .
      "('','$recregion', '$recpostdate', '$rectitle', '$recsummary', '$reccontributor', '$ingredcontent', 
      '$prepsubhead1', '$prepcontent1', '$prepsubhead2', '$prepcontent2', '$prepsubhead3', 
      '$prepcontent3', '$prepsubhead4', '$prepcontent4', '$prepsubhead5', '$prepcontent5')";
于 2016-01-13T13:06:54.143 回答
0

您的表有 20 列,但您只向插入查询添加 18 个值。您必须添加卷名称或将空值添加到您不会填充值的列。

insert into recipes (<column names>) values(<values>)

有关插入语法的更多信息,请查看官方文档

于 2016-01-13T12:12:56.677 回答
0

是的-这已通过对@Sougata 问题的评论解决-但是无法将其标记为评论中的答案。正如许多人后来指出的那样,问题是 - 我没有在 INSERT 查询中指定值应用于哪些列。最终使用了这个:

$query    = "INSERT INTO recipes (recregion, recpostdate, rectitle, recsummary, reccontributor, recpicname, recpicpath, recpictype, 
  ingredcontent, prepsubhead1, prepcontent1, prepsubhead2, prepcontent2, prepsubhead3, 
  prepcontent3, prepsubhead4, prepcontent4, prepsubhead5, prepcontent5) VALUES" .
  "('$recregion', '$recpostdate', '$rectitle', '$recsummary', '$reccontributor', '$recpicname', '$recpicpath', '$recpictype', 
  '$ingredcontent', '$prepsubhead1', '$prepcontent1', '$prepsubhead2', '$prepcontent2', '$prepsubhead3', 
  '$prepcontent3', '$prepsubhead4', '$prepcontent4', '$prepsubhead5', '$prepcontent5')";
$result   = $conn->query($query);
于 2016-01-13T13:45:13.667 回答