MySQLi更新与行情等

时间:2014-03-04 02:34:10

标签: php mysql mysqli sql-update

我正在尝试使用PHP更新Joomla模块,但参数字段中充满了引号和单引号。

这是原始的MySQL查询。我怎样才能在PHP中使用查询中的所有引号和单引号?

UPDATE `xxyyzz_modules` 
SET `params` = '{"moduleclass_sfx":"","loadJQuery":"1","tableType":"datatable","theme":"ui-lightness","source":"sql","title":"","className":"arttable_table","tablecode":"","sqlQuery":"SELECT * FROM `xxyyzz_dtregister_paymentdetailsuser` WHERE datetime between ''2014\\/02\\/16'' and ''2014\\/03\\/02''","csscode":"","connectionString":"","csvFile":"","csvDelimiter":",","convertLinks":"2","linkConversionPattern":"TITLE|URL","linksNofollow":"0","linksNewWindow":"0","showFirstLink":"0","searchText":"Search:","searchSize":"15","headerStyle":"","cellStyle":"","excel":"","chartType":"LineChart","chartLeftHeader":"","xAxis":"","yAxis":"","chartWidth":"1100","chartHeight":"300","hideTable":"1","module_tag":"div","bootstrap_size":"0","header_tag":"h3","header_class":"","style":"0"}' 
WHERE `id` =140;

2 个答案:

答案 0 :(得分:1)

您需要转义用于分隔PHP字符串的任何类型的引号。

$sql = 'UPDATE `xxyyzz_modules`
        SET `params` = \'{"moduleclass_sfx":"","loadJQuery":"1","tableType":"datatable","theme":"ui-lightness","source":"sql","title":"","className":"arttable_table","tablecode":"","sqlQuery":"SELECT * FROM `xxyyzz_dtregister_paymentdetailsuser` WHERE datetime between \'\'2014\\/02\\/16\'\' and \'\'2014\\/03\\/02\'\'","csscode":"","connectionString":"","csvFile":"","csvDelimiter":",","convertLinks":"2","linkConversionPattern":"TITLE|URL","linksNofollow":"0","linksNewWindow":"0","showFirstLink":"0","searchText":"Search:","searchSize":"15","headerStyle":"","cellStyle":"","excel":"","chartType":"LineChart","chartLeftHeader":"","xAxis":"","yAxis":"","chartWidth":"1100","chartHeight":"300","hideTable":"1","module_tag":"div","bootstrap_size":"0","header_tag":"h3","header_class":"","style":"0"}\'
        WHERE `id` =140;';

$sql = "UPDATE `xxyyzz_modules`
        SET `params` = '{\"moduleclass_sfx\":\"\",\"loadJQuery\":\"1\",\"tableType\":\"datatable\",\"theme\":\"ui-lightness\",\"source\":\"sql\",\"title\":\"\",\"className\":\"arttable_table\",\"tablecode\":\"\",\"sqlQuery\":\"SELECT * FROM `xxyyzz_dtregister_paymentdetailsuser` WHERE datetime between ''2014\\\\/02\\\\/16'' and ''2014\\\\/03\\\\/02''\",\"csscode\":\"\",\"connectionString\":\"\",\"csvFile\":\"\",\"csvDelimiter\":\",\",\"convertLinks\":\"2\",\"linkConversionPattern\":\"TITLE|URL\",\"linksNofollow\":\"0\",\"linksNewWindow\":\"0\",\"showFirstLink\":\"0\",\"searchText\":\"Search:\",\"searchSize\":\"15\",\"headerStyle\":\"\",\"cellStyle\":\"\",\"excel\":\"\",\"chartType\":\"LineChart\",\"chartLeftHeader\":\"\",\"xAxis\":\"\",\"yAxis\":\"\",\"chartWidth\":\"1100\",\"chartHeight\":\"300\",\"hideTable\":\"1\",\"module_tag\":\"div\",\"bootstrap_size\":\"0\",\"header_tag\":\"h3\",\"header_class\":\"\",\"style\":\"0\"}'
        WHERE `id` =140";

或者使用here-doc:

$sql = <<'EOS'
       UPDATE `xxyyzz_modules`
       SET `params` = '{"moduleclass_sfx":"","loadJQuery":"1","tableType":"datatable","theme":"ui-lightness","source":"sql","title":"","className":"arttable_table","tablecode":"","sqlQuery":"SELECT * FROM `xxyyzz_dtregister_paymentdetailsuser` WHERE datetime between ''2014\\/02\\/16'' and ''2014\\/03\\/02''","csscode":"","connectionString":"","csvFile":"","csvDelimiter":",","convertLinks":"2","linkConversionPattern":"TITLE|URL","linksNofollow":"0","linksNewWindow":"0","showFirstLink":"0","searchText":"Search:","searchSize":"15","headerStyle":"","cellStyle":"","excel":"","chartType":"LineChart","chartLeftHeader":"","xAxis":"","yAxis":"","chartWidth":"1100","chartHeight":"300","hideTable":"1","module_tag":"div","bootstrap_size":"0","header_tag":"h3","header_class":"","style":"0"}'
       WHERE `id` =140;
EOS;

答案 1 :(得分:0)

而不是手动转义字符串:

$json = '{"moduleclass_sfx":"","loadJQuery":"1","tableType":"datatable","theme":"ui-lightness","source":"sql","title":"","className":"arttable_table","tablecode":"","sqlQuery":"SELECT * FROM `xxyyzz_dtregister_paymentdetailsuser` WHERE datetime between ''2014\\/02\\/16'' and ''2014\\/03\\/02''","csscode":"","connectionString":"","csvFile":"","csvDelimiter":",","convertLinks":"2","linkConversionPattern":"TITLE|URL","linksNofollow":"0","linksNewWindow":"0","showFirstLink":"0","searchText":"Search:","searchSize":"15","headerStyle":"","cellStyle":"","excel":"","chartType":"LineChart","chartLeftHeader":"","xAxis":"","yAxis":"","chartWidth":"1100","chartHeight":"300","hideTable":"1","module_tag":"div","bootstrap_size":"0","header_tag":"h3","header_class":"","style":"0"}' 

$sql = "UPDATE `xxyyzz_modules` 
SET `params` = '" . mysqli_real_escape_string($json) . "'
WHERE `id` =140";