返回

javascript——如何从编辑字段的记录中插入旧值并进行更新?

发布时间:2022-03-21 14:38:49 380
# 前端

我的项目的“编辑”按钮工作不正常。我想要实现的是,如果我在数据库中的特定记录上单击编辑按钮,这些值将填充到表单中,并且可以对其进行编辑。我无法正确调用Ncreatequotes上的编辑函数。php

Ncreatequotes.php

<?php  include('serverquotes.php'); ?>
<?php 
  if (isset($_GET['edit'])) {
    $id = $_GET['edit'];
    $update = true;
    $record = mysqli_query($db, "SELECT * FROM mquotes WHERE id=$id");

    if ($record->num_rows == 1) {
      $row = mysqli_fetch_array($record);
      $quotes = $row['quotes'];
      $author = $row['author'];

    edit();

    }
  }
?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<style>
table {
  font-family: arial, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

td, th {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}

tr:nth-child(even) {
  background-color: #dddddd;
}
.btn {
    padding: 10px;
    font-size: 15px;
    color: white;
    background: #5F9EA0;
    border: none;
    border-radius: 5px;
}
.edit_btn {
    text-decoration: none;
    padding: 2px 5px;
    background: #2E8B57;
    color: white;
    border-radius: 3px;
}

.del_btn {
    text-decoration: none;
    padding: 2px 5px;
    color: white;
    border-radius: 3px;
    background: #800000;
}
</style>
</head>
<body>

<h2>Motivational Quotes</h2>

<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>

<?php $results = mysqli_query($db, "SELECT * FROM mquotes"); ?>

<table>
  <tr>
    <th>Quotes</th>
    <th>Author</th>
    <th>Action</th>
  </tr>

  <?php while ($row = mysqli_fetch_array($results)) { ?>
    <tr>
      <td><?php echo $row['quotes']; ?></td>
      <td><?php echo $row['author']; ?></td>
      <td>
       <a href="Ncreatequote.php?edit=<?php echo $row['id']; ?>" class="edit_btn" data-toggle="modal" data-target="#myEdit"> Edit</a>

      
        <a href="serverquotes.php?del=<?php echo $row['id']; ?>" class="del_btn">Delete</a>
      </td>
    </tr>
  <?php } ?>
</table>

<!-- Modal -->
<div id="myModal" class="modal fade" 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">Add Quote</h4>
      </div>
      <div class="modal-body">
       <form method="post" action="serverquotes.php" >
    <input type="hidden" name="id" value="<?php echo $id; ?>">
    <div class="input-group">
      <label>Quotes</label>
      <input type="text" name="quotes" value="<?php echo $quotes; ?>">
    </div>
    <div class="input-group">
      <label>Author</label>
      <input type="text" name="author" value="<?php echo $author; ?>">
    </div>
    <div class="modal-footer">
      
      <button class="btn btn-default" type="submit" name="save" >Save</button>
    </div>
  </form>
      </div>
    </div>

  </div>
</div>

<!-- Modal -->
<div id="myEdit" class="modal fade" 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">Edit Quote</h4>
      </div>
      <div class="modal-body">
      <form method="post" action="serverquotes.php" >
    <input type="hidden" name="id" value="<?php echo $id; ?>">
    <div class="input-group">
      <label>Quotes</label>
      <input type="text" name="quotes" value="<?php echo $quotes; ?>">
    </div>
    <div class="input-group">
      <label>Author</label>
      <input type="text" name="author" value="<?php echo $author; ?>">
    </div>
    <div class="input-group">
      
      <button class="btn" type="submit" name="update" style="background: #556B2F;" >update</button>
      
    </div>
  </form>
      </div>
    </div>

  </div>
</div>


</body>
</html>

 

serverquotes.php
<?php 
    session_start();
    $db = mysqli_connect('localhost', 'root', '', 'serenitydb');

    // initialize variables
    $quotes = "";
    $author = "";
    $id = 0;
    $update = false;

    if (isset($_POST['save'])) {
        $quotes = $_POST['quotes'];
        $author = $_POST['author'];

        mysqli_query($db, "INSERT INTO mquotes (quotes, author) VALUES ('$quotes', '$author')"); 
        $_SESSION['message'] = "New quote has been added"; 
        header('location: Ncreatequotes.php');
    }


    if (isset($_POST['update'])) {
    $id = $_POST['id'];
    $quotes = $_POST['quotes'];
    $author = $_POST['author'];

    mysqli_query($db, "UPDATE mquotes SET quotes='$quotes', author='$author' WHERE id=$id");
    $_SESSION['message'] = "Quote is updated!"; 
    header('location: Ncreatequotes.php');
    }

    if (isset($_GET['del'])) {
    $id = $_GET['del'];
    mysqli_query($db, "DELETE FROM mquotes WHERE id=$id");
    $_SESSION['message'] = "The quote is deleted!"; 
    header('location: Ncreatequotes.php');
    }
 
?>
<?php 
    session_start();
    $db = mysqli_connect('localhost', 'root', '', 'serenitydb');

    // initialize variables
    $quotes = "";
    $author = "";
    $id = 0;
    $update = false;

    if (isset($_POST['save'])) {
        $quotes = $_POST['quotes'];
        $author = $_POST['author'];

        mysqli_query($db, "INSERT INTO mquotes (quotes, author) VALUES ('$quotes', '$author')"); 
        $_SESSION['message'] = "New quote has been added"; 
        header('location: Ncreatequotes.php');
    }


    if (isset($_POST['update'])) {
    $id = $_POST['id'];
    $quotes = $_POST['quotes'];
    $author = $_POST['author'];

    mysqli_query($db, "UPDATE mquotes SET quotes='$quotes', author='$author' WHERE id=$id");
    $_SESSION['message'] = "Quote is updated!"; 
    header('location: Ncreatequotes.php');
    }

    if (isset($_GET['del'])) {
    $id = $_GET['del'];
    mysqli_query($db, "DELETE FROM mquotes WHERE id=$id");
    $_SESSION['message'] = "The quote is deleted!"; 
    header('location: Ncreatequotes.php');
    }

?>
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像