2012-07-16 3 views
1

PHP를 사용하고 MySQL 데이터베이스에 항목을 추가하려고합니다. 나는 다음과 같은 코드를 사용하는 경우PHP가 SQL 항목을 추가하지 않습니다

그것은 작동 :

mysql_query("INSERT INTO `intranet`.`product_form` (`id`, `ProductName`, `ProductInitiatedBy`) VALUES (NULL, 'item1', 'item2')") or die("Could not perform select query - " . mysql_error());; 

을하지만, 나는 다음과 같은 코드를 사용하는 경우가 작동하지 않습니다

$product_name=("tom"); 
mysql_query("INSERT INTO `intranet`.`product_form` (`id`, `ProductName`, `ProductInitiatedBy`) VALUES (NULL, $product_name, 'item2')") or die("Could not perform select query - " . mysql_error());; 

을 나는라는 오류가 발생합니다 :

선택 쿼리를 수행 할 수 없습니다. 의 'ProductName1234'열을 알 수 없습니다.

ProductName1234$product_name의 데이터이며 추가하려고하는 데이터 여야하며 열이 없어야합니다.

+0

먼저, 코드를 포맷! –

+0

그냥 .. 내가 [코드]를 입력하고 내가 잘못 했어 .. 모든 지금 고정 발견. – Aaron

답변

3

변경 :

mysql_query("INSERT INTO `intranet`.`product_form` (`id`, `ProductName`, `ProductInitiatedBy`) VALUES (NULL, $product_name, 'item2')") or die("Could not perform select query - " . mysql_error());; 

에 :

당신은 ' $product_name에 추가 할 필요가
mysql_query("INSERT INTO `intranet`.`product_form` (`id`, `ProductName`, `ProductInitiatedBy`) VALUES (NULL, '$product_name', 'item2')") or die("Could not perform select query - " . mysql_error()); 

: 모든

VALUES (NULL, '$product_name', 'item2') 
      ^   ^
+0

나는 그것을 시도하고 그것은 작동하지 않았다. – Aaron

+0

@Aaron, 틀림없이? –

+0

이전에 그렇게 해 봤지만 잘 작동하는 것 같습니다. 미안합니다. – Aaron

4

그런 문자열을 삽입 할 때 따옴표로 묶어야합니다. 그렇지 않으면 MySQL은 데이터를 삽입하기 위해 어딘가에서 열을 지정하려고한다고 생각합니다.

mysql_query("INSERT INTO intranet.product_form (id, ProductName, ProductInitiatedBy) VALUES (NULL, \"$product_name\", 'item2')"); 
+0

감사합니다 힙합 .. 그랬어. – Aaron

관련 문제