如何在PHP中插入时跳过一些表列?

时间:2017-01-31 14:04:13

标签: php database mysqli phpmyadmin insert-into

我有2种用户类型的注册表格,分别是Surveyee和Client。他们在拥有相同的领域时有相似之处,但是Surveyee有更多。因此,我希望我的surveyee插入代码避免使用一列,因为它只适用于客户端usertype。

<?php
if(isset($_POST['submit'])){

   include("connection.php");
    $username = $_POST['username'];
    $fullname = $_POST['fullname'];
    $email = $_POST['email'];
    $age = $_POST['age'];
    $password = $_POST['password'];
    $confirmpassword = $_POST['confirmpassword'];
    $gender = $_POST['gender'];
    $occupation = $_POST['occupation'];

    $user = mysqli_query($con, "SELECT username from user WHERE username = '".$username."'");
    $count = mysqli_num_rows($user);

    if($password != $confirmpassword){
        echo "Password does not match!";
    }
    else{
        if($count != 0){
            echo "Username already exists!";
        }

        else{

            $insert = mysqli_query($con, "INSERT INTO `user` (`username`,`fullname`,`email`,`companyname`,`age`,`password`,`gender`,`occupation`,`usertype`) VALUES (`$username`,`$fullname`,`$email`,``,`$age`,`$password`,$gender`,`$occupation`,`Surveyee`)");
            if(!$insert){
                echo mysqli_errno();
            }
            else{
                echo "Records have been saved!";
            }
        }
    }
}
?>

这是我的桌面设计 enter image description here

我试图避免列公司名称,但我收到了错误。警告:mysqli_errno()只需要1个参数,第29行给出0,即if(!$ insert){                 echo mysqli_errno();             部分

 $insert = mysqli_query($con, "INSERT INTO `user` (`username`,`fullname`,`email`,`companyname`,`age`,`password`,`gender`,`occupation`,`usertype`) VALUES (`$username`,`$fullname`,`$email`,``,`$age`,`$password`,$gender`,`$occupation`,`Surveyee`)");
        if(!$insert){

1 个答案:

答案 0 :(得分:1)

删除 Apostrophes (&#39;)并使用反引号(`)括起列和表名。

$insert = mysqli_query($con, "INSERT INTO `user` (`username`,`fullname`,`email`,`companyname`,`age`,`password`,`gender`,`occupation`,`usertype`) VALUES ('$username','$fullname',$email','',$age','$password','$gender','$occupation','Surveyee')");

根据您给出的查询,

INSERT INTO (Column_Name, Column_Name) Table_Name VALUES ('','');语法也是错误的。

将其更改为,

INSERT INTO `Table_Name` (`Column_Name`, `Column_Name`) VALUES ('','');