Weird error in php mysql query says syntax error while query works fine in phpmyadmin sql

时间:2016-04-15 11:14:22

标签: php mysql sql mysqli phpmyadmin

I have encountered an weird error in php today spend lots of hours but didn't got the reasons.

Here is the code

$query = "INSERT INTO `mobile_spacification` (`id`,`product_name`,`gsmarenaurl`, `network_technology`, `ntwrk_2gband`, `ntwrk_3gband`, `ntwrk_4gband`, `ntwrk_speed`, `ntwrk_gprs`, `ntwrk_edge`, `launch_anu`, `launch_status`, `body_diman`, `body_weight`, `body_sim`, `display_type`, `display_size`, `display_resolution`, `display_multitouch`, `display_protection`, `plateform_os`, `plateform_chipset`, `plateform_cpu`, `plateform_gpu`, `memory_crdslot`, `memory-internal`, `camera_primary`, `camera_feature`, `camera_video`, `cemara_secondary`, `sound_alert_type`, `sound_loudspeaker`, `audio_jack`, `comms_wlan`, `comms_bluetooth`, `comms_gps`, `comms_radio`, `comms_usb`, `feature_sensor`, `feature_messaing`, `feature_browser`, `featuer_java`, `feature_misc`, `battery_type`, `battery_stndby`, `battery_talktime`, `misc_color`)
                    VALUES 
             (NULL,'$name','$site','".$table_field['Technology']."','".$table_field['2G bands']."','".$table_field['3G bands']."','".$table_field['4G bands']."','".$table_field['Speed']."','".$table_field['GPRS']."','".$table_field['EDGE']."','".$table_field['Announced']."','".$table_field['Status']."','".$table_field['Dimensions']."','".$table_field['Weight']."','".$table_field['SIM']."','".$table_field['Type']."','".$table_field['Size']."','".$table_field['Resolution']."','".$table_field['Multitouch']."','".$table_field['Protection']."','".$table_field['OS']."','".$table_field['Chipset']."','".$table_field['CPU']."','".$table_field['GPU']."','".$table_field['Card slot']."','".$table_field['Internal']."',
             '".$table_field['Primary']."','".$table_field['Features']."','".$table_field['Video']."','".$table_field['Secondary']."','".$table_field['Alert types']."','".$table_field['Loudspeaker']."','".$table_field['3.5mm jack']."','".$table_field['WLAN']."','".$table_field['Bluetooth']."','".$table_field['GPS']."','".$table_field['Radio']."','".$table_field['USB']."','".$table_field['Sensors']."','".$table_field['Messaging']."','".$table_field['Browser']."','".$table_field['Java']."','".$table_field['Other Features']."','".$table_field['battery_type']."','".$table_field['Stand-by']."','".$table_field['Talk time']."','".$table_field['Colors']."');";


echo $query =  preg_replace( "/\r|\n/", "",htmlentities($query, ENT_QUOTES));               
$query_run = mysqli_query($conn,$query) or die("<br><br>".mysqli_error($conn));

It returns the following error

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''Samsung Galaxy J3 (2016)','http://www.gsmarena.com/samsung_galax' at line 1

It's okay we got that kind of error when there is a syntax error so I did what I always do try echoing the query and copy pasting it to phpmyadmin sql

The echo says

INSERT
INTO
  `mobile_spacification`(
    `id`,
    `product_name`,
    `gsmarenaurl`,
    `network_technology`,
    `ntwrk_2gband`,
    `ntwrk_3gband`,
    `ntwrk_4gband`,
    `ntwrk_speed`,
    `ntwrk_gprs`,
    `ntwrk_edge`,
    `launch_anu`,
    `launch_status`,
    `body_diman`,
    `body_weight`,
    `body_sim`,
    `display_type`,
    `display_size`,
    `display_resolution`,
    `display_multitouch`,
    `display_protection`,
    `plateform_os`,
    `plateform_chipset`,
    `plateform_cpu`,
    `plateform_gpu`,
    `memory_crdslot`,
    `memory-internal`,
    `camera_primary`,
    `camera_feature`,
    `camera_video`,
    `cemara_secondary`,
    `sound_alert_type`,
    `sound_loudspeaker`,
    `audio_jack`,
    `comms_wlan`,
    `comms_bluetooth`,
    `comms_gps`,
    `comms_radio`,
    `comms_usb`,
    `feature_sensor`,
    `feature_messaing`,
    `feature_browser`,
    `featuer_java`,
    `feature_misc`,
    `battery_type`,
    `battery_stndby`,
    `battery_talktime`,
    `misc_color`
  )
VALUES(
  NULL,
  'Samsung Galaxy J3 (2016)',
  'http://www.gsmarena.com/samsung_galaxy_j3_(2016)-7760.php',
  'GSM / HSPA / LTE',
  'GSM 850 / 900 / 1800 / 1900 - SIM 1 & SIM 2 (dual-SIM model only)',
  'HSDPA 850 / 900 / 1900 / 2100 - J320F J3109',
  'LTE band 1(2100) 3(1800) 5(850) 7(2600) 8(900) 20(800) - J320F',
  'HSPA 42.2/5.76 Mbps LTE Cat4 150/50 Mbps',
  'Yes',
  'Yes',
  '2015 November',
  'Available. Released 2016 January',
  '142.3 x 71 x 7.9 mm (5.60 x 2.80 x 0.31 in)',
  '138 g (4.87 oz)',
  'Single SIM (Micro-SIM) or Dual SIM (Micro-SIM dual stand-by)',
  'Super AMOLED capacitive touchscreen 16M colors',
  '5.0 inches (~68.2% screen-to-body ratio)',
  '720 x 1280 pixels (~294 ppi pixel density)',
  'Yes',
  '',
  'Android OS v5.1.1 (Lollipop)',
  'Spreadtrum SC8830',
  'Quad-core 1.2 GHz Cortex-A7',
  'Mali-400',
  'microSD up to 128 GB (dedicated slot)',
  '8/16 GB 1.5 GB RAM',
  '8 MP f/2.2 autofocus LED flash - J3109 J320F5 MP autofocus LED flash - J320P',
  'Sensors',
  '1080p@30fps (J3109) 720p@30fps (J320P J320F)',
  '5 MP f/2.2 720p@30fps - J3109 J320F2 MP - J320P',
  'Vibration; MP3 WAV ringtones',
  '',
  '',
  'Wi-Fi 802.11 b/g/n Wi-Fi Direct hotspot',
  'v4.1 A2DP',
  'Yes with A-GPS GLONASS/ BDS (region dependent)',
  'FM radio RDS recording',
  'microUSB v2.0 USB On-The-Go',
  'Accelerometer proximity',
  'SMS(threaded view) MMS Email Push Mail IM',
  'HTML5',
  'No',
  '- Active noise cancellation with dedicated mic - MP4/H.264 player - MP3/WAV/eAAC+/Flac player - Photo/video editor - Document viewer',
  'Removable Li-Ion 2600 mAh battery',
  'Up to 349 h',
  'Up to 22 h',
  'White Black Gold'
);

when running this query in phpmyadmin I got no error it works fine but when running through php it returns error I am so confused....

1 个答案:

答案 0 :(得分:0)

您不能在整个查询字符串上使用htmlentities( ..., ENT_QUOTES )

您看到了正确的查询,因为您的回显是由浏览器解析的,但如果您查看HTML源代码,您会看到如下内容:

(...) VALUES (NULL,&#039;Something&#039; (...)

删除ENT_QUOTES

实际上,更好的做法是处理每个字段,而不是整个查询。最佳做法是使用准备好的陈述。