So i have a code of a CRUD form where i can publish topics with titles and edit/update and exclude, everything is working , the only problem im having is with the update of the topic content, in which when i click to change an already published topic, it only displays the topic title, but not the content, it still works though if i add another content and update, it will normally change the previously published subject for the one i added, i've been trying to find an answer but its difficult since im still just a beginner with php, most of the code was taken from the internet which i change the somethings to fit my idea.
This is the update php code.
<?php include('connect.php');
// get values to update
if (isset($_GET['edit'])) {
$id = $_GET['edit'];
$update = true;
$rec = mysqli_query($db,"SELECT * FROM news WHERE id=$id");
$record = mysqli_fetch_array($rec);
$title = $record['title'];
$content = $record['content'];
$id = $record['id'];
}
?>
The html
<form method="post" action="connect.php" >
<div class="input-group">
<input type="hidden" name="id" value="<?php echo $id; ?>">
</div>
<div class="input-group">
<input type="text" name="title" placeholder="Title" value="<?php echo $title; ?>">
</div>
<div class="input-group">
<textarea name="content" placeholder="Content" value="<?php echo $content; ?>"></textarea>
</div>
<!-- form buttons -->
<div class="input-group">
<?php if ($update == true): ?>
<button class="btn" type="submit" name="update">Update</button>
<button class="btn" type="button" value="cancel" onclick="history.back();">Cancel</button>
<?php else: ?>
<button class="btn" type="submit" name="send">Send</button>
<?php endif ?>
</div>
</form>
<table>
<div class="form">
<?php while ($row = mysqli_fetch_array($results)) { ?>
<tr style="border:0;font-size:30px;";>
<td><b><?php echo $row['title']; ?></b></td>
</tr>
<tr style="border-radius: 5px;background-color: #ebebeb;">
<td><?php echo $row['content']; ?></td>
</tr>
<!-- Delete/edit buttons -->
<tr style="border:0;">
<td>
<a href="news.php?edit=<?php echo $row['id']; ?>" class="fix_btn" >Edit</a>
<a style="float:right;" href="news.php?del=<?php echo $row['id']; ?>" class="fix_btn"
onclick="return confirm('Are you sure you want to delete this topic?')">Delete</a>
</td>
</tr>
<tr style="height:60px; border:0;"><td></td></tr>
<?php } ?>
</div>
</table>
database connection (connect.php)
<?php
session_start();
$db = mysqli_connect('localhost', 'root', '', 'orbita');
// initialize variables
$title = "";
$content = "";
$id = 0;
$update = false;
if (isset($_POST['send'])) {
$title = $_POST['title'];
$contet = $_POST['content'];
mysqli_query($db, "INSERT INTO news (title, content) VALUES ('$title', '$content')");
$_SESSION['message'] = "Success!";
header('location: news.php');
}
if (isset($_POST['update'])) {
$id = $_POST['id'];
$title = $_POST['title'];
$content = $_POST['content'];
mysqli_query($db, "UPDATE news SET title ='$title', content ='$content' WHERE id=$id");
$_SESSION['message'] = "Success update!";
header('location: news.php');
}
if (isset($_GET['del'])) {
$id = $_GET['del'];
mysqli_query($db, "DELETE FROM news WHERE id=$id");
$_SESSION['message'] = "Message deleted";
header('location: news.php');
}
?>