为什么我的插入查询在php中不起作用,并且没有错误

时间:2019-07-19 21:00:18

标签: php mysql sql mysqli

我正在开发一个网站,并且我有插入查询,但是即使没有出现错误,它也不起作用(它没有在数据库中插入数据),我正在使用localhost XAMMP并输入了连接代码在另一页中,这是我的连接代码a和插入代码。我虽然连接有问题,但会打印该语句

<?php

$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$db     = "Driving1";

$conn = new mysqli($dbhost, $dbuser, $dbpass, $db);

if ($conn->connect_error) {
    echo "connection was failed ";
} else {
    echo "connection made";
}
?>
   <?php

session_start();

$username  = "";
$email     = "";
$number    = "";
$password1 = "";
$password2 = "";
$errors    = array();

include 'conn.php';

if (isset($_POST['submit'])) {
    $username  = mysqli_real_escape_string($conn, $_POST['username']);
    $email     = mysqli_real_escape_string($conn, $_POST['email']);
    $password1 = mysqli_real_escape_string($conn, $_POST['pass1']);
    $password2 = mysqli_real_escape_string($conn, $_POST['pass2']);
    $number    = mysqli_real_escape_string($conn, $_POST['mobile']);

    if ($password1 != $password2) {
        array_push($errors, "The two passwords do not match");
    }

    $user_check_query = "SELECT * FROM Trainee WHERE name='$username' ";
    $result           = mysqli_query($conn, $user_check_query);

    $user = mysqli_fetch_assoc($result);

    if ($user) {
        if ($user['name'] === $username) {
            array_push($errors, "Username already exists");
        }
    }

    if (count($errors) == 0) {
        $password = md5($password1);

        $query = "INSERT INTO Trainee (email, name, password,number) 
               VALUES('$email','$username','$password','$number')";

        $result1 = mysqli_query($conn, $query);

        $_SESSION['username'] = $username;
        $_SESSION['success']  = "You are now logged in";
        //header('location: index.php');
    }
}
?>

0 个答案:

没有答案