简体   繁体   中英

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 file that manages the connection to the database and data entry, but when I try I get the following error:

"Fatal error: Uncaught Error: Call to undefined function mysql_connect() in C:\\xampp\\htdocs\\example\\connectivity.php:8 Stack trace: #0 {main} thrown in C:\\xampp\\htdocs\\example\\connectivity.php on line 8"

I post all the code that I wrote. Does anyone know where I'm wrong?

here is the form index.html

    <!DOCTYPE HTML>
<html>

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

    <?php include("connectivity.php"); ?>

</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>

Then the code used to define database and the table:

CREATE DATABASE practice;
USE practice;

CREATE TABLE contact
(
contactID INT(9) NOT NULL auto_increment,
contactName VARCHAR(40) NOT NULL,
contactEmail VARCHAR(40) NOT NULL,
message VARCHAR(250) NOT NULL,
PRIMARY KEY(contactID)
);

Finally the connectivity.php file:

<?php
//connecting to the database
define('DB_HOST', 'localhost');
define('DB_NAME', 'practice');
define('DB_USER','root');
define('DB_PASSWORD','');

$con=mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());
//inserting Record to the database
$name = $_POST['name'];
$email = $_POST['email'];
$message =  $_POST['message'];

$query = "INSERT INTO contact(contactName,contactEmail,message)VALUES('$name','$email','$message')";
$result = mysql_query($query);
if($result)
    {
        echo "Successfully updated database";
    }
    else
    {
     die('Error: '.mysql_error($con));
    }
    mysql_close($con);
?>

PS: I installed the latest version of XAMMP (5.6.15)

$con=mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD,##TABLE NAME##) or die("Failed to connect to MySQL: " . mysql_error());

$query = "INSERT INTO contact(contactName,contactEmail,message)VALUES('$name','$email','$message')"; $result = mysqli_query($con,$query);

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM