|
| 1 | +<?php |
| 2 | +require_once 'config/config.php'; |
| 3 | +require_once 'config/db.php'; |
| 4 | + |
| 5 | +// Check For Submit |
| 6 | +if (isset($_POST['update_id'])) { |
| 7 | +// Get Form Data |
| 8 | +$update_id = mysqli_real_escape_string($conn, $_POST['update_id']); |
| 9 | +$title = mysqli_real_escape_string($conn, $_POST['title']); |
| 10 | +$body = mysqli_real_escape_string($conn, $_POST['body']); |
| 11 | +$author = mysqli_real_escape_string($conn, $_POST['author']); |
| 12 | + |
| 13 | +$query = "UPDATE post SET |
| 14 | +title='$title', |
| 15 | +author='$author', |
| 16 | +body='$body' |
| 17 | +WHERE id={$update_id}"; |
| 18 | + |
| 19 | +if(mysqli_query($conn, $query)) { |
| 20 | +header('Location: '.ROOT_URL.''); |
| 21 | +} else { |
| 22 | +echo 'ERROR: '.mysqli_error($conn); |
| 23 | +} |
| 24 | +} |
| 25 | + |
| 26 | +// Get Id |
| 27 | +$id = mysqli_real_escape_string($conn, $_GET['id']); |
| 28 | + |
| 29 | +// Create Query |
| 30 | +$query = 'SELECT * FROM post WHERE id='.$id; |
| 31 | + |
| 32 | +// Get Result |
| 33 | +$result = mysqli_query($conn, $query); |
| 34 | + |
| 35 | +// Fetch Data |
| 36 | +$post = mysqli_fetch_assoc($result); |
| 37 | +// var_dump($posts); |
| 38 | + |
| 39 | +// Free Result |
| 40 | +mysqli_free_result($result); |
| 41 | + |
| 42 | +// Close Connection |
| 43 | +mysqli_close($conn); |
| 44 | +?> |
| 45 | +<?php include_once 'inc/header.php'; ?> |
| 46 | +<div class="container"> |
| 47 | +<h1>Add Post</h1> |
| 48 | +<form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST"> |
| 49 | +<div class="form-group"> |
| 50 | +<label>Title</label> |
| 51 | +<input type="text" name="title" class="form-control" value="<?php echo $post['title']; ?>"> |
| 52 | +</div> |
| 53 | +<div class="form-group"> |
| 54 | +<label>Author</label> |
| 55 | +<input type="text" name="author" class="form-control" value="<?php echo $post['author']; ?>"> |
| 56 | +</div> |
| 57 | +<div class="form-group"> |
| 58 | +<label>Body</label> |
| 59 | +<textarea name="body" class="form-control"><?php echo $post['body']; ?></textarea> |
| 60 | +</div> |
| 61 | +<input type="hidden" name="update_id" value="<?php echo $post['id']; ?>"> |
| 62 | +<input type="submit" name="submit" value="Submit" class="btn btn-primary"> |
| 63 | +</form> |
| 64 | +</div> |
| 65 | +<?php include_once 'inc/footer.php'; ?> |
0 commit comments