简体   繁体   中英

how to edit the last inserted data in mysql database?

Hi guys all I am trying to do here is to be able to update the original comments. but with the way my code looks like now, it just keeps adding new content with new row id. How can I delete the original content form the same row id and update it with the new content?

<?php 
require 'classes/Database.php';
$database = new Database;
$post = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);

if(isset($post['submit'])){
    $title = $post['title'];
    $body = $post['body'];

    $database->query('INSERT INTO posts (title, body) VALUES(:title, :body)');
    $database->bind(':title', $title);
    $database->bind(':body', $body);
    $database->execute();
    if($database->lastInsertId()){
       echo '<p>Post Added!</p>';
    }
} 
if(isset($post['submit'])){
        $id = $database->lastInsertId();
        $title = $post['title'];
        $body = $post['body'];
        $database->query('UPDATE posts SET title = :title, body = :body WHERE id = :id');
        $database->bind(':title', $title);
        $database->bind(':body', $body);
        $database->bind(':id', $id);
        $database->execute();
        header('location:index.php');
 }


if(isset($post['delete'])){
    $delete_id = $_POST['delete_id'];
    $database->query('DELETE FROM posts WHERE id = :id');
    $database->bind(':id', $delete_id);
    $database->execute();
}


$database->query('SELECT * FROM posts');
$rows = $database->resultset();
?>
<h1>Add Post</h1>
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
    <label>Post ID</label><br />
    <input type="text" name="id" placeholder="Specify ID" /><br /><br />
    <label>Post Title</label><br />
    <input type="text" name="title" placeholder="Add a Title..." /><br /><br  
/>
<label>Post Body</label><br />
<textarea name="body"></textarea><br /><br />
<input type="submit" name="submit" value="Submit" />

   <h1>Posts</h1>
    <div>
   <?php foreach($rows as $row) : ?>
    <div>
    <h3><?php echo $row['title']; ?></h3>
    <p><?php echo $row['body']; ?></p>
    <br />
    <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
        <input type="hidden" name="delete_id" value="<?php echo $row['id'];   
     ?>">
        <input type="submit" name="delete" value="Delete" />
      </form>
    </div>
   <?php endforeach; ?>
  </div>

At first glance it looks like you are never getting to the UPDATE because your if and elseif have the same condition, isset($post['submit']). I don't know what else is in your code but you need to decide if this is an edit using some other logic.

you should use value of lastInsertId after insert.

for example:

if(isset($post['submit'])){
    $title = $post['title'];
    $body = $post['body'];

    $database->query('INSERT INTO posts (title, body) VALUES(:title, :body)');
    $database->bind(':title', $title);
    $database->bind(':body', $body);
    $database->execute();
    $lastId = $database->lastInsertId();
    if($lastId){
       echo '<p>Post Added!</p>';
    }
}

and then:

...
$database->bind(':id', $lastId);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM