使用特定ID编辑数据

时间:2015-05-20 02:17:17

标签: php mysql

我有一个用于公告的编辑按钮,但是当我编辑它时,所有公告都会发生变化。我只想编辑特定的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`");

?>

请帮助我,谢谢你的回答我真的很感激

1 个答案:

答案 0 :(得分:0)

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