查询无效:列计数与第1行的值计数不匹配

时间:2010-10-27 17:36:27

标签: php mysql insert-into

我有一个奇怪的问题,我正在通过PHP发送SQL查询:

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')

2 个答案:

答案 0 :(得分: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;

请勿对未发送到网络浏览器的内容使用htmlentities。对发送的每个值使用数据库驱动程序的转义方法(mysql_real_escape_string)。

编辑:更好的是,使用预先准备好的语句和数据绑定MySQLiPDO,这会在绑定数据时自动转义数据。

答案 1 :(得分: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());
相关问题