Affichage des articles dont le libellé est Insert Data Into MySQL Database using php. Afficher tous les articles
Affichage des articles dont le libellé est Insert Data Into MySQL Database using php. Afficher tous les articles

PHP Pdo Code : How To Insert Data Into MySQL Database From Form Inputs Text Using Php

Php Code To Insert Form Data Into Mysql Database Using Pdo .


________________________________________________________

In this Php Tutorial we will Leran How To Insert Data Into MySQL Database Table From Form Inputs In Php using PDO .
I Use In This Tutorial:
- NetBeans IDE .
- XAMPP .
- PhpMyAdmin .


 




Php Source Code:


<?php

// php code -> 1bestcsharp.blogspot.com
// php insert data to mysql database using PDO

if(isset($_POST['insert']))
{
    try {

        // connect to mysql

        $pdoConnect = new PDO("mysql:host=localhost;dbname=test_db","root","");
    } catch (PDOException $exc) {
        echo $exc->getMessage();
        exit();
    }

    // get values form input text and number
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $age = $_POST['age'];
    
    // mysql query to insert data

    $pdoQuery = "INSERT INTO `users`(`fname`, `lname`, `age`) VALUES (:fname,:lname,:age)";
    
    $pdoResult = $pdoConnect->prepare($pdoQuery);
    
    $pdoExec = $pdoResult->execute(array(":fname"=>$fname,":lname"=>$lname,":age"=>$age));
    
        // check if mysql insert query successful
    if($pdoExec)
    {
        echo 'Data Inserted';
    }else{
        echo 'Data Not Inserted';
    }
}


?>

<!DOCTYPE html>

<html>

    <head>

        <title>PHP INSERT DATA USING PDO</title>

        <meta charset="UTF-8">

        <meta name="viewport" content="width=device-width, initial-scale=1.0">

    </head>
    <body>
        <form action="php_insert_data_in_mysql_database_using_pdo.php" method="post">

            <input type="text" name="fname" required placeholder="First Name"><br><br>

            <input type="text" name="lname" required placeholder="Last Name"><br><br>

            <input type="number" name="age" required placeholder="Age" min="10" max="100"><br><br>

            <input type="submit" name="insert" value="Insert Data">

        </form>

    </body>

</html>



///////////////OUTPUT:



php insert mysql
Php Insert Form
php insert data into mysql database
Age Must Be Between 10 And 100 
                     




PHP MySQLI Code : How To Insert Data Into MySQL Database From Form Inputs Text Using Php

Php Insert Form Data Into Mysql Database Using MySQLI Code.


________________________________________________________

In this Php Tutorial we will Learn How To Insert Data Into MySQL Database Table From Form Inputs In Php using MySQLI .
I Use In This Tutorial:
- NetBeans IDE .
- XAMPP .
- PhpMyAdmin .


 




Php Source Code:


<?php

// php code to Insert data into mysql database from input text
if(isset($_POST['insert']))
{
    $hostname = "localhost";
    $username = "root";
    $password = "";
    $databaseName = "test_db";
    
    // get values form input text and number

    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $age = $_POST['age'];
    
    // connect to mysql database using mysqli

    $connect = mysqli_connect($hostname, $username, $password, $databaseName);
    
    // mysql query to insert data

    $query = "INSERT INTO `users`(`fname`, `lname`, `age`) VALUES ('$fname','$lname','$age')";
    
    $result = mysqli_query($connect,$query);
    
    // check if mysql query successful

    if($result)
    {
        echo 'Data Inserted';
    }
    
    else{
        echo 'Data Not Inserted';
    }
    
    mysqli_free_result($result);
    mysqli_close($connect);
}

?>

<!DOCTYPE html>

<html>

    <head>

        <title> PHP INSERT DATA </title>

        <meta charset="UTF-8">

        <meta name="viewport" content="width=device-width, initial-scale=1.0">

    </head>

    <body>
        <form action="php_insert_data_in_mysql_database.php" method="post">

            <input type="text" name="fname" required placeholder="First Name"><br><br>

            <input type="text" name="lname" required placeholder="Last Name"><br><br>

            <input type="number" name="age" required placeholder="Age" min="10" max="100"><br><br>

            <input type="submit" name="insert" value="Add Data To Database">

        </form>

    </body>

</html>



///////////////OUTPUT:



php mysql insert
Php Form Insert

php insert data into mysql
Age Must Be Between 10 And 100