为什么sql无法执行插入查询?

时间:2016-12-08 09:04:14

标签: php html mysql

我正在尝试执行以下代码,但我一直收到以下错误:

  

“无法执行插入查询。”

似乎插入Employees的工作无效。

我不确定缺少什么。

以下是我的代码:

if(isset($_POST['submit'])) {
    $fname = $_POST['fname'];
    $minitial = $_POST['minitial'];
    $lname = $_POST['lname'];
    $gender = $_POST['gender'];
    $phone = $_POST['phone'];
    $dob = $_POST['dob'];
    $ssn = $_POST['ssn'];
    $address = $_POST['address'];
    $city = $_POST['city'];
    $state = $_POST['state'];
    $zip = $_POST['zip'];
    $email = $_POST['email'];
    $username = $_POST['username'];
    $password = $_POST['password'];

    if($fname == "" || $minitial == "" || $lname == "" || $gender == "" || 
            $phone == "" || $dob == "" || $ssn == "" || $address == "" || 
            $city == "" || $state == "" || $zip == "" || $email == "" || 
            $username == "" || $password == "") {
        echo "All fields should be filled. Either one or many fields are empty.";
        echo "<br/>";
        echo "<a href='register.php'>Go back</a>";} 
    else {
        mysqli_query($mysqli, "INSERT INTO 'Employees'('fname', 'minitial', 'lname', 'gender', 'phone', 'dob', 'ssn', 'address', 'city', 'state', 'zip', 'email', 'username', 'password') VALUES('$fname', '$minitial', '$lname', '$gender', '$phone', '$dob', '$ssn', '$address', '$city', '$state', '$zip', '$email', '$username', md5('$password'))")
        or die("Could not execute the insert query.");

            echo "Registration successfully";
            echo "<br/>";
            echo "<a href='login.php'>Login</a>";
        }

3 个答案:

答案 0 :(得分:3)

这是因为您正在引用如下所示的表名。不要单引号列名称,否则它被视为字符串文字而不是实际的表/列名称

createAuthorizationHeader(headers: Headers) {
    // this does add the header in time
    localStorage.setItem('api_token', 'some token');
    headers.append('Authorization', 'Bearer ' + localStorage.getItem('api_token'));

    // this does not add the header in time
    let api_token = await this.storage.get('api_token');
    headers.append('Authorization', 'Bearer ' + api_token);
}

你实际上想要像

那样逃避它
INSERT INTO 'Employees'

重新编写您的INSERT INTO `Employees` 声明

INSERT

答案 1 :(得分:0)

'替换为`或删除插入查询中的字段列表中的'

所以你的查询将是

mysqli_query($mysqli, "INSERT INTO `Employees`(`fname`, `minitial`, `lname`, `gender`, `phone`, `dob`, `ssn`, `address`, `city`, `state`, `zip`, `email`, `username`, `password`) VALUES('$fname', '$minitial', '$lname', '$gender', '$phone', '$dob', '$ssn', '$address', '$city', '$state', '$zip', '$email', '$username', md5('$password'))") 

答案 2 :(得分:0)

mysqli_error()无法解决您的问题,但它至少会告诉您它的位置。替换:

mysqli_query($mysqli, "INSERT INTO 'Employees'('fname', 'minitial', 'lname', 'gender', 'phone', 'dob', 'ssn', 'address', 'city', 'state', 'zip', 'email', 'username', 'password') VALUES('$fname', '$minitial', '$lname', '$gender', '$phone', '$dob', '$ssn', '$address', '$city', '$state', '$zip', '$email', '$username', md5('$password'))")
or die("Could not execute the insert query.");

with:

mysqli_query($mysqli, "INSERT INTO 'Employees'('fname', 'minitial', 'lname', 'gender', 'phone', 'dob', 'ssn', 'address', 'city', 'state', 'zip', 'email', 'username', 'password') VALUES('$fname', '$minitial', '$lname', '$gender', '$phone', '$dob', '$ssn', '$address', '$city', '$state', '$zip', '$email', '$username', md5('$password'))")
or die(mysqli_error($mysqli));