Insert Contact Information in PHP/MySQL Using PDO
Submitted by alpha_luna on Tuesday, May 24, 2016 - 12:05.
Insert Contact Information
If you are looking for Insert Contact Information tutorial then you are at the right place. In this article, we are going to learn about on how to add contact information in the database and you can see all your contact on your page.Let's start with:
Creating our Table
We are going to make our database.- Open the PHPMyAdmin.
- Create a database and name it as "contact_info".
- After creating a database name, then we are going to create our table. And name it as "tbl_member".
- Kindly copy the code below.
- CREATE TABLE `tbl_member` (
- `tbl_member_id` INT(11) NOT NULL,
- `first_name` VARCHAR(100) NOT NULL,
- `last_name` VARCHAR(100) NOT NULL,
- `contact_number` VARCHAR(100) NOT NULL,
- `email` VARCHAR(100) NOT NULL,
- `address` VARCHAR(100) NOT NULL
- ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Database Connection
- <?php
- $conn = new PDO('mysql:host=localhost; dbname=contact_info','root', '');
- ?>
Add Form Field
- <form method="post" action="add_person_query.php">
- <table border="1" cellspacing="5" cellpadding="5" width="100%">
- <tr>
- <td>
- </td>
- <td width="395px">
- <input type="text" class="text_Box" name="first_name" autofocus="autofocus" placeholder="First Name ....." required />
- </td>
- </tr>
- <tr>
- <td>
- </td>
- <td width="395px">
- <input type="text" class="text_Box" name="last_name" placeholder="Last Name ....." required />
- </td>
- </tr>
- <tr>
- <td>
- </td>
- <td width="395px">
- <input type="text" class="text_Box" name="contact_number" placeholder="Contact Number ....." required />
- </td>
- </tr>
- <tr>
- <td>
- </td>
- <td width="395px">
- <input type="email" class="text_Box" name="email" placeholder="Email .....">
- </td>
- </tr>
- <tr>
- <td>
- </td>
- <td width="395px">
- <input type="text" class="text_Box" name="address" placeholder="Address ....." required />
- </td>
- </tr>
- <tr>
- <td colspan="2">
- <a style="margin-right:70px;">
- <button type="submit" class="btn_confirm">
- Save Data
- </button>
- </a>
- <a href="index.php">
- <button type="submit" class="btn_cancel">
- Cancel
- </button>
- </a>
- </td>
- </tr>
- </table>
- </form>
Add - PHP Query Using PDO
- <?php
- include ('db.php');
- $first_name=$_POST['first_name'];
- $last_name=$_POST['last_name'];
- $contact_number=$_POST['contact_number'];
- $email=$_POST['email'];
- $address=$_POST['address'];
- $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $sql = "INSERT INTO tbl_member (first_name, last_name, contact_number, email, address )
- VALUES ('$first_name', '$last_name', '$contact_number', '$email', '$address')";
- echo "<script>alert('Successfully Added!'); window.location='index.php'</script>";
- ?>
Viewing Data
- <table border="1" cellspacing="5" cellpadding="5" width="100%">
- <thead>
- <tr>
- </tr>
- </thead>
- <tbody>
- <?php
- require_once('db.php');
- $result = $conn->prepare("SELECT * FROM tbl_member ORDER BY tbl_member_id ASC");
- $result->execute();
- for($i=0; $row = $result->fetch(); $i++){
- $id=$row['tbl_member_id'];
- ?>
- <tr>
- </tr>
- <?php } ?>
- </tbody>
- </table>
Output:
Adding new Data. Viewing Data. And, that's it. This is the steps on how to insert contact in the database. For my next tutorial, we are going to create on how to Update Contact Information. Kindly click the "Download Code" button for full source code. Thank very much. Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.Add new comment
- 169 views