简体   繁体   English

如何在PHP中使用HTML表单在MySQL中插入记录

[英]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. 我已经在html中创建了一个表单,我希望将表单中输入的数据发送到XAMMP中的mysql数据库。 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: 我创建了数据库,表和管理与数据库和数据条目的连接的connection.php文件,但是当我尝试时,出现以下错误:

"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" “致命错误:未捕获的错误:调用C:\\ xampp \\ htdocs \\ example \\ connectivity.php:8中未定义的函数mysql_connect()堆栈跟踪:#0 {main}抛出了C:\\ xampp \\ htdocs \\ example \\ connectivity。第8行的php”

I post all the code that I wrote. 我发布了我编写的所有代码。 Does anyone know where I'm wrong? 有人知道我错了吗?

here is the form index.html 这是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: 最后,connection.php文件:

<?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) PS:我安装了最新版本的XAMMP(5.6.15)

$con=mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD,##TABLE NAME##) or die("Failed to connect to MySQL: " . mysql_error()); $ con = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,## TABLE NAME ##)或die(“无法连接到MySQL:”。mysql_error());

$query = "INSERT INTO contact(contactName,contactEmail,message)VALUES('$name','$email','$message')"; $ query =“在联系人中插入INERT(contactName,contactEmail,message)VALUES('$ name','$ email','$ message')”; $result = mysqli_query($con,$query); $结果= mysqli_query($ con,$ query);

Firts you see your phpinfo: 首先,您会看到您的phpinfo:

<?php
    phpinfo();
?>

Then see in here , php_mysql is enabled or disabled? 然后在这里看到, php_mysql是启用还是禁用?

If there not php_mysql , change php.ini file: Uncomment extension=php_mysql.dll 如果没有php_mysql ,请更改php.ini文件:Uncomment extension=php_mysql.dll

If you are using one of the latest version of xampp therefore you have to use PDO or MySQLi . 如果您使用的是xampp的最新版本,则必须使用PDO或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 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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