0

I am using following function:

function insertRow($table,$column,$values) {
    foreach($column as $data) {
        if($data==end(array_values($column))) {
            $params .= $data;
        } else {
            $params .= ''.$data.', ';
        }
    }
    foreach($values as $data2) {
        if($data==end(array_values($values))) {
            $value .= "\"$data2\"";
        } else {
            $value .= "\"$data2\", ";
        }
    }
    $sql = 'INSERT INTO '.$table.' ('.$params.') VALUES ('.$value.')';
    echo $sql;
}

When I try following code:

    $column = array('title','link');
    $values = array('input1','input2');
    $result = insertRow('categories',$column,$values);
echo $result;

The output is:

INSERT INTO categories (title, link) VALUES ("input1""input2")

But the expected output is:

INSERT INTO categories (title, link) VALUES ("input1","input2")

Can anyone help me out please?

  • Just use `implode` to generate a comma-separated list instead e.g.https://3v4l.org/eUX8U – Nick Sep 26 '20 at 04:27
  • this doesn't solves my problem. please see the expected result. also I need to escape each strings. – Meraj-Ul Islam Sep 26 '20 at 04:48
  • Please check the updated link, it has the strings escaped. Note that you should use single quotes (`'`) in MySQL for string data as double quotes (`"`) can in certain modes mean a column name. – Nick Sep 26 '20 at 04:49

0 Answers0