SQL Update仅在查询时更新1行

时间:2013-11-23 00:11:22

标签: php mysql sql-update

我做了一个表单来更新sql。我需要通过向数据库发送两个值来更新帖子以使其成为特色:

“是”进入featured,将date("F j, Y, g:i a")加入date_featured

它有效,但只更新featured列,而不是date_featured

MYSQL看起来像这样:

特色varchar(25)latin1_swedish_ci NULL:No DEFAULT:no date_featured datetime NULL:No DEFAULT:0000-00-00 00:00:00

继承我的代码,省略了DB细节

if(isset($_POST['update']))
{
$dbhost = 'XXX';
$dbuser = 'XXX';
$dbpass = 'XXX';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}

$id = $_POST['id'];
$featured = "yes";
$datefeatured = date("F j, Y, g:i a");
$sql = "UPDATE pins SET featured = '$featured', date_featured = '$datefeatured' WHERE id ='$pinDetails->id'";

mysql_select_db('XXX');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
echo "Featured on $datefeatured.";
mysql_close($conn);
}
else
{

HTML:

<form method="post" action="<?php $_PHP_SELF ?>">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td>Feature
<input name="id" type="hidden" id="id">
<input name="update" type="submit" id="update" value="Change">
</td>
</tr>
</table>
</form>

当我点击提交按钮时,它会更新并显示回显,但当我检查数据库时,featured列已更新。

我哪里错了?

1 个答案:

答案 0 :(得分:2)

确保您的日期格式为日期('Y-m-d')