¿Cómo asignar y concatizar usando múltiples delimitadores en una matriz?

i apologize if the question is wrong. i am a still a newbie and a learner however i would appreciate if someone correct me if i am somewhere wrong.

here in the Class method i am using for Inserting the data into the database

public function insert($table,$col,$value)
{
    if(is_array($col) && is_array($value))
    {
        $query = "INSERT INTO ".$table."(" . implode(",",$col) . ") VALUES(" . implode(",",$value) . ")";
    }
    else
    {
        $query = "INSERT INTO " . $table . "(" . $col . ") VALUES(". $value . ")";
    }
}

now here i am determining if the $col and $value is an array if yes then process it.

however i have a problem here since the VALUES in the Insert statement needs to be represnted in the single or double quote format it will not process the query and hence print the error

for example the below code would print the error

$query = "INSERT INTO users(username,email) VALUES(test,test@test.com)";

and the correct format will be

$query = "INSERT INTO users(username,email) VALUES('test','test@test.com')";

now in the col value i would like to add the single quotes to every value in the array for example the $value array which is like this.

$value = array('test','test@test.com');

should give back the value

'test','test@test.com'

en lugar de

test,test@test.com

how do i achieve it?

preguntado el 09 de enero de 11 a las 11:01

It will give back the second example because the single quotes are for PHP to parse, and are not part of the strings. -

Plus, concatenating like that will be very ugly, try sprintf() instead. You also need to make sure you escape your table, column and values. -

yes then how do i make sure that single quotes remains in the string so that i can parse that values using mysql_query(); -

i would appreciate if you could explain me with the codes as i am still a newbie and i am not able to understand what are you exactly trying to say. -

4 Respuestas

 $query = "INSERT INTO $table ('" . implode("','",$col) . "')
           VALUES ('" . implode("','",$value) . "')";

Make sure that neither $col nor $value is empty.

Respondido el 09 de enero de 11 a las 14:01

Right code:

public function insert($table,$col,$value)
{
    if(is_array($col) && is_array($value))
    {
        $query = "INSERT INTO ".$table."(" . implode(",",$col) . ") VALUES('" . implode("','",$value) . "')";
    }
    else
    {
        $query = "INSERT INTO " . $table . "(" . $col . ") VALUES('". $value . "')";
    }
}

CHANGE:

VALUES(" . implode(",",$value) . ")

A

VALUES('" . implode("','",$value) . "')

(Your output:)

VALUES(demo,demo2)

(New Output:)

VALUES('demo','demo2')

Respondido el 08 de enero de 14 a las 15:01

you could do this?

$value = array("'test'","'test@test.com'");

Respondido el 09 de enero de 11 a las 14:01

Puedes usar array_map () método:

function addQuotes($str)
{
    return "'".$str."'";
}

$value = array_map("addQuotes", $value);

or follow a Oswald's answer recommendations.

Respondido el 09 de enero de 11 a las 14:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.