Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to insert additional data into mysql while using implode
    primarykey
    data
    text
    <p>here is the code:</p> <pre><code> if (($handle = fopen($source_file, "r")) !== FALSE) { $columns = fgetcsv($handle, $max_line_length, ","); foreach ($columns as &amp;$column) { $column = str_replace(".","",$column); } while (($data = fgetcsv($handle, $max_line_length, ",")) !== FALSE) { while(count($data) &lt; count($columns)) { array_push($data, NULL); } $c = count($data); for($i = 0; $i &lt; $c; $i++) { $data[$i] = "'{$data[$i]}'"; } $sql[] = '(' . implode(',',$data) . ','.$_POST[group].')'; } $sql = implode(',',$sql); $query = "INSERT INTO mytable (".mysql_real_escape_string(implode(",",$columns)).",Custgroup,user_id) VALUES " . $sql . "\n"; mysql_query($query) or trigger_error(mysql_error()); fclose($handle); } } </code></pre> <p>If my csv file is:</p> <p>lastname,firstname,gender</p> <p>bob,ah,male</p> <p>So now the query will be : <code>INSERT INTO mytable (lastname,firstname,gender) VALUES ('bob','ah','male')</code>. But how if I want to insert extra data into mysql together with the data in csv file? Such as I have a value $_POST['group'] = 'family' and $_POST['user_id'] = '10', so I tried:</p> <pre><code>$sql[] = '(' . implode(',',$data) . ','.$_POST[group].','.$_POST[user_id].')'; $query = "INSERT INTO $target_table (".mysql_real_escape_string(implode(",",$columns)).",custgroup,user_id) VALUES " . implode(',',$sql) . "\n"; </code></pre> <p>But in the query it will become : <code>INSERT INTO mytable (lastname,firstname,gender,custgroup,user_id) VALUES ('bob','ah','male',family,10)</code>. It didn't have single quote , so I have error to insert the record.Can I know how to solve this problem?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload