Skip to content Skip to sidebar Skip to footer

How To Insert A Record In MySQL With PHP With An HTML Form

I have created a form in html and I would like that the dataentered in the form is sent to a mysql database in XAMMP. I created the database, the table and the connectivity.php fil

Solution 1:

If you are using one of the latest version of xampp therefore you have to use PDO or MySQLi .

Your have to change your codes to something like this.

Your connectivity page

<?php


    $db = new PDO('mysql:host=localhost;dbname=practice;charset=utf8', 
                  'root', 
                  '',
                  array(PDO::ATTR_EMULATE_PREPARES => false,
                  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));


?>
<?php

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

    $name = $_POST['name'];
    $email = $_POST['email'];
    $message =  $_POST['message'];



    $stmt = $db->prepare("INSERT INTO `contact` (contactName,contactEmail,message)
    VALUES (:name, :email, :message)");
    $stmt->bindParam(':name', $name);
    $stmt->bindParam(':email', $email);
    $stmt->bindParam(':message', $message);

    $stmt->execute();

    echo 'added';

}

?>

Your home page

<!DOCTYPE HTML>
<html>

<head>
<title>Contact Us</title>
<link rel="stylesheet" type="text/css" href="style.css">

</head>

<body>
    <div id="contact">
        <h3>Contact Us For Any Query</h3>
        <form method="POST" action="connectivity.php">
            Name
            <br>
            <input type="text" name="name">
            <br> Email
            <br>
            <input type="text" name="email">
            <br> Message
            <br>
            <textarea rows="10" cols="50" maxlength="100" name="message"></textarea>
            <br>
            <input type="submit" value="Send Message">
        </form>
    </div>
</body>

</html>

Hope this helps


Solution 2:

Firts you see your phpinfo:

<?php
    phpinfo();
?>

Then see in here , php_mysql is enabled or disabled?

If there not php_mysql, change php.ini file: Uncomment extension=php_mysql.dll


Post a Comment for "How To Insert A Record In MySQL With PHP With An HTML Form"