Posts Details

Here is a detailed
explaination on this course

Php with database

Php / Php with database

How to insert data in the database using php. 

We also have other methods of inserting records into the database but we will focus on this method for now.

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=, initial-scale=1.0">
    <title>Documenttitle>
head>
<body>
    <form action ="#" method="post">
    <input type="text" name="firstname" placeholder= "Enter First Name"> <br>
    <input type="text" name="lastname" placeholder= "Enter Last Name"> <br>
     <input type="submit"  name="submit">

    form>
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myapp";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error){
    die("connection failed".$con->connect_error);
    }

    if(isset($_POST['submit'])){

        $fname = $_POST['firstname'];
        $lname = $_POST['lastname'];

        $sql = "INSERT INTO test (fname, lname) VALUES('$fname','$lname')";
            if ($conn->query($sql) === TRUE){
                echo "insert successfully";
            }else {
                echo "Error: " . $sql . "
"
. $conn->error;
            }
            mysqli_query($conn, $sql);
        }

?>
body>
html>


Leave a comment