1

我有一个奇怪的问题,我正在通过 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')

它向我抛出了这个错误: Invalid query: Column count doesn't match value count at row 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')
4

2 回答 2

3

如果你 print $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未发送到 Web 浏览器的内容。mysql_real_escape_string对发送的每个单独的值使用数据库驱动程序的转义方法 ( )。

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

于 2010-10-27T18:16:11.140 回答
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());
于 2017-06-21T03:50:39.900 回答