2010-10-27 111 views
1

我有一個奇怪的問題,我通過PHP發送SQL查詢:查詢無效:列數並不在行匹配值計數1

INSERT INTO `lib_plex` (`id`, `active`, `lastUpdated`, `entry_date`, `entry_ip`, `address`, `city`, `state_iso`, `zip_code`, `plex_type`, `price`, `has_garage`, `has_indoor_parking`, `has_outdoor_parking`, `has_pool`, `has_fireplace`, `average_nb_room`, `construction_year`, `building_material`) 

VALUES ('','1','2010-10-27 13:22:59','2010-10-27 13:22:59','2130706433','COMMERCE ST.','85825','OK','73521','commercial','595000','0','0','0','0','0','11','','Aluminum Siding') 

它拋出我這個錯誤: 無效查詢:列計數與第1行的值計數不匹配。 雖然,當我粘貼並運行相同的確切查詢在PhpMyAdmin,它完美的作品,所以它讓我很困惑...

我統計的列數和值的數量,他們匹配(19 )。我試圖刪除'id'字段,因爲它是自動遞增的,但它沒有改變任何東西。我究竟做錯了什麼?爲什麼它在PhpMyAdmin中工作?

感謝您的幫助!

編輯:

這裏的PHP代碼:

$values = array('', 1, $lastUpdated, $entry_date, $entry_ip, $streetName, $cityId, $listing['stateorprovince'], $listing['postalcode'], $listing['type'], $listing['listprice'], $has_garage, $has_indoor_parking, $has_outdoor_parking, $has_pool, $has_fireplace, $average_nb_room, $listing['yearbuilt'], $listing['exteriortype']); 

$q = "INSERT INTO `lib_plex` (`id`, `active`, `lastUpdated`, `entry_date`, `entry_ip`, `address`, `city`, `state_iso`, `zip_code`, `plex_type`, `price`, `has_garage`, `has_indoor_parking`, `has_outdoor_parking`, `has_pool`, `has_fireplace`, `average_nb_room`, `construction_year`, `building_material`) 
VALUES ('".htmlentities(implode("','",$values),ENT_QUOTES)."')"; 

$this->execMysqlQuery($q); 

和被調用的方法:

private function execMysqlQuery($q, $returnResults = false, $returnInsertId = false){ 
$c = mysql_connect(DB_SERVER,DB_LOGIN,DB_PASSWORD); 
mysql_select_db(DB_NAME, $c); 

$result = mysql_query($q); 
if (!$result) { 
    die('Invalid query: ' . mysql_error(). "<br/>=>".$q); 
} 

if ($returnInsertId) 
    return mysql_insert_id(); 

mysql_close($c); 

if ($returnResults) 
    return $result; 

return true; 
} 

和錯誤:

Invalid query: Column count doesn't match value count at row 1 
=>INSERT INTO `lib_plex` (`id`, `active`, `lastUpdated`, `entry_date`, `entry_ip`, `address`, `city`, `state_iso`, `zip_code`, `plex_type`, `price`, `has_garage`, `has_indoor_parking`, `has_outdoor_parking`, `has_pool`, `has_fireplace`, `average_nb_room`, `construction_year`, `building_material`) VALUES ('','1','2010-10-27 13:47:35','2010-10-27 13:47:35','2130706433','COMMERCE ST.','85825','OK','73521','commercial','595000','0','0','0','0','0','11','','Aluminum Siding') 
+0

你能告訴我們這個查詢被分配的PHP代碼嗎? – Fosco 2010-10-27 17:39:01

+1

你確定你已經在代碼中選擇了正確的數據庫嗎? – 2010-10-27 17:40:06

+0

感謝您的問題,我在上次編輯中包含代碼 – 2010-10-27 17:51:51

回答

3

如果您打印$q,我敢打賭它看起來像這樣:

INSERT INTO `lib_plex` (`id`, `active`, `lastUpdated`, `entry_date`, `entry_ip`, `address`, `city`, `state_iso`, `zip_code`, `plex_type`, `price`, `has_garage`, `has_indoor_parking`, `has_outdoor_parking`, `has_pool`, `has_fireplace`, `average_nb_room`, `construction_year`, `building_material`) 
VALUES ('&#39;,&#39;1&#39;,&#39;2010-10-27 13:22:59&#39;,&#39;2010-10-27 13:22:59&#39;,&#39;2130706433&#39;,&#39;COMMERCE ST.&#39;,&#39;85825&#39;,&#39;OK&#39;,&#39;73521&#39;,&#39;commercial&#39;,&#39;595000&#39;,&#39;0&#39;,&#39;0&#39;,&#39;0&#39;,&#39;0&#39;,&#39;0&#39;,&#39;11&#39;,&#39;&#39;,&#39;Aluminum Siding'); 

(我沒有PHP工作;這是一個猜測)

換句話說,htmlentities正在把你的報價變成HTML實體。具體而言,請將'轉換爲&#39;

請勿在沒有發送到Web瀏覽器的情況下使用htmlentities。對每個單獨的值用你的數據庫驅動程序的方法逃避(mysql_real_escape_string)在被送到

編輯:更重要的是,使用預準備語句和數據MySQLiPDO結合,它會自動逃跑的數據,你將它綁定。

+0

哦,天啊,謝謝你讓我覺得自己像個白癡:]就是這樣! – 2010-10-27 18:27:51

+0

+1不錯的一個。 - – webbiedave 2010-10-27 18:32:52

+0

我會使用MySQLi或PDO,但我正在爲客戶端工作,而且我不確定是否允許在他們的服務器上使用PDO或MySQLi。我被迫在自定義php框架(不是我的)中工作,該框架使用自己的查詢方法,這通常足夠有效。只是在這種情況下,我必須插入大量條目,並且這些方法在大量使用時似乎會導致內存泄漏......所以我必須再次使用基本的mysql ,非常感謝! – 2010-10-27 18:36:00

0
if ($insert) { 
    $query = "INSERT INTO employee VALUES ($empno,'$lname','$fname','$init','$gender','$bdate','$dept','$position',$pay,$dayswork,$otrate,$othrs,$allow,$advances,$insurance,'')"; 
    $msg = "New record saved!"; 
    } 
    else { 
    $query = "UPDATE employee SET empno=$empno,lname='$lname',fname='$fname',init= '$init',gender='$gender',bdate='$bdate',dept='$dept',position='$position',pay=$pay,dayswork=$dayswork,otrate=$otrate,othrs=$othrs,allow=$allow,advances=$advances,insurance=$insurance WHERE empno = $empno"; 
    $msg = "Record updated!"; 
    } 
    include 'include/dbconnection.php'; 
    $result=mysql_query ($query,$link) or die ("invalid query".mysql_error()); 
+1

嗨,歡迎來到堆棧溢出。請參閱[如何回答](https://stackoverflow.com/help/how-to-answer) – Maher 2017-06-21 03:59:18

相關問題