开发者

PHP: Date function error -> Column count doesn't match

I'm tryin to insert dates using the date function while inserting posts.

When the form is submitted I get the following message:

Column count doesn't match value count at row 1

The SQL rows are in the correct order, the date row is the last one.

PHP: Date function error -> Column count doesn't match

Here's the function calling content:

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'];

The rest of the code:

    <?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
?>


it should have 4 column, but this insert only set 3 columns

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

you are missing time column

try

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

OR

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

second problem

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

this exceed your column definition of varchar(20), the last 5 characters will get truncated


You use

$sql = "INSERT INTO content VALUES (null, '$title', '$body')";

You must give a value for time column or allow it to have NULL values and change column definition to use NULL by default.

The correct code in this place is:

$sql = "INSERT INTO content VALUES (null, '$title', '$body', '$time')";


Your INSERT statement needs to set four column values but you only provide three.

It generally improves readability and helps prevent this if you specify the columns:

 INSERT INTO content
     (title, body, time)
 VALUES
     ('$title', '$body', '{$p['time']}')

This would also help if you were to add columns to the table later.

I'd also suggest looking into Parameterized Statements for queries like these to prevent SQL injection issues and you could get rid of all the mysql_real_escape_strings.

It would be cleaner to use the database time to set the time field rather than using php (where the source could be any number of imperfectly synchronized client servers). It could be set as the column Default so you wouldn't need to include it in your statement if this were a time-compatible field type, or you could use CURRENT_TIME() explicitly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