1

Currently I'm busy with a simple CKeditor notepad for webapplication. I already have code to save the user text into the database.

Now I want to add code that will retrieve the latest saved (latest id) text from the database, so the user can continue his/her work.

<?php 
    if(isset($_POST['editor1'])) {
        $text = $_POST['editor1'];

        $conn = mysqli_connect("$dbhost", "$dbuser", "$dbpass", "$db") or die("ERROR");

        $query = mysqli_query($conn, "INSERT INTO content (content) VALUES ('$text')");

        if($query)
            echo "Succes!";
        else
            echo "Failed!"; 
    }
?>

this is the code to save the users text.

Now I want to build code that will retrieve the latest saved text from the database, but I can't make a start with my code.

<textarea name="editor1" id="editor1" rows="10" cols="80">

    <?php 
        $conn = mysqli_connect("$dbhost", "$dbuser", "$dbpass", "$db") or die("ERROR");

        $sql = "SELECT content from content";           
    ?>

</textarea>

This is what I currently have.

1 Answer 1

2

You need to execute your query by using mysqli_query() and also need to fetch data by using mysqli_fetch_assoc() as:

Example:

<textarea name="editor1" id="editor1" rows="10" cols="80">
<?php
$sql = "SELECT `content` FROM `content`";
$query = mysqli_query($conn,$sql);
$result = mysqli_fetch_assoc($query);
echo $result['content']; // will print your content.
?>
</textarea>

UPDATE 1:

For fetching latest record than you can use ORDER BY with LIMIT 1 in your query as:

$sql = "SELECT `content` FROM `content` ORDER BY id DESC LIMIT 1"; // assuming id is your primary key column.
Sign up to request clarification or add additional context in comments.

2 Comments

Hey! Now the text in the textarea is the first id from my database, only I want to display the latest id that is saved in the database.
@SmashingJummy: use ORDER BY id DESC

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.