更新&从模态引导程序中删除MySQL

时间:2016-04-06 20:49:49

标签: php mysql crud bootstrap-modal

我试图从模式窗口内的表单中删除和/或更新数据。为了创建它我没有问题但是其他操作(删除和更新)我总是有问题,我认为关键是“id”似乎取最后一个值,而不是我要删除或更新的值。 我知道我的代码可能更好。

   <div class="container">

        <div class="row espacio">
                <p class="col-md-1"><a data-toggle="modal" href="#myModal" class="btn btn-default glyphicon glyphicon-plus"><span></span></a></p>
            <p class="col-md-1 col-md-offset-10"><span class="btn btn-default glyphicon glyphicon-envelope"><a href=""></a></span></p>
            <table class="table  table-bordered">
                <caption>Bitácora</caption>
                <thead>
                    <tr>
                      <th>Tecnico</th>
                      <th>Turno</th>
                      <th>Información</th>
                      <th>Estado</th>
                      <th>Creada</th>
                      <th>Modificada</th>
                    </tr>
                </thead>
              <tbody>

                <?php
                  $txt ="";
                  $nombres =array();
                  $turnos = array('Mañana','Tarde','Noche');
                  $estados =array('Pendiente','Cerrado');
                  $db = new Db;
                  $sql = "SELECT * FROM tecnicos";
                  foreach ($db->consulta($sql) as $row) {
                      array_push($nombres,$row['Nombre']);
                  }
                  $sql = 'SELECT * FROM bitacora ORDER BY Estado DESC';
                  $consulta = $db->consulta($sql);
                  while ($row = mysqli_fetch_array($consulta)) {
                        $tecnico = $turno = $estado = "";
                        if ($row['Estado'] == "Pendiente"){
                          $txt .= "<tr class= 'bg-warning'>";
                        }else{
                          $txt .= "<tr class= 'bg-success'>";
                        }
                        $txt .= "
                                <td>". $row['Name'] . "</td>
                                <td>". $row['Turno'] . "</td>
                                <td>". $row['Info'] . "</td>
                                <td>". $row['Estado'] . "</td>
                                <td>". $row['RecordDate'] . "</td>
                                <td>". $row['ModiDate'] . "</td>
                                <td class='text-center'><a data-toggle='modal' href='#myModalUpdate".$row['Id']."''><span class='glyphicon glyphicon-pencil'></span></a>
                                <a data-toggle='modal' href='#myModalDelete".$row['Id']."''><span class='glyphicon glyphicon-remove'></span></a> </td>
                              </tr>";


                        //MODAL UPDATE

                        for ($i=0; $i <sizeof($nombres) ; $i++) { 
                            if ($nombres[$i] == $row['Name'] ) {
                                $tecnico .= '<option value="'.$nombres[$i].'" selected="selected">'.$nombres[$i].'</option>';
                            }
                            else{
                                $tecnico .= '<option value="'.$nombres[$i].'" >'.$nombres[$i].'</option>';
                            }
                        }

                        for ($i=0; $i <sizeof($turnos) ; $i++) { 
                            if ($turnos[$i] == $row['Turno']) {
                                $turno .= '<option value="'.$turnos[$i].'" selected="selected">'.$turnos[$i].'</option>';
                            }
                            else{
                                $turno .= '<option value="'.$turnos[$i].'" >'.$turnos[$i].'</option>';
                            }
                        }

                        for ($i=0; $i <sizeof($estados) ; $i++) { 
                            if ($estados[$i] == $row['Estado']) {
                                $estado .= '<option value="'.$estados[$i].'" selected="selected">'.$estados[$i].'</option>';
                            }
                            else{
                                $estado .= '<option value="'.$estados[$i].'" >'.$estados[$i].'</option>';
                            }
                        }

                        $txt .= " <div class='modal fade' id='myModalUpdate".$row['Id']."' role='dialog'>
                                    <div class='modal-dialog'>
                                      <!-- Modal content-->
                                      <div class='modal-content'>
                                          <div class='modal-header'>
                                            <button type='button' class='close' data-dismiss='modal'>&times;</button>
                                            <h4 class='modal-title'>Modifica registro en Bitácora</h4>
                                          </div>
                                          <div class='modal-body'>
                                            <form role='form' action='' method='post'>
                                              <div class='form-group'>
                                                  <input type='text' name='id' value='".$row['Id']."'>
                                              </div>
                                              <div class='form-group'>
                                                  <label>Técnico</label>
                                                  <select class='form-control' name='modnombre'>
                                                      ".$tecnico."
                                                  </select>
                                              </div>
                                              <div class='form-group'>
                                                  <label>Turno</label>
                                                  <select class='form-control' name='modturno'>
                                                      ".$turno."
                                                   </select>
                                              </div>
                                              <div class='form-group'>
                                                  <label>Información</label>
                                                  <textarea class='form-control' name='modinformacion'>".$row['Info']."</textarea>
                                              </div>
                                              <div class='form-group'>
                                                  <label>Estado</label>
                                                  <select class='form-control' name='modestado'>
                                                      ".$estado."
                                                  </select>
                                              </div>
                                              <div class='form-group text-center'>
                                                  <button type='submit' class='btn btn-warning' name='actualizar'>Actualiza</button>
                                              </div>
                                          </div><!--/modal body-->
                                          <div class='modal-footer'>
                                            <button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
                                          </div>
                                      </div><!--/modal content-->
                                  </div><!--/modal dialog-->
                                </div><!--/modal-->";

                        //MODAL DELETE

                        $txt .= " <div class='modal fade' id='myModalDelete".$row['Id']."' role='dialog' >
                                    <div class='modal-dialog'>
                                      <!-- Modal content-->
                                      <div class='modal-content'>
                                          <div class='modal-header'>
                                            <button type='button' class='close' data-dismiss='modal'>&times;</button>
                                            <h4 class='modal-title'>Elemina registro en Bitácora</h4>
                                          </div>
                                          <div class='modal-body'>
                                            <form role='form' action='' method='post'>
                                              <div class='form-group'>
                                                  <input type='text' name='id' value='".$row['Id']."'>
                                              </div>
                                              <div class='form-group text-center'>
                                                  <button type='submit' class='btn btn-danger' name='eliminar'>Elimina</button>
                                              </div>
                                          </div><!--/modal body-->
                                          <div class='modal-footer'>
                                            <button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
                                          </div>
                                      </div><!--/modal content-->
                                  </div><!--/modal dialog-->
                                </div><!--/modal-->";


                }//end for principal
                echo $txt;
                ?>

              </tbody>
        </table>
    </div>

    <!--MODAL CREATE-->
    <div class="modal fade" id="myModal" role="dialog">
        <div class="modal-dialog">
            <!-- Modal content-->
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title">Añade registro en Bitácora</h4>
                </div>
                <div class="modal-body">
                    <form role="form" action="" method="post">
                        <div class="form-group">
                            <label>Técnico</label>
                            <select class="form-control" name="nombre">
                                <?php
                                    $txt ="";
                                    $db = new Db;
                                    $sql = "SELECT * FROM tecnicos";
                                    foreach ($db->consulta($sql) as $row) {
                                        $txt.= "<option value='".$row['Nombre']."'>".$row['Nombre']."</option>";
                                    }
                                    echo $txt;
                                ?>
                            </select>
                        </div>
                        <div class="form-group">
                            <label>Turno</label>
                            <select class="form-control" name="turno">
                                <option value="Mañana">Mañana</option>
                                <option value="Tarde">Tarde</option>
                                <option value="Noche">Noche</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label>Información</label>
                            <textarea class="form-control" name="informacion"></textarea>
                        </div>
                        <div class="form-group text-center">
                            <button type="submit" class="btn btn-success" name="crear">Crear</button>
                        </div>
                    </form>
                </div><!--/modal body-->
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                </div>
            </div><!--/modal content-->
        </div><!--/modal dialog-->
    </div><!--/modal-->







