28 February 2022

insert data in php with variables and textbox

How To insert Data in Php using PDO with Variable?




After a database and a table have been created,we can start insert records in it.


Here are some syntax you have to follow:


The SQL query must be in quoted.


String values inside the SQL query must be in quoted except numeric value.


id and create_at field  we are not going to define because it is auto-increment and timestamp use INSERT INTO statement to insert records.




here a basic syntax of insert record:


INSERT INTO table_name (field1, field2, field3,...)VALUES (var1, var2, var3,...).




In previous blog we insert data in without variable but this time we add html code and we insert record using html and table named "register" with sixcolumns: "id", "name", "email", "password" ,"mobile_no","create_at".





table Query: 



CREATE TABLE `register` (

 `id` int(11) NOT NULL AUTO_INCREMENT,

 `name` varchar(30) NOT NULL,

 `email` varchar(30) NOT NULL,

 `password` varchar(15) NOT NULL,

 `mobile_no` varchar(10) NOT NULL,

 `create_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,

 PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1



HTML Code:


<!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.0">
    <title>Document</title>
</head>
<body>
    <form action="" method="post">
        <table>
            <tr>
                <td>Name</td>
                <td><input type="text" name="txtname"></td>
            </tr>
            <tr>
                <td>Email</td>
                <td><input type="email" name="txtemail"></td>
            </tr>
            <tr>
                <td>password</td>
                <td><input type="password" name="password"></td>
            </tr>
            <tr>
                <td>Mobile No</td>
                <td><input type="text" name="mobileno"></td>
            </tr>
            <tr>
               
                <td><input type="submit" name="submit"></td>
            </tr>
        </table>
    </form>
</body>
</html>


PHP Code:

<?php

if(isset($_POST["submit"])){
    $server = "localhost";
    $username = "root";
    $password = "";
    $dbname = "demo1";  
    try {
        $conn = new pdo("mysql:host=$server;dbname=$dbname",$username,$password);
       
        $name = $_POST['txtname'];
        $email = $_POST['txtemail'];
        $pwd = $_POST['password'];
        $mobileno = $_POST['mobileno'];
       
        $sql = "INSERT INTO register(name,email,password,mobile_no) VALUES ('$name','$email','$pwd',$mobileno)";
        $conn->exec($sql);
        echo "Insert Data Successfully";
    } catch (\Throwable $th) {
       
        echo "$sql" . "<br>". $th->getMessage();
    }
    $conn = null;

}

?>


you can also watch our video if you are not understand:





No comments:

Post a Comment

If You have any doubt, or want to know about something, please let me know

how to create custom plugin deactivate hook and display all post in wordpress?

 how to create custom plugin deactivate hook and display all post in wordpress? make new folder in plugin after whatever you want your folde...