Insert Data in sql database using php

How to insert data in sql database using PHP?
In my previews post i show how to create connection with database using php and now in this post i show how to add data in database using php.

so for this first we need a table in database say login which have two columns user and password and we try to insert data in them so we use html form to get data from user and by using post method we get that data in php and then insert it in sql using sql query.


<?php
include "conn.php";

$a=$_POST['name'];
$b=$_POST['password'];

$sql = "INSERT INTO login (usar,pasword)
VALUES ('".$a."','".$b."'');";



if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";

} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>


we include the conn.php file here because we create connection in this file to learn how to create connection see create-connection-with-sql-database-in.html

No comments

Powered by Blogger.