需要在bootstrap中使用modal编辑一行表

时间:2014-09-29 10:28:03

标签: php mysql sql mysqli phpmyadmin

我有一个网站,登录后管理员可以编辑"设施列表"这是他的个人资料。我使用下面的代码创建了一个简单的表格,显示了设施。

<?php
$con=mysqli_connect("abc.com","abc","abc","abc");// Check connection
if (mysqli_connect_errno()) 
  {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$result = mysqli_query($con,"SELECT * FROM amenities");
echo "<table class='table table-striped table-bordered table-hover'>
<thead>
  <tr>
    <th>amenities</th>
    <th>edit</th>
  </tr>
</thead>";
while($row = mysqli_fetch_array($result)) 
  {
    echo "<tbody data-link='row' class='rowlink'>";
    echo "<tr>";
    echo "<td>" . $row['amenities'] . "</td>";
    echo "<td> <a href='#edit' data-toggle='modal'> edit </a> </td>";
    echo "</tr>";
    echo "</tbody>";    
  }
    echo "</table>";
    mysqli_close($con);
?>

除了列表,我还尝试合并编辑按钮后面的模态。显示该表的页面视图为:

id    amenities    edit
1     amenities1   edit
2     amenities2   edit

当管理员点击特定的编辑按钮(例如:设施1前面的编辑​​按钮)时,会显示一个模式,允许管理员仅编辑该设施(在我们的例子中是设施1)

用于Modal的代码是

<div class = "modal fade" id="edit" role="dialog">
    <div class = "modal-dialog">
        <div class = "modal-content">
            <div class = "modal-header">
                <h4> Edit Page </h4>
            </div>

            <div class="modal-body">
                <form role="form" action="edit_amenities.php" method="post">
                    <div class="form-group">
                        <label for="exampleInputEmail1">Name of Amenities</label>
                            <input type="email" class="form-control" id="exampleInputEmail1" placeholder="Email" name="amenities">
                    </div>

                    <input name="submit" type="submit" value=" Save ">              
                </form> 
            </div>  

            <div class="modal-footer">
                <a class="btn btn-primary" data-dismiss="modal"> Close </a>
            </div>
        </div>
    </div>  
</div>

在表单后端(edit_amenities.php)工作的代码是:

<?php
include('admin_session.php');

$con=mysqli_connect("abc","abc","abc","abc");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$password = mysqli_real_escape_string($con, $_POST['amenities']);

$sql = "UPDATE amenities SET amenities='".$amenities."' where id='".$id."'";

if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}
header("Location: admin_amenities.php");
exit;

mysqli_close($con);
?>

数据库视图

id   amenities
1     q
2     a

我希望当打开模态时,管理员可以输入新名称并替换旧名称并保存在数据库中。但是我无法实现它。如果有人能帮助我,我将不胜感激

1 个答案:

答案 0 :(得分:0)

为什么不使用这样的东西,只是一个例子:

<div class="modal-body">
  <form action="edit_amenities.php" method="post">
    <div class="form-group">
      <label for="exampleInputEmail1">Name of Amenities</label>
      <input type="text" class="form-control" name="update[amenities]">
    </div>

    <input name="submit" type="submit" value=" Save ">              
  </form> 
</div>  

和PHP:

if ($this->getRequest()->isPost()) {
  $post = $this->getRequest()->getPost();
}

$amenitie = $this->amenitie->fetchRow(
  $this->amenitie->select()->where('id = ?', $this->params['id'])
);
if ($amenitie) {
  foreach ($post['update'] as $key => $val) {
    $amenitie->{$key} = $val;
  }

  $amenitie->save();
}

编辑:在你的例子中:

$sql = "UPDATE amenities SET amenities='".$amenities."'";

使用WHERE,并在输入字段中,提供要更新的amenitie的ID。