dongyuzhu2244 2014-12-02 19:27
浏览 12

PHP MYSQL列数不匹配[关闭]

Trying to insert POST data as new record in mysql database. Table has 5 columns:

ContractorID, firstName, lastName, email, password

$insert = "INSERT INTO CONTRACTOR (firstName, lastName, email, password)
VALUES ($firstName, $lastName, $email, $password)";

if (mysqli_query($conn, $insert)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $insert . "<br>" . mysqli_error($conn);
}

ContractorID is an auto increment primary key and so I thought I could leave it off the insert. However, even when I change to:

$insert = "INSERT INTO CONTRACTOR (ContractorID, firstName, lastName, email, password)
VALUES ('DEFAULT', '$firstName', '$lastName', '$email', '$password')";

I get the same error:

Error: INSERT INTO CONTRACTOR (firstName, lastName, email, password) VALUES ('w4rwsfsdf', 'wqerwerw', 'rweqrqwer' 'qwerqwer') Column count doesn't match value count at row 1

EDIT: It was a missing comma causing the issue, I copied the code down wrong here and the error message correctly. Couldn't copy and paste linux to windows.

  • 写回答

1条回答 默认 最新

  • dongwei7245 2014-12-02 19:31
    关注

    As per your originally posted question before you added the missing comma and not marking it as an "edit".

    You have a comma missing in your VALUES

    VALUES ('DEFAULT' '$firstName'
                     ^ right there
    

    change it to:

    VALUES ('DEFAULT', '$firstName'
    

    Edit:

    I suggest you delete/drop your present table if you can, and run the following:

    $sql = "CREATE TABLE IF NOT EXISTS CONTRACTOR
    (
      `ContractorID` int(10) NOT NULL AUTO_INCREMENT,
      `firstName` varchar(55) DEFAULT NULL,
      `lastName` varchar(55) DEFAULT NULL,
      `email` varchar(255) DEFAULT NULL,
      `password` varchar(255) DEFAULT NULL,
       PRIMARY KEY (`ContractorID`)
    
                ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
    
    INSERT INTO CONTRACTOR (firstName, lastName, email, password)
    VALUES ('John', 'Doe', 'email@example.com', 'test_password')";
    
    if (mysqli_query($conn, $sql)) {
        echo "New table created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }
    

    Then try the query again.

    $insert = "INSERT INTO CONTRACTOR (firstName, lastName, email, password)
    VALUES ('Bob', 'Smith', 'email_2@example.com', 'test_password_2')";
    
    if (mysqli_query($conn, $insert)) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $insert . "<br>" . mysqli_error($conn);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)