如何将数据插入数据库

时间:2019-01-25 09:37:11

标签: php

我已经创建了与数据库的连接,并且INSERT INTO我的表无法正常工作,INSERT INTO我的表VALUES没有任何参数,或者它必须具有

if(isset($_POST["submit"])){
    include_once "emenikeconnect.php";

    $first = mysqli_real_escape_string($dbconnection, $_POST["first"]);
    $last = mysqli_real_escape_string($dbconnection, $_POST["last"]);
    $email = mysqli_real_escape_string($dbconnection, $_POST["email"]);
    $pass = mysqli_real_escape_string($dbconnection, $_POST["password"]);

    // Error handlers
    // Check for empty field

    if(empty($first) ||empty($last) ||empty($email) ||empty($pass)){
        die("Please this field is required");
         exit();
    }else{
        // Check if input character are valid
        if(!preg_match("/^[a-zA-Z]*$/",$first)|| !preg_match("/^[a-zA-Z]*$/",$last)){
            die("Firstname or Lastname have bad text");
            exit();
        }else{
            // Check if the email address is valid
            if(filter_var($email, FILTER_VALIDATE_EMAIL)){
                die("This email addres is invalid");
                exit();
            }else{
                // Hashing password
               $password = hash($pass);

                 // Insert data into database
               $querydata = "INSER INTO UserTable(ID, First-name, Last-name, Email, Password) VALUES ()";
               mysqli_query($dbconnection,$querydata);
               die("Register succesfullu");

            }

        }
    }
}

1 个答案:

答案 0 :(得分:2)

此行存在语法错误:

$querydata = "INSER INTO UserTable(ID, First-name, Last-name, Email, Password) VALUES ()";

您写的是INSER而不是INSERT。另外,您在VALUES子句中应具有实际值,例如VALUES(1, 'John', 'Smith', 'john.smith@example.com', 'secret')

另一方面,宁愿使用ORM库,该库为您提供所有基础知识。