PHP Insert/add Values in database
In previous post we read rows from database hope you have done with that if not i suggest you to read this http://www.hardiktech.com/2018/02/php-database-retrive.html First. Now next we are going to insert values in database for this follow these stepsStep 1: First of all start your wamp/xampp server and open phpmyadmin tool normally available with both,
For this open http://localhost/phpmyadmin in your browser.
Step 2: Second click on databases tab and create a database with any name like testdb.
Step 3: Now click on Your Database name appeared under database tab of phpmyadmin and a new a table named members and add 4 colums.
Step 4 : write field name as id,name, email, password type and size and make id column as primary key and auto increment as shown in image.
Step 5:create a new php document and name it as regform.php and save it in server root , in case using wamp save it in www directory or in htdoc when using xampp.
Step 6: write following code in reg form.
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/><title>Registration Form</title></head><body><div class="container"><div class="row"><h1>Registation Form</h1><form action="register.php" method="post"><input type="text" name="name" id="" class="" placeholder="Name"/><br/><input type="text" name="email" id="" class="" placeholder="yourmail@domain.com"/><br/><input type="password" name="password" id="" class="" placeholder="Password"/><br/><br/><input type="submit" value="Register" name="" id="" class="btn btn-primary" /><br/></form></div></div></body></html>
Step 7 : create a new php document and name it as register.php and save it in server root , in case using wamp save it in www directory or in htdoc when using xampp.
Step 8 : write following code in register.php form.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
echo "$name $email $password";
$conn=mysqli_connect('localhost','root','','jashan');
$sql="insert into members(name,email,password)
values('$name','$email','$password')";
$success=mysqli_query($conn,$sql);
if($success)echo"Register successfully";
?>Step 9 : open browser and see output http://localhost/regform.php
Step 10: Now fill form and insert values in database , to see database you can follow previous tutorial. or can check with localhost/phpmyadmin also you will see output like

Step 10: Now fill form and insert values in database , to see database you can follow previous tutorial. or can check with localhost/phpmyadmin also you will see output like



No comments:
Post a Comment