PHP:日期函数错误 -> 列数不匹配

标签 php mysql

我尝试在插入帖子时使用日期函数插入日期。

提交表单后,我收到以下消息:

Column count doesn't match value count at row 1

SQL 行顺序正确,日期行是最后一行。

enter image description here

函数调用内容如下:

function add_content($p){
    $title = mysql_real_escape_string($p['title']);
    $body = mysql_real_escape_string($p['body']);
    $p['time'] = date("F j, Y, g:i a");
    $time = $p['time'];

其余代码:

    <?php

class blog {
    private $host;
    private $username;
    private $password;
    private $db;
    private $link;

    public function __construct($host, $username, $password, $db){
        $this->db = $db;
        $this->link = mysql_connect($host, $username, $password, $db);
        mysql_select_db($this->db, $this->link) or die (mysql_error());
    }


    function get_content($id=''){
    if($id !=NULL):
        $id = mysql_real_escape_string($id);
        $sql = "SELECT * FROM content WHERE id = '$id'";
        $return = '<p><a href="index.php">Vover al Indice</a></p>';
    else:
        $sql = "SELECT * FROM content ORDER by id DESC";
    endif;

    $result = mysql_query($sql) or die (mysql_error());

    if(mysql_num_rows($result) !=NULL):
    while($row = mysql_fetch_assoc($result)){
        echo '<h1><a href="index.php?id='.$row['id'].'">'.$row['title'].'</a></h1>';
        echo '<span class="time">'.$row['time'].'</span>';
        echo '<p>'.$row['body'].'</p>';
        }
    else:
        echo '<p>Oops, post not found!</p>';
    endif;
    if(isset($return)){
        echo $return;
        }
    }

    function add_content($p){
        $title = mysql_real_escape_string($p['title']);
        $body = mysql_real_escape_string($p['body']);
        $p['time'] = date("F j, Y, g:i a");
        $time = $p['time'];

        if(!$title OR !$body):
            if(!$title):
                echo "<p>You have to fill the title.</p>";
            endif;
            if(!$body):
                echo "<p>You have to fill the body.</p>";
            endif;
            echo '<p><a href="add-content.php">Try again!</a></p>';
            else:
                $sql = "INSERT INTO content VALUES (null, '$title', '$body')";
                $result = mysql_query($sql) OR DIE (mysql_error());
                echo "Added sucessfully!";
            endif;

    }

    function manage_content()
    {
        echo '<div id="manage">';
        $sql = "SELECT * FROM content";
        $result = mysql_query($sql) or die(mysql_error());
        while($row = mysql_fetch_assoc($result)): ?>
            <div>
                <h2 class="title"><?php echo $row['title']?></h2>
                <span class="actions"><a href="update-content.php?id=<?php echo $row['id']?>">Edit</a> | <a href="?delete=<?php echo $row['id']?>">Delete</a></span>
        </div>  <?php
        endwhile;
        echo '</div>'; //End of Manage Div

    }

    function delete_content($id){
        if(!$id){
            return false;
        }else{
            $id = mysql_real_escape_string($id);
            $sql = "DELETE FROM content WHERE id = '$id'";
            $result = mysql_query($sql) or die (mysql_error());
            echo "Content Deleted Successfully";
        }
    }

    function update_content_form($id){
    $id = mysql_real_escape_string($id);
    $sql = "SELECT * FROM content WHERE id = '$id'";
    $result = mysql_query($sql) or die (mysql_error());
    $row = mysql_fetch_assoc($result);  ?>

     <form method="post" action="index.php">
        <input type="hidden" name="update" value="true"/>
        <input type="hidden" name="id" value="<?php echo $row['id']?>"/>
        <div>
            <label for="title">Title:</label>
            <input type="text" name="title"= id="title" value="<?php echo $row['title']?>"/>
        </div>
        <div>
            <label for="body"></label>
            <textarea name="body" id="body" rows="8" cols="40"><?php echo $row['body']?></textarea>
        </div>
        <input type="submit" name="submit" value="Update Content"/>

    </form><?php
    }

    function update_content($p){
        $title = mysql_real_escape_string($p['title']);
        $body = mysql_real_escape_string($p['body']);
        $id = mysql_real_escape_string($p['id']);

        if(!$title OR !$body):
            if(!$title):
                echo "<p>You have to fill the title.</p>";
            endif;
            if(!$body):
                echo "<p>You have to fill the body.</p>";
            endif;
            echo '<p><a href="update-content.php?id='.$id.'">Try again!</a></p>';
            else:
                $sql = "UPDATE content SET title='$title', body='$body' WHERE id = '$id'";
                $result = mysql_query($sql) OR DIE (mysql_error());
                echo "Updated sucessfully!";
            endif;
    }
}// End of Class
?>

最佳答案

它应该有 4 列,但是这个插入只设置了 3 列

INSERT INTO content VALUES (null, '$title', '$body')

您缺少time

尝试

INSERT INTO content VALUES (null, '{$title}', '{$body}', '{$time}');

INSERT INTO content SET title='{$title}', body='{$body}', time='{$time}';

第二个问题

$p['time'] = date("F j, Y, g:i a");
$time = $p['time']; // 25 characters long

这超出了 varchar(20) 的列定义,最后 5 个字符将被截断

关于PHP:日期函数错误 -> 列数不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7178327/

相关文章:

php - 如何从mysql数据库在PHP中的多维数组中添加数组?

mysql 在排序时将包含字母的字段视为最低值

mysql - 如何根据其他列的结果运行 SELECT 语句?

php - 在组织中培养 PHP 能力

php - JavaScript 图像调整缓慢

php - Symfony 3.3 - 无法使用 EntityType 选择空值

mysqlfailover 没有将 slave 切换到 master,缺少 gtid_executed 系统变量

PHP PDO 将多行/值插入到 mySQL 表中

PHP 使用 if 语句在一封邮件中发送多行

php - 验证或删除 laravel 中的额外字段