2010-10-27 41 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

相关问题