数据未插入表,数据库连接没有错误

时间:2019-07-18 04:17:10

标签: php mysql connection

我对PHP非常陌生,一个学生刚刚创建了我的第一个登录系统。数据库连接成功,但数据未插入表中

我已经回显了它从注册表单获取值的变量。

<?php
$conn = "mysqli_connect('localhost','root','','user')";
if ($conn) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    $conf_password = $_POST['conf_password'];

    if ($username == '' || $password == '' || $conf_password == '') {
        echo "Incomplete credentials ";
        exit;
    } else {
        if ($password == $conf_password) {
            $query = "INSERT INTO table1(Sno,Username,Password)
              VALUES(NUll,'$username','$password')";

            $exec_query = "mysqli_query($conn,$query)";
            if ($exec_query) {
                echo "User Inserted";
                echo $username;
                echo $password;
                echo $conf_password;
            } else {
                echo "Error Occurred";
            }
        } else {
            echo "Please confirm Password";
        }
    }
}

"mysqli_close($conn)";
?>`

期望-数据已插入表1

2 个答案:

答案 0 :(得分:1)

mysqli_query是一个函数 删除引号写为 mysqli_query($conn,$query)mysqli_close($conn)

相同

答案 1 :(得分:0)

你可以试试吗?

<?php
    $conn = mysqli_connect('localhost','root','','user');
    if($conn)
    {
        $username=$_POST['username'];
        $password=$_POST['password'];
        $conf_password=$_POST['conf_password'];

        if ($username==''||$password==''||$conf_password=='')
        {
            echo "Incomplete credentials ";
            exit;
        }
        else
        {
            if ($password == $conf_password)
            {
                $query= "INSERT INTO table1(Sno,Username,Password) 
                VALUES(NULL,'$username','$password')";

                $exec_query= mysqli_query($conn,$query);
                if ($exec_query)
                {
                    echo "User Inserted";
                    echo $username;
                    echo $password;
                    echo $conf_password;
                }
                else
                { 
                    echo "Error Occurred";
                }
            }
            else
            {
                echo "Please confirm Password";
            }
        }
    }

    mysqli_close($conn);   
?>

Expecting - Data inserted into table1