>  기사  >  백엔드 개발  >  MySQL 쿼리에서 \"행 1의 열 개수 불일치\" 오류가 발생하는 이유는 무엇입니까?

MySQL 쿼리에서 \"행 1의 열 개수 불일치\" 오류가 발생하는 이유는 무엇입니까?

Patricia Arquette
Patricia Arquette원래의
2024-10-27 10:20:31168검색

Why am I getting the

PHP, MySQL 오류: 행 1의 열 개수 불일치

이 오류는 "열 개수가 행 1의 값 개수와 일치하지 않습니다. "는 데이터를 삽입하려는 열 수와 제공되는 실제 값 수 사이의 불일치를 나타냅니다.

제공된 코드 스니펫에서 다음과 같은 테이블에 데이터를 삽입하려고 합니다. 8개 값($name, $description, $shortDescription, $ingredients, $method, $username, $length, $dateAdded)을 사용하는 9개 열(id, Name, Description, shortDescription, Ingredients, Method, Length, dateAdded, Username).

오류 원인:

INSERT 쿼리에 정의된 열 개수와 전달되는 값 개수가 일치하지 않아 오류가 발생합니다. . 이 경우 쿼리에서는 9개의 값이 필요하지만 "이미지" 열을 제외하고 8개만 제공됩니다.

해결책:

오류를 해결하려면 다음을 확인하세요. 값의 수가 INSERT 쿼리의 열 수와 일치하는지 확인합니다. "이미지" 열에 누락된 값을 추가하거나 삽입되는 열 목록에서 이를 제거할 수 있습니다.

코드 수정:

옵션 1: 누락된 값 추가

<code class="php">$query = sprintf("INSERT INTO dbname (id, Name, Description, shortDescription, Ingredients, Method, Image, Length, dateAdded, Username) VALUES ('', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
    mysql_real_escape_string($name),
    mysql_real_escape_string($description),
    mysql_real_escape_string($shortDescription),
    mysql_real_escape_string($ingredients),
    mysql_real_escape_string($method),
    '', // Placeholder for empty 'image' value
    mysql_real_escape_string($length),
    mysql_real_escape_string($dateAdded),
    mysql_real_escape_string($username));</code>

옵션 2: "이미지" 열 제거

<code class="php">$query = sprintf("INSERT INTO dbname (id, Name, Description, shortDescription, Ingredients, Method, Length, dateAdded, Username) VALUES ('', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
    mysql_real_escape_string($name),
    mysql_real_escape_string($description),
    mysql_real_escape_string($shortDescription),
    mysql_real_escape_string($ingredients),
    mysql_real_escape_string($method),
    mysql_real_escape_string($length),
    mysql_real_escape_string($dateAdded),
    mysql_real_escape_string($username));</code>

위 내용은 MySQL 쿼리에서 \"행 1의 열 개수 불일치\" 오류가 발생하는 이유는 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.