【问题标题】:Column count doesn't match value count at row 1 error列计数与第 1 行错误处的值计数不匹配
【发布时间】:2013-07-03 01:11:37
【问题描述】:

我在以下代码的标题中收到错误消息,该页面用于将子类别添加到我的论坛。

<?php

include '../includes/connect.php';
include '../header.php';

echo '<h2>Create a Sub category</h2>';
if($_SESSION['signed_in'] == false | $_SESSION['user_level'] != 1 )
{
//the user is not an admin
echo 'Sorry, you do not have sufficient rights to access this page.';
}
else
{
//the user has admin rights
if($_SERVER['REQUEST_METHOD'] != 'POST')
{
    //the form hasn't been posted yet, display it
    echo '<form method="post" action="">
        Category name: ';
        $sql = "SELECT cat_id, cat_name, cat_description FROM categories";
        $result = mysql_query($sql);
    echo '<select name="topic_cat">';
                while($row = mysql_fetch_assoc($result))
                {
                    echo '<option value="' . $row['cat_id'] . '">' . $row['cat_name'] . '</option>';
                }
            echo '</select><br />';


    echo 'Sub category name: <input type="text" name="sub_cat_name" /><br />
        Sub category description:<br /> <textarea name="sub_desc" /></textarea><br /><br />
        <input type="submit" value="Add Sub Category" />
     </form>';
}
else
{
    //the form has been posted, so save it
    $sql = "INSERT INTO subcategories(c_id, sub_cat_name, sub_desc)
       VALUES('" . $cat_id . ", " . $sub_cat_name . ", " . $sub_desc . "')";
    $result = mysql_query($sql) or die (mysql_error());
    echo 'The sub category <b>" . $sub_cat_name . "</b> has been added under the main category <b>" . $cat_name . "</b>';
    if(!$result)
    {
        //something went wrong, display the error
        echo 'Error' . mysql_error();
    }
    else
    {
        echo 'New Sub category succesfully added.';
    }
}
}
; ?>

我的 categories 表的结构是这样的..

  • cat_id
  • cat_desc

我的 subcategories 表的结构是这样的..

  • id(AI)
  • c_id
  • sub_cat_name
  • sub_desc

如果我没有提供足够的信息,请告诉我。

【问题讨论】:

  • 您没有提供错误信息
  • 对不起,错误信息在标题中。列计数与第 1 行的值计数不匹配

标签: php mysql select insert


【解决方案1】:

你错过了一些报价。改变

VALUES('" . $cat_id . ", " . $sub_cat_name . ", " . $sub_desc . "')";

到

VALUES('" . $cat_id . "', '" . $sub_cat_name . "', '" . $sub_desc . "')";

【讨论】:

  • 谢谢!解决了它,现在我需要弄清楚为什么它将空白数据插入数据库>_
  • 您似乎没有将 $_POST 变量读入您要放入查询的变量中。
【解决方案2】:

您在这里没有正确引用内容:

VALUES('" . $cat_id . ", " . $sub_cat_name . ", " . $sub_desc . "')";

你需要

VALUES('" . $cat_id . "', '" . $sub_cat_name . "', '" . $sub_desc . "')";

注意多余的单引号。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-21
    • 2021-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多