user2881809
user2881809

Reputation:

PHP How add quotes?

$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);";

In result we get next $sqlL:

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

But in not right and this it will not work.

Tell me please how add quotes in $Allel ?

P.S.: i would like get next query:

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

Upvotes: 0

Views: 185

Answers (8)

JokiRuiz
JokiRuiz

Reputation: 311

Try this:

<?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);";

Upvotes: 0

Kickstart
Kickstart

Reputation: 21513

While using an array and implode is probably the best solution, the problem with your code is that you set up one variable with the quotes, then instead add a different variable to the 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);";

To use implode, add the items to an array (ignoring the comma), then just implode that array with a separating comma in the final assignment into the DELETE statement:-

$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).");";

You should be escaping the string using mysql_real_escape_string or equivalent. Not sure which database drivers you are using so although the mysql_* drivers are deprecated they will do for an example:-

$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).");";

Upvotes: 1

Daniel W.
Daniel W.

Reputation: 32260

There is only one answer to this... use prepared statements and don't build your own query using string functions and "handcraft" quotation marks and stuff. It is risky and the opposite of clean programing.

Anyways:

If $Allel contains pure values, use

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

First line will wrap ' arround the values, second line will concat each with ,.

Upvotes: 1

Satish Sharma
Satish Sharma

Reputation: 9625

try this

$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);";

Upvotes: 0

JSunny
JSunny

Reputation: 477

You can use implode function,

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

See the example below

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

Array is: 1,2,3

Upvotes: 0

t0mpl
t0mpl

Reputation: 5025

i think he wants something different , can you try it ?

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

and sql

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

Upvotes: 1

clami219
clami219

Reputation: 3038

Try with this:

$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);";

Upvotes: 1

Fabien Papet
Fabien Papet

Reputation: 2319

You can use the PHP implode() function

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

PHP Documentation

Upvotes: 0

Related Questions