0

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');
}

?>
Whillis
  • 31
  • 6
  • Did you check inside your Database that content is empty or not? You can check it using phpmyadmin easily. – Masood Oct 07 '19 at 15:59
  • 1
    Your code is [open to SQL injection](https://xkcd.com/327/). [Use PDO and parameters](https://stackoverflow.com/a/60496/6089612). This is very urgent for you!! – Don't Panic Oct 07 '19 at 16:04
  • @Massod Aslami yes i already added some information through the form everything works its just not showing up the content this code is just a work from school so im not worrying about that so much – Whillis Oct 07 '19 at 19:29

2 Answers2

1

textarea does not have a value attribute.
Try changing it to this and see if it works

<div class="input-group">
    <textarea name="content" placeholder="Content" >
        <?php echo $content; ?>
    </textarea>
</div>
CaffeinatedCod3r
  • 821
  • 7
  • 14
1

You cannot use attribute for textarea elements

replace

<textarea name="content" placeholder="Content" value="<?php echo $content; ?>"></textarea>

with

<textarea name="content" placeholder="Content"><?php echo $content; ?></textarea>

Emre Rothzerg
  • 289
  • 1
  • 5