Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do i insert arrays into mysql using PHP?
    primarykey
    data
    text
    <p>I have a complex form that has 11 columns. As for rows they will vary from about 20 to 50 depending on number of students. I am capturing data via a <code>php script</code> as arrays. Each column produces an array. Example, from the form I have <code>fname[], lname[], exam_no[]</code> etc so when the form is submitted with say, 10 rows, I end up with 11 arrays each with 10 entries, which I pass through some <code>php function</code> to remove empty elements. The arrays are being generated with no issues.</p> <p>I want to insert data from these arrays into <code>mysql</code> using a loop. Basically, <code>lname array</code> will have all first_name for the first name column, <code>lname array</code> will feed the last_name column of the db and so on.</p> <p>I am just unable to even start constructing the <code>MySQL query</code> to insert the data, I am well conversant with the 'ordinary' insert where you have columns and values and you already know how many rows will insert, mostly one row per insert.</p> <p>Is is even possible to populate a <code>MySQL Database Table</code> with an insert using a <code>PHP loop</code> and with this many number of columns and making it flexible to insert any number of rows as that will vary each time a user enters student data?</p> <p>UPDATE</p> <p>This is what I came up with. It works but NOT as desired!</p> <p>Arrays are coming like <code>$fname[], lname[]</code> etc Then I built the master array to be <code>$master_array=array['$fname, $lname];</code></p> <pre><code>$sql = "INSERT INTO testing (date, fname, lname) VALUES "; foreach($master_array as $subarray) { $sql .= "(NOW( )"; echo 'A nested array: '.$subarray.'&lt;br /&gt;'; foreach($subarray as $value) { $sql .= ", '$value'"; echo 'A Name: '.$value.'&lt;br /&gt;'; } $sql.= "), "; } $sql = substr($sql,0,-2); //removes extraneous , at end. echo $sql; $result=mysqli_query($dbc, $sql) or die('Query Failed!'); ?&gt; </code></pre> <p>Since my query involves conactinating small pieces of code, I was printing it after its built to see what is to be inserted. It looked like so;</p> <pre><code>INSERT INTO table (date, fname, lname) VALUES (NOW( ), 'Andrew', 'Alex'), (NOW( ), 'Peterson', 'Marlon') </code></pre> <p>As I suspected, it inserts all first names in the first row, and all last names in the second row. The desired result is to insert first names in the first-name column and second names in the second name column. So now I need a way to insert all elements of one array into a single column and then move to the second array and insert it in the next column. That sounds complex, and I wonder if it's doable! Let me be educated by the masters of the php language as I am an intermediate or may be brand new newbie!</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.
 

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