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));


?><?phpif (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><linkrel="stylesheet"type="text/css"href="style.css"></head><body><divid="contact"><h3>Contact Us For Any Query</h3><formmethod="POST"action="connectivity.php">
            Name
            <br><inputtype="text"name="name"><br> Email
            <br><inputtype="text"name="email"><br> Message
            <br><textarearows="10"cols="50"maxlength="100"name="message"></textarea><br><inputtype="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"