沃梦达 / IT编程 / 前端开发 / 正文

php – 将html表单数据发送到mysql数据库

我无法通过php将我的html表单输入数据存储在mysql数据库中.当我填写表单并提交时,我的register.php页面是空白的,成功或错误消息都没有回显到页面上.注意:是的我有root密码和数据库名称.两个都在我的代码中正确填写...

我无法通过php将我的html表单输入数据存储在mysql数据库中.当我填写表单并提交时,我的register.php页面是空白的,成功或错误消息都没有回显到页面上.

注意:是的我有root密码和数据库名称.两个都在我的代码中正确填写我刚删除它们的帖子.

编辑:分号固定,仍然出现未知错误.

HTML格式:

<form action="register.php" method="POST">
                <input type="text" name="first_name" placeholder="first name" /><br /><br />
                <input type="text" name="last_name" placeholder="last name" /><br /><br />
                <input type="text" name="email" placeholder="email address" /><br /><br />
                <input type="text" name="password" placeholder="password" /><br /><br />
                <input type="submit" name="register" value="Sign Up" />
            </form>

register.php

<?php
if (isset($_POST['register'])) {
    $host = "localhost";
    $username = "root";
    $password = "";
    $database = "";
    $conn = mysqli_connect($host, $username, $password, $database);

    if (mysqli_connect_errno($conn)) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $first_name = mysqli_real_escape_string($_POST['first_name']);
    $last_name = mysqli_real_escape_string($_POST['last_name']);
    $email = mysqli_real_escape_string($_POST['email']);
    $password = mysqli_real_escape_string($_POST['password']);
    $date = date("Y-m-d");

    $password = md5($password);

    $sql = "INSERT INTO users (first_name, last_name, email, password, sign_up_date) VALUES ('$first_name', '$last_name', '$email', '$password', '$date')";

    if (!mysqli_query($sql)) {
        die('Error: ' .mysqli_error());
    }

    echo "Entry added successfully";

    mysqli_close($conn); 
} 
?>

解决方法:

试试这个:

<?php
if (isset($_POST['register'])) {
    $host = "localhost";
    $username = "root";
    $password = "";
    $database = ""; //also added semi colon and what database name
    $conn = mysqli_connect($host, $username, $password, $database);

    if (mysqli_connect_errno($conn)) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $first_name = mysqli_real_escape_string($conn,$_POST['first_name']); //I've added $conn inside mysqli_real_escape_string expected 1st param
    $last_name = mysqli_real_escape_string($conn,$_POST['last_name']);
    $email = mysqli_real_escape_string($conn,$_POST['email']);
    $password = mysqli_real_escape_string($conn,$_POST['password']);
    $date = date("Y-m-d");

    $password = md5($password);

    $sql = "INSERT INTO users (first_name, last_name, email, password, sign_up_date) VALUES ('$first_name', '$last_name', '$email', '$password', '$date')";
    $query = mysqli_query($conn, $sql); // I think this will add data in your database.

    if (!$conn) { //I change this from mysql_query($sql)
        die('Error: ' .mysqli_error());
    } else {
       echo "Entry added successfully";

       mysqli_close($conn); 
    }
}

本文标题为:php – 将html表单数据发送到mysql数据库