</div> <!-- /container -->

这些是行动的形式

enter code //formulario CREATE
if(isset($_POST["crear"])) {
    $nombre = $_POST["nombre"];
    $turno = $_POST["turno"];
    $informacion = $_POST["informacion"];
    $sql = "INSERT INTO bitacora (Name,Turno,Info,Estado,RecordDate,ModiDate) VALUES ('" . $nombre . "', '" . $turno . "', '" . $informacion . "','Pendiente',now(),now())";
    $db->consulta($sql);
}

// formulario UPDATE

if(isset($_POST["actualizar"])) {
    $sql = "UPDATE bitacora SET Name = '" . $_POST["modnombre"] . "', Turno = '" . $_POST["modturno"] . "', Info = '" .$_POST["modinformacion"]. "',  Estado = '" . $_POST["modestado"] . "' , ModiDate =now() WHERE Id = '".$_POST["id"]."' ";
    $db->consulta($sql);
}

//表格DELETE

if(isset($_POST["eliminar"])) {
    $id = $_POST["id"];
    $sql = "DELETE FROM bitacora WHERE Id = $id";
    $db->consulta($sql);
}here

1 个答案:

答案 0 :(得分:0)

您的输入似乎名为&#34; id&#34;没有采用$ row [&#39; Id&#39;]的正确值。尝试检查它,看看他的&#34;价值&#34;不是空的。

如果值为空,则问题是$ row [&#39; Id&#39;]的值。 尝试用$ row [&#39; id&#39;]替换$ row [&#39; Id&#39;]并查看结果。