In this article, we going to see how to save the textbox value to database in PHP. To achieve this we go through each points step by step.

For saving value in database we have to create table in database.

So First point is we going to create table in database.

Let’s start with saving textbox value to database in PHP

To create table in database, execute CREATE STATEMENT SQL query.

Below is the example of query for table name employ.

CREATE TABLE `employ` ( `id` int(11) NOT NULL, `name` varchar(255) NOT NULL, `email` varchar(255) NOT NULL, `phone` varchar(255) NOT NULL, `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP) ENGINE=InnoDB DEFAULT CHARSET=latin1;

After executing above sql query statement table is created.

To understand in a detail regarding SQL query and there operation you check with link MySQL

Table Created
Table Created

From Below script you come to know each and every thing.

<!doctype html><html lang="en"><head> <meta charset="utf-8"> <title>Display table in PHP</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <style>table, td{width:300px;text-align:center;}table, th, td { border: 1px solid black; border-collapse: collapse; padding:5px;}</style></head><body><form action="save.php" method="post" style="border:1px solid #ccc"> <div class="container"> <?php if(isset($error)){ ?> <div style = "font-size:16px; color:#cc0000; margin-top:10px"><?php echo $error; ?></div> <?php }else{ if(isset($_SESSION['msg'])){echo $_SESSION['msg']; } } ?> <h1>ADD DETAILS</h1> <p>Please fill in this form</p> <hr> <label for="name"><b>Name</b></label> <input type="text" placeholder="Enter Name" autocomplete="off" name="name" id="name" required value="<?php echo isset($_SESSION['name'])? $_SESSION['name']:'';?>"> <label for="email"><b>Email</b></label> <input type="text" placeholder="Enter Email" autocomplete="off" name="email" id="email" required value="<?php echo isset($_SESSION['email'])? $_SESSION['email']:'';?>"> <label for="psw"><b>Phone</b></label> <input type="text" placeholder="Phone" name="phone" id="phone" required value="<?php echo isset($_SESSION['phone'])? $_SESSION['phone']:'';?>"> <div class="clearfix"> <button class="btnSearch" name="btnSearch" id="btnSearch" >Save</button> </div> </div> </form> <input type="text" placeholder="Name" autocomplete="off" name="fnname" id="fnname" > <button class="btnSearchone" name="btnSearchone" id="btnSearchone" >Get Save Textbox value</button> <div id="search_results" style="padding:5px;"></div><script type="text/javascript">$(document).ready(function() { //Add a JQuery click listener to our search button. $('#btnSearchone').click(function(){ //get the titlesearch that is being search for //from the search_box. var titlesearch = $('#fnname').val().trim(); //Carry out a GET Ajax request using JQuery $.ajax({ //The URL of the PHP file that searches MySQL. url: 'getdata.php', data: { titlesearch: titlesearch }, success: function(returnData){ $('#search_results').html(''); //Parse the JSON that we got back from search.php var results = JSON.parse(returnData); //Loop through our employee array and append their //names to our search results div. $.each(results, function(key, value){ $('#search_results').append("<table >");$('#search_results').append("<tr>");$('#search_results').append("<th>Name");$('#search_results').append("</th>");$('#search_results').append("<th>Email");$('#search_results').append("</th>");$('#search_results').append("<th>Phone");$('#search_results').append("</th>");$('#search_results').append("</tr>"); $('#search_results').append("<tr>");$('#search_results').append("<td>"+value.name+"</td>");$('#search_results').append("<td>"+value.email+"</div>");$('#search_results').append("<td>"+value.phone+"</div>");$('#search_results').append("</tr>");$('#search_results').append("</table>"); }); //If no employees match the name that was searched for, display a //message saying that no results were found. if(results.length == 0){ $('#search_results').html("<div class='divtitle'>No Jobs with that name were found!</div></br>"); } } }); });});</script></body></html>

In the above script if you will see, I have declare form which consist of textbox, our point is to save the textbox value.

On Submitting this form, i have add form action to save.php , it means that all the form post value is checked in this file.

Below is the script of save file.

From this you will clear regarding save of data to database.

So let’s check with this save file.

<?php /* database configuaration */ define('DB_SERVER', 'localhost'); define('DB_USERNAME', 'root'); define('DB_PASSWORD', ''); define('DB_DATABASE', 'phpdatabase'); $con = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE); session_start(); if($_SERVER["REQUEST_METHOD"] == "POST") { // username and password sent from form if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['phone'])) { $name = $_POST['name']; $email = $_POST['email']; $phone = $_POST['phone']; $sql_Name = mysqli_real_escape_string($con,$name); $sql_Email = mysqli_real_escape_string($con,$email); $sql_Phone = mysqli_real_escape_string($con,$phone); $sql = "SELECT email FROM employ WHERE email = '$sql_Email'"; $result = mysqli_query($con,$sql); $count = mysqli_num_rows($result); if($count == 0) { $sqlinsert = "INSERT INTO employ (name, email , phone)VALUES ('$sql_Name','$sql_Email', '$sql_Phone')"; if (mysqli_query($con, $sqlinsert)) { $_SESSION['name'] = $sql_Name; $_SESSION['email'] = $sql_Email; $_SESSION['phone'] = $sql_Phone; $_SESSION['msg'] = "New record created successfully"; header("location: index.php"); } else { $error = "Error: " . $sqlinsert . "<br>" . mysqli_error($con); } }else { $error = "Email already used"; } } } ?>

With the help of above script we can save textbox value to database.

To check with data of textbox value is inserted is not, i am using Ajax to get record from table which i have inserted.

If you check in above script in which i have declared form.

Below the form i have one input textbox and button.

On click of the button, i am sending value using URL in Ajax.

The URL is path of the PHP file which is called using Ajax.

In SQL query is executed to fetch the record from the table.

Below is script of PHP file getdata.php which is using in Ajax.

<?php //MySQL username.$dbUser = 'root'; //MySQL password.$dbPassword = ''; //MySQL host / server.$dbServer = 'localhost'; //The MySQL database your table is located in.$dbName = 'phpdatabase'; //Connect to MySQL database using PDO.$pdo = new PDO("mysql:host=$dbServer;dbname=$dbName", $dbUser, $dbPassword); //Get the name that is being searched for.$titlesearch = isset($_GET['titlesearch']) ? trim($_GET['titlesearch']) : ''; //The simple SQL query that we will be running.$sql = "SELECT * FROM `employ` WHERE `name` LIKE :titlesearch"; //Add % for wildcard search!$titlesearch = "%$titlesearch%"; //Prepare our SELECT statement.$statement = $pdo->prepare($sql); //Bind the $name variable to our :name parameter.$statement->bindValue(':titlesearch', $titlesearch);//Execute the SQL statement.$statement->execute(); //Fetch our result as an associative array.$results = $statement->fetchAll(PDO::FETCH_ASSOC); //Echo the $results array in a JSON format so that we can//easily handle the results with JavaScript / JQueryecho json_encode($results);?>

After all executing successfully you will get out put as:

FINAL OUTPUT
FINAL OUTPUT

I hope you liked my this article. If you have any queries or any question regarding this, Feel free to comment on Me.