SmashingJummy SmashingJummy - 7 months ago 58
PHP Question

CKeditor, retrieve user text from database

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.

Answer

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 in your query as:

$sql = "SELECT `content` FROM `content` ORDER BY id DESC"; // assuming id is your primary key column.
Comments