PHP:Date函数错误->;列计数不为';不匹配
我正在尝试在插入帖子时使用date函数插入日期 提交表格时,我收到以下消息:PHP:Date函数错误->;列计数不为';不匹配,php,mysql,Php,Mysql,我正在尝试在插入帖子时使用date函数插入日期 提交表格时,我收到以下消息: 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']);
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
?>
它应该有4列,但此插入只设置了3列
INSERT INTO content VALUES (null, '$title', '$body')
您缺少time
列
试一试
或
第二个问题
这超出了varchar(20)
的列定义,最后5个字符将被截断它应该有4列,但此插入仅设置3列
INSERT INTO content VALUES (null, '$title', '$body')
您缺少time
列
试一试
或
第二个问题
这超出了varchar(20)
的列定义,最后5个字符将被截断
$sql = "INSERT INTO content VALUES (null, '$title', '$body')";
您必须为time
列指定一个值,或者允许它具有NULL
值,并将列定义更改为默认使用NULL
此处的正确代码为:
$sql = "INSERT INTO content VALUES (null, '$title', '$body', '$time')";
你用
$sql = "INSERT INTO content VALUES (null, '$title', '$body')";
您必须为time
列指定一个值,或者允许它具有NULL
值,并将列定义更改为默认使用NULL
此处的正确代码为:
$sql = "INSERT INTO content VALUES (null, '$title', '$body', '$time')";
INSERT语句需要设置四列值,但只提供三列值
如果指定以下列,它通常会提高可读性并有助于防止出现这种情况:
INSERT INTO content
(title, body, time)
VALUES
('$title', '$body', '{$p['time']}')
如果以后要向表中添加列,这也会有所帮助
我还建议研究类似查询的参数化语句,以防止SQL注入问题,您可以去掉所有mysql\u real\u escape\u字符串
使用数据库时间来设置时间字段比使用php(源可以是任意数量的不完全同步的客户机服务器)更干净。可以将其设置为列默认值,这样如果这是一个时间兼容的字段类型,就不需要将其包含在语句中,或者可以显式使用CURRENT_time()。INSERT语句需要设置四个列值,但只提供三个列值
如果指定以下列,它通常会提高可读性并有助于防止出现这种情况:
INSERT INTO content
(title, body, time)
VALUES
('$title', '$body', '{$p['time']}')
如果以后要向表中添加列,这也会有所帮助
我还建议研究类似查询的参数化语句,以防止SQL注入问题,您可以去掉所有mysql\u real\u escape\u字符串
使用数据库时间来设置时间字段比使用php(源可以是任意数量的不完全同步的客户机服务器)更干净。它可以设置为列默认值,这样如果这是一个时间兼容的字段类型,您就不需要在语句中包含它,或者您可以显式地使用CURRENT_time()。为什么要将时间存储在VARCHAR而不是DATETIME?为什么要将时间存储在VARCHAR而不是DATETIME?谢谢,我完全忘记了这一点!谢谢,我完全忘了那个!