PHP如何添加引号?

时间:2014-07-17 08:21:18

标签: php mysql

$sqlL = '';
$Allel = '';
foreach($arr as $el){
    $AllUrl .= ",'".addslashes($el)."'";
    $sqlL .= "INSERT INTO Table (ID, Seet, Column) VALUES ('4', '$Seet', '$el');";
}

        $AllUrl = substr($Allel, 1); //delete first comma
        $sqlL .= "DELETE FROM Table WHERE ID= '4' AND URL NOT IN($Allel);";

结果我们得到下一个$sqlL

DELETE FROM Table WHERE ID= '4' AND URL NOT IN(house, test, test test);

但是不对,这不会奏效。

请告诉我如何在$Allel中添加引号?

P.S。:我想得到下一个查询:

DELETE FROM Table WHERE ID= '4' AND URL NOT IN('house', 'test', 'test test');

8 个答案:

答案 0 :(得分:1)

试试这个:

$sqlL = '';

foreach($arr as $el){
    $sqlL .= "INSERT INTO Table (ID, Seet, Column) VALUES ('4', '$Seet', '$el');";
}

$AllUrl = "'".implode("','",$arr)."'";
$sqlL .= "DELETE FROM Table WHERE ID= '4' AND URL NOT IN($AllUrl);";

答案 1 :(得分:1)

这只有一个答案...使用预处理语句,不使用字符串函数和“手工”引号和东西构建自己的查询。这是有风险的,与清洁程序相反。

反正:

如果$Allel包含纯值,请使用

$Allel = array_map(function($v) { return "'" . $v . "'"; }, $Allel);
$Allel = implode(', ', $Allel);

第一行将'包围值,第二行将使用,连接。

答案 2 :(得分:1)

我认为他想要一些不同的东西,你能尝试一下吗?

implode("', '", $array);

和sql

$sqlL .= "DELETE FROM Table WHERE ID= '4' AND URL NOT IN('".$Allel."');";

答案 3 :(得分:1)

虽然使用数组和内爆可能是最好的解决方案,但代码的问题是你用引号设置一个变量,然后在SQL中添加一个不同的变量: -

$sqlL = '';
foreach($arr as $el)
{
    $AllUrl .= ",'".addslashes($el)."'";
    $sqlL .= "INSERT INTO Table (ID, Seet, Column) VALUES ('4', '$Seet', '$el');";
}

$AllUrl = substr($AllUrl, 1); //delete first comma
$sqlL .= "DELETE FROM Table WHERE ID= '4' AND URL NOT IN($AllUrl);";

要使用implode,将项添加到数组(忽略逗号),然后在DELETE语句的最终赋值中使用分隔逗号将该数组内爆: -

$sqlL = '';
foreach($arr as $el)
{
    $AllUrl[] = "'".addslashes($el)."'";
    $sqlL .= "INSERT INTO Table (ID, Seet, Column) VALUES ('4', '$Seet', '$el');";
}

$sqlL .= "DELETE FROM Table WHERE ID= '4' AND URL NOT IN(".implode(',', $AllUrl).");";

您应该使用mysql_real_escape_string或等效字符串转义字符串。不确定您正在使用哪些数据库驱动程序,所以尽管不推荐使用mysql_ *驱动程序,但它们可以作为示例: -

$sqlL = '';
foreach($arr as $el)
{
    $AllUrl[] = "'".mysql_real_escape_string($el)."'";
    $sqlL .= "INSERT INTO Table (ID, Seet, Column) VALUES ('4', '$Seet', '$el');";
}

$sqlL .= "DELETE FROM Table WHERE ID= '4' AND URL NOT IN(".implode(',', $AllUrl).");";

答案 4 :(得分:0)

您可以使用PHP implode()函数

<?php
implode(', ', $array);

PHP Documentation

答案 5 :(得分:0)

您可以使用内爆功能,

$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);

参见下面的示例

$array = array("1","2","3");
echo "Array is: ".implode(",",$array );

Array is: 1,2,3

答案 6 :(得分:0)

试试这个

$sqlL = '';
$Allel = '';

$arr_Allel = array();
foreach($arr as $el){
    $arr_Allel[] = ",'".addslashes($el)."'";
    $sqlL .= "INSERT INTO Table (ID, Seet, Column) VALUES ('4', '$Seet', '$el');";
}

$Allel = implode(",", $arr_Allel);

$sqlL .= "DELETE FROM Table WHERE ID= '4' AND URL NOT IN($Allel);";

答案 7 :(得分:0)

试试这个:

<?php 

foreach($arr as $el){
    $AllUrl .= "'".addslashes($el)."' "; // Add items between spaces
}

$AllUrl_ = explode(" ",$AllUrl);
$AllUrlFinal = implode(",", $AllUrl_ ); // Change spaces by commas
$AllUrlFinal=trim($AllUrlFinal, ","); // would cut trailing and prefixing commas.
$sqlL .= "DELETE FROM Table WHERE ID= '4' AND URL NOT IN($AllUrlFinal);";