我在插入帖子时尝试使用日期功能插入日期。
提交表单后,我收到以下消息:
Column count doesn't match value count at row 1
SQL行的顺序正确,日期行是最后一行。
这是调用内容的函数:
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
?>
答案 0 :(得分:1)
它应该有4列,但这个插入只设置3列
INSERT INTO content VALUES (null, '$title', '$body')
您缺少time
列
试
INSERT INTO content VALUES (null, '{$title}', '{$body}', '{$time}');
OR
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个字符将被截断
答案 1 :(得分:0)
您使用
$sql = "INSERT INTO content VALUES (null, '$title', '$body')";
您必须为time
列提供值,或者允许其拥有NULL
值,并将列定义更改为默认使用NULL
。
这个地方的正确代码是:
$sql = "INSERT INTO content VALUES (null, '$title', '$body', '$time')";
答案 2 :(得分:0)
您的INSERT语句需要设置四个列值,但只提供三个。
它通常会提高可读性,并在指定列时帮助防止这种情况:
INSERT INTO content
(title, body, time)
VALUES
('$title', '$body', '{$p['time']}')
如果您稍后要向表中添加列,这也会有所帮助。
我还建议查看这些查询的参数化语句以防止SQL注入问题,你可以摆脱所有的mysql_real_escape_strings。
使用数据库时间设置时间字段而不是使用php(其中源可以是任意数量的不完美同步的客户端服务器)会更清晰。它可以设置为Default列,因此如果这是一个时间兼容的字段类型,您可以不需要将它包含在语句中,或者您可以显式使用CURRENT_TIME()。