In this tutorial, I will show you how to submit Summernote content to MySql database.
Summernote is a popular WYSIWYG (What You See Is What You Get) editor that allows users to format and style text, add images and videos, and more. In web applications, it’s often used to allow users to create rich-text content that can be stored in a database and displayed later.
If you’re building a web application that uses Summernote, you may want to store the content created by users in a MySQL database using PHP. In this article, we’ll go over the steps required to accomplish this.
Setting Up the Database
The first step is to create a MySQL database and table to store the Summernote content. We’ll assume you already have a MySQL server installed and running. If not, you can download and install MySQL from the official website.
Create a new database using the following SQL command:
CREATE DATABASE `summernote_example`;
Next, create a table to store the content. We’ll call our table notes
and include columns for the note’s ID, title, and content.
CREATE TABLE `notes` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(255) NOT NULL, `content` longtext NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Creating the HTML Form
Once the database is set up, we can create an HTML form that allows users to enter a title and Summernote content for their note. Here’s an example form:
<form method="POST" action="save-note.php"> <label for="title">Title:</label> <input type="text" name="title" id="title" required> <label for="content">Content:</label> <textarea name="content" id="content"></textarea> <button type="submit">Save Note</button> </form>
Note that we’ve included a name
attribute for each form input so we can reference them in our PHP code later.
Handling the Form Submission with PHP
When the user submits the form, we need to handle the data and store it in the MySQL database. We’ll create a new PHP script called save-note.php
to handle the form submission.
Here’s the PHP code to handle the form submission:
<?php // Connect to the MySQL server $mysqli = new mysqli("localhost", "username", "password", "summernote_example"); // Check for errors if ($mysqli->connect_errno) { die("Failed to connect to MySQL: " . $mysqli->connect_error); } // Escape the form data to prevent SQL injection attacks $title = $mysqli->real_escape_string($_POST['title']); $content = $mysqli->real_escape_string($_POST['content']); // Insert the data into the MySQL database $query = "INSERT INTO notes (title, content) VALUES ('$title', '$content')"; if (!$mysqli->query($query)) { die("Failed to save note: " . $mysqli->error); } // Redirect the user back to the form header("Location: index.html");
Let’s go over this code line by line:
- We create a new instance of the
mysqli
class and connect to the MySQL server. - Check for any errors during the connection process.
- Escape the form data using the
real_escape_string
method to prevent SQL injection attacks. - Construct a MySQL query to insert the data into the
notes
table. - Check for any errors during the query execution.
- Redirect the user back to the form using the
header
function.
Displaying the Summernote Content
Now that we’ve stored the Summernote content in the MySQL database, we can display it on a webpage. We’ll create a new PHP script called view-notes.php
to retrieve the data from the database and display it in an HTML table.
Here’s the PHP code to retrieve and display the Summernote content:
<?php // Connect to the MySQL server $mysqli = new mysqli("localhost", "username", "password", "summernote_example"); // Check for errors if ($mysqli->connect_errno) { die("Failed to connect to MySQL: " . $mysqli->connect_error); } // Retrieve the notes from the MySQL database $query = "SELECT * FROM notes"; $result = $mysqli->query($query); // Check for errors if (!$result) { die("Failed to retrieve notes: " . $mysqli->error); } // Display the notes in an HTML table echo "<table>"; echo "<tr><th>ID</th><th>Title</th><th>Content</th></tr>"; while ($row = $result->fetch_assoc()) { $id = $row['id']; $title = $row['title']; $content = $row['content']; echo "<tr>"; echo "<td>$id</td>"; echo "<td>$title</td>"; echo "<td>$content</td>"; echo "</tr>"; } echo "</table>";
Let’s go over this code line by line:
- We create a new instance of the
mysqli
class and connect to the MySQL server. - Next, we check for any errors during the connection process.
- Then we construct a MySQL query to retrieve all the notes from the
notes
table. - Now execute the query and store the result in the
$result
variable. - We check for any errors during the query execution.
- Loop through each row in the result set using the
fetch_assoc
method. - For each row, we retrieve the
id
,title
, andcontent
values. - Display the values in an HTML table row.
- Repeat steps 7-8 for each row in the result set.
- We close the HTML table.
Overview
In this article, we’ve gone over the steps required to store Summernote content in a MySQL database using PHP. We’ve covered creating the database and table, creating the HTML form, handling the form submission with PHP, and displaying the Summernote content on a webpage.
With this knowledge, you can now build web applications that allow users to create rich-text content that can be stored and displayed later.
Now that you have an idea of what it is all about, go to the next page for finished codes to see it in action.
4 Comments
Your videos are really helpful. Thank you.
Would it be possible to create a video showing to read a summernote record from a MySQL database and put it into a summernote so that the text can be edited further, please? Apologies if you have covered this already and my brain cell (singular) missed it.
Thanks again 🙂
HI Andy, if I don’t have it I sure will. Basically it is just updating the database instead of inserting the content. I believe on this on I only showed how to insert the content. https://a1websitepro.com/how-to-submit-summernote-content-to-mysql-database-in-php/ However, I ahve been so busy lately but Ill try to make some time for this.
Thanks for getting back to me. I worked it out and will happily share the code with you. The trick was to make sure that there is no accidental (or intentional) code injection. There is htmlspecialcharaters, but that leaves out some things I would rather have hidden. I wrote my own short routine in PHP to do this (it’s only a .replace() for each character). The Summernote can then be saved in a text field in the ordinary way.
Thank you for sharing. 🙂 Glad you got it.