gpt4 book ai didi

php - 编辑具有特定 ID 的数据

转载 作者:行者123 更新时间:2023-11-29 03:31:12 25 4
gpt4 key购买 nike

我有一个用于公告的编辑按钮,但当我编辑它时,所有公告都会更改。我只想编辑特定的 ID。这是我的代码:

   <?php
if(mysql_num_rows($query_announcement) > 0)
{
$row = mysql_num_rows($query_announcement);
for($x = 1; $x<= $row; $x++){

$get_announcement= mysql_fetch_assoc($query_announcement);
$html = '<tr>';
$html .= '<td>'.$get_announcement['id'].'</td>';
$html .= '<td>'.$get_announcement['announce_name'].'</td>';
$html .= '<td>'.$get_announcement['announce_description'].'</td>';
$html .= '<td>'.$get_announcement['announce_location'].'</td>';
$html .= '<td>'.date('M d,Y', strtotime($get_announcement['date_start'])).'</td>';
$html .= '<td>'.$get_announcement['date_end'].'</td>';
$html .= '<td>'.date('M d,Y', strtotime($get_announcement['date_added'])).'</td>';
$html .= '<td width="15%">

<button type="button" data-toggle="modal" data-target="#modal_addAnnouncementEdit" class="btn btn-default" title="Edit"><span class="fa fa-pencil"></span></button>
<button type="button"class="btn btn-default" title="Delete" ><span class="fa fa-trash-o"></span></button>

</td>';
$html .= '</tr>';

echo $html;
}
}
else
{
echo '<tr><td colspan="8" align="center"><h3>No Announcement</h3></td></tr>';
}
?>

编辑模式:

<div class="modal fade" role="dialog" id="modal_addAnnouncementEdit" aria-labelledby="gridSystemModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="gridSystemModalLabel"><span class="fa fa-bullhorn fa-fw fa-lg"></span> Edit Announcement</h4>
</div>
<div class="modal-body">
<form class="form-horizontal" id="form_addAnnouncementEdit">
<div class="form-group">
<label for="" class="col-sm-3 control-label">Title:</label>
<div class="col-sm-7">

<input type="text" class="form-control" id="addAnnouncement_title_edit" name="addAnnouncement_title_edit" placeholder="Title">
</div>
</div>

<div class="form-group">
<label for="" class="col-sm-3 control-label">Description:</label>
<div class="col-sm-7">
<input type="text" class="form-control" id="addAnnouncement_description_edit" name="addAnnouncement_description_edit" placeholder="Description">
</div>
</div>

<div class="form-group">
<label for="" class="col-sm-3 control-label">Location:</label>
<div class="col-sm-7">
<input type="text" class="form-control" id="addAnnouncement_location_edit" name="addAnnouncement_location_edit" placeholder="Location">
</div>
</div>

<div class="form-group">
<label for="" class="col-sm-3 control-label">Date Start:</label>
<div class="col-sm-7">
<input type="text" class="datepicker" id="addAnnouncement_dateStart_edit" name="addAnnouncement_dateStart_edit" placeholder="Date Start">
</div>
</div>

<div class="form-group">
<label for="" class="col-sm-3 control-label">Time:</label>
<div class="col-sm-7">
<input type="text" class="form-control" id="addAnnouncement_dateEnd_edit" name="addAnnouncement_dateEnd_edit" placeholder="Time">
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" id="addAnnouncement_save_submit" name="addAnnouncement_save_submit" >Save Changes</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->

我的更新查询:

<?php
require"../includes/library/DB.php";

$title = $_POST['addAnnouncement_title_edit'];
$description = $_POST['addAnnouncement_description_edit'];
$location = $_POST['addAnnouncement_location_edit'];
$date_start = $_POST['addAnnouncement_dateStart_edit'];
$date_end = $_POST['addAnnouncement_dateEnd_edit'];
$date_created = date("Y-m-d H:i:s");
$query_announcement = mysql_query("SELECT * FROM announcement");
$get_announcement = mysql_fetch_assoc($query_announcement);
$insert_announcement = mysql_query("UPDATE `loanapplicationform`.`announcement` SET `announce_name` = '$title', `announce_description` = '$description', `announce_location` = '$location',
`date_start` = '$date_start', `date_end` = '$date_end' WHERE `announcement`.`id`=`$id`");

?>

请帮我解决这个问题谢谢你的回答我真的很感激

最佳答案

在您的MY UPDATE QUERY 部分,您需要定义$id

这是您的查询:

$insert_announcement = mysql_query("UPDATE    
`loanapplicationform`.`announcement` SET `announce_name` = '$title',
`announce_description` = '$description', `announce_location` = '$location',
`date_start` = '$date_start', `date_end` = '$date_end' WHERE
`announcement`.`id`=`$id`");

当它被执行时,它并不知道$id 是什么。所以它会将你的 WHERE 子句视为

...WHERE `announcement`.`id`=

代替

...WHERE `announcement`.`id`='abc12345'

如果您只是简单地将 $id 替换为 $get_announcement['id'] 将不起作用,因为 $get_announcement['id'] 只包含循环中的值。

您需要在定义所有其他变量的地方定义$id,例如:

$id = $_POST['addAnnouncement_id_edit'];

$title = $_POST['addAnnouncement_title_edit'];
$description = $_POST['addAnnouncement_description_edit'];
...
...

关于php - 编辑具有特定 ID 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30339198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com