php - 在 mySQL 数据库中插入 HTML 数据

标签 php html mysql

我写了一个 HTML 表单填写页面(sample.html),我也写了一个从 HTML 页面插入数据的 PHP(sample.php)代码,但是我无法收到正确的消息给我,请解释其他人!!!

这是 php 代码:-

<?php

$connection = mysql_connect('localhost', 'root','');

if (!$connection){
    die("Database Connection Failed" . mysql_error());
}

$select_db = mysql_select_db( "selva",$connection);

if (!$select_db){
    die("Database Selection Failed" . mysql_error());
}

error_reporting(0);

session_start();

$first_name = $_POST['first_name'];
$father_name = $_POST['father_name'];
$gender = $_POST['gender'];
$date_of_birth = $_POST['date_of_birth'];
$sports = $_POST['sports'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];

if($first_name!='' and $father_name!='' and $gender!='' and $date_of_birth!='' and $sports!='' and $mobile!='' and $email!='') {

     $query = mysql_query("insert into register(first_name, father_name, gender, date_of_birth, sports, mobile, email) values('$first_name', '$father_name', '$gender', '$date_of_birth','$sports','$mobile','$email')");

     echo "<br/><br/><span>Data Inserted successfully...!!</span>";

 } else {
     echo "<p>Failed to Insert data <br/> Some Fields are Blank....!!</p>";
 }

 mysql_close($connection); 

 ?>

请问如何将HTML页面的数据插入数据库??

最佳答案

试试这个..

use <form action="sample.php" method="post">

<?php

$connection = mysqli_connect('localhost', 'root','');

if (!$connection){
    die("Database Connection Failed" . mysql_error());
} else {
    $select_db = mysqli_select_db( "selva",$connection);

    if (!$select_db){
        die("Database Selection Failed" . mysql_error());
    } else {
        error_reporting(0);

        session_start();

        $first_name = $_POST['first_name'];
        $father_name = $_POST['father_name'];
        $gender = $_POST['gender'];
        $date_of_birth = $_POST['date_of_birth'];
        $sports = $_POST['sports'];
        $mobile = $_POST['mobile'];
        $email = $_POST['email'];

        if($first_name!='' and $father_name!='' and $gender!='' and $date_of_birth!='' and $sports!='' and $mobile!='' and $email!='') {

           mysqli_query($connection, "insert into register(first_name, father_name, gender, date_of_birth, sports, mobile, email) values('$first_name', '$father_name', '$gender', '$date_of_birth','$sports','$mobile','$email')");

            if(mysqli_affected_rows($connection) > 0){
                echo "<p>Data Successfully add Added</p>";
            } else {
                echo "Employee NOT Added<br />";
                echo mysqli_error ($connection);
            }
         } else {
             echo "<p>Failed to Insert data <br/> Some Fields are Blank....!!</p>";
         }

         mysql_close($connection);
    }
}

?>

关于php - 在 mySQL 数据库中插入 HTML 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40419705/

相关文章:

html - 如何使用 CSS 和 <div> 让文本出现在 <td> 的底部?

html - 样式化 html 标签是不好的做法吗?

php - 从数据库表中提取字段数据的简单函数

php - 使用 PHP 回显 JSON 响应

php - NSData(if 语句)

jquery - Bootstrap 3 sm vs xs - webstorm 改变断点?

java - 将大型 JSON 文件中的数据插入 MySQL 数据库时出现 OutOfMemoryError

mysql - MySQL 中区分大小写的查询

php - 在 PHP 中,diff : $var2=$var1 ; $var2=&$var1; 是什么

php 将子目录的内容写入单独的文本文件