You may find a hell lot of tutorials on this topic, but may not work on your system because of what
Note: For Beginner!
It'll be very short and simple, so that you can understand the whole code easily!
For this I will be using XAMPP (Apache) as a server ,specifically, version 5.5.37 , you may want to download it if you are going to follow this tutorial!
Depending on the Operating System you are using, you should download them!
For Windows User:
Just Install It! Hope you know how to install!
For Linux Users:
Now, to install into your system,
Go to Terminal.
Give the following commands:
I will not talk about Mac version as I haven't done on it!
Now, we can begin the coding! All our code should be located inside the
If you wonder where
For Windows user, the
For Linux users, it is inside
For this data entry, we will create only one file!
But, before that we will first explain each part one by one!
To enter data into the database we need to connect to the database first! And to connect to a database, we must have a database! LOL!
So, Lets create one database by going to
Let's go to the
Now, we have our database
Connection to the database can be created in a number of ways;
The connection to database is created as below:
Before going further, lets create a simple html form!
Now if any field is empty, we will notify that a field is empty! For that, we will require an array where we will store the error message!
To check whether a field is empty, we will use
Now, if the error[] array is empty, we will insert data from html form into the database as below:
Now, we can say that we are done!
Lets take a look at the while code.
Name this file anything as you wanted (should be .php extension) and open your browser and go to
Enter any data, if there is anything wrong, please say it in the comment section!
PHP
version you are using! So, I'd also like to share a simple data entry into MySQL
database using PHP
! It may work with your system! :DNote: For Beginner!
It'll be very short and simple, so that you can understand the whole code easily!
For this I will be using XAMPP (Apache) as a server ,specifically, version 5.5.37 , you may want to download it if you are going to follow this tutorial!
Depending on the Operating System you are using, you should download them!
For Windows User:
Just Install It! Hope you know how to install!
For Linux Users:
Now, to install into your system,
Go to Terminal.
Give the following commands:
cd /path-to-your-file
sudo chmod +x xampp-linux-x64-5.5.37-0-installer.run
sudo ./xampp-linux-x64-5.5.37-0-installer.run
I will not talk about Mac version as I haven't done on it!
Now, we can begin the coding! All our code should be located inside the
htdocs
folder of your xampp/lampp installation directory i.e. our workspace will be htdocs
folder!If you wonder where
htdocs
folder is;For Windows user, the
htdocs
folder is usually inC:/xampp/htdocs
For Linux users, it is inside
/opt/lampp/htdocs
For this data entry, we will create only one file!
But, before that we will first explain each part one by one!
To enter data into the database we need to connect to the database first! And to connect to a database, we must have a database! LOL!
So, Lets create one database by going to
http://127.0.0.1/phpmyadmin
or http://localhost/phpmyadmin
. Lets create one new database called directory
!
Let's go to the
SQL
tab and enter the following query!CREATE TABLE `members` ( `id` int(11) NOT NULL, `name` varchar(50) NOT NULL, `phone` varchar(12) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Now, we have our database
directory
with a table members
! Lets start the connection!Connection to the database can be created in a number of ways;
mysqli (procedural)
, mysqli (object oriented)
and pdo
! Now, we are going to use procedural mysqli! The connection to database is created as below:
<?php $dbcon = @mysqli_connect("localhost","root","","directory"); if($dbcon){ echo ""; } else{ echo "Error!"; } ?>
Before going further, lets create a simple html form!
<!DOCTYPE html> <html> <head> <title>Simple Data Entry</title> </head> <body style="max-width:600px;margin:auto;align:justify;padding:8px;"> <form action="entry.php" method="post"> <legend>PLEASE FILL UP!</legend><br/> <label for="name">ENTER NAME:</label><br/> <input type="text" name="name"><br/> <label for="phone">ENTER PHONE NUMBER:</label><br/> <input type="tel" name="phone"><br/> <br/> <input type="hidden" name="submitinfo" value="TRUE"> <input type="submit"> </form> </body> </html>
Now if any field is empty, we will notify that a field is empty! For that, we will require an array where we will store the error message!
$error = array();
To check whether a field is empty, we will use
empty()
library function as below:if(!empty($_POST['name'])) $name = $_POST['name']; else $error[] = "Please Enter Name!";
Now, if the error[] array is empty, we will insert data from html form into the database as below:
$query = "INSERT INTO `members` (`name`,`phone`) VALUES('$name','$phone')"; $insert = mysqli_query($dbcon,$query);
Now, we can say that we are done!
Lets take a look at the while code.
<?php //Lets connect the database.... $dbcon = @mysqli_connect("localhost","root","","directory"); if($dbcon){ echo ""; } else{ echo "Error!"; } //create one array to store error message.... $error = array(); //Let assign our html form data to our variables... if(isset($_POST['submitinfo'])){ //if submit button is clicked! if(!empty($_POST['name'])) $name = $_POST['name']; else $error[] = "Please Enter Name!"; if(!empty($_POST['phone'])) $phone = $_POST['phone']; else $error[] = "Please Enter Phone Number!"; if(empty($error)){ $query = "INSERT INTO `members` (`name`,`phone`) VALUES('$name','$phone')"; $insert = mysqli_query($dbcon,$query); if(!$insert){ echo "Query Failed!"; } if(mysqli_affected_rows($dbcon)==1){ echo "New Data Inserted!"; }else{ echo "Error!"; } }else{ echo '<ol>'; foreach ($error as $key => $values) { echo ' <li>'.$values.'</li>'; } echo '</ol>'; } mysqli_close($dbcon); } ?> <!-- html form --> <!DOCTYPE html> <html> <head> <title>Simple Data Entry</title> </head> <body style="max-width:600px;margin:auto;align:justify;padding:8px;"> <form action="<?php $_PHP_SELF; ?>" method="post"> <legend>PLEASE FILL UP!</legend><br/> <label for="name">ENTER NAME:</label><br/> <input type="text" name="name"><br/> <label for="phone">ENTER PHONE NUMBER:</label><br/> <input type="tel" name="phone"><br/> <br/> <input type="hidden" name="submitinfo" value="TRUE"> <input type="submit"> </form> </body> </html>
Name this file anything as you wanted (should be .php extension) and open your browser and go to
http://localhost/your_file_name.php
!
Enter any data, if there is anything wrong, please say it in the comment section!
Very true and inspiring article. I strongly believe all your points. I also learnt a lot from your post. Cheers and thank you for the clear path.
ReplyDeletePHP Institutes in Chennai
PHP Training Center in Chennai
This comment has been removed by a blog administrator.
ReplyDelete