开发者

How do i insert arrays into mysql using PHP?

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 php script as arrays. Each column produces an array. Example, from the form I have fname[], lname[], exam_no[] 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 php function to remove empty elements. The arrays are being generated with no issues.

I want to insert data from these arrays into mysql using a loop. Basically, lname array will have all first_name for the first name column, lname array will feed the last_name column of the db and so on.

I am just unable to even start constructing the MySQL query 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.

Is is even possible to populate a MySQL Database Table with an insert using a PHP loop 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?

UPDATE

This is what I came up with. It works but NOT as desired!

Arrays are coming like $fname[], lname[] etc Then I built the master array to be $master_array=array['$fname, $lname];

$sql = "INSERT INTO testing (date, fname, lname) VALUES ";
foreach($master_array as $subarray) {
   $sql .= "(NOW( )";
   echo 'A nested array: '.$subarray.'<br />';
   foreach($subarray as $value) {
      $sql .= ", '$value'";
      echo 'A Name: '.$value.'<br />';
   }
   $sql.= "), ";
}
$sql = substr($sql,0,-2); //removes extraneous , at end.
echo $sql;
$result=mysqli_query($dbc, $sql)
or die('Query Failed!');
?>

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;

INSERT INTO table (date, fname, lname) VALUES (NOW( ), 'Andrew', 'Alex'), (NOW( ), 'Peterson', 'Marlon')

As I suspected, it inserts all first na开发者_运维技巧mes 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!


Isn't it better to group the information by student?

Example:

<form...>
<? foreach ($students as $id => $info) : ?>
  <input type="text" name="fname;<=$id;?>" value="<?=$info['fname'];?>" />
  <input type="text" name="lname;<=$id;?>" value="<?=$info['lname'];?>" />
  etc
<? endforeach ?>
</form>

(I'm using PHP short tags here)

Then, when you process the form:

$update = array();

foreach ($_POST as $key => $value) {
  // key will look like: fname;1, fname;2, etc
  // so, split it on the ; sign to separate the field name from the student's id
  $data = explode(';',$key);

  // result:
  // $data[0] = fname, lname, etc
  // $data[1] = 1, 2, etc
  // you can add some checks to make sure that this field is valid
  // and that the id is in fact a valid id (number, > 0, etc)

  // sanitize data (however you want, just an example)
  $value = mysql_real_escape_string(trim($value));

  // now add it to the update array, grouped by student id
  $update[$data[1]][$data[0]] = $value;

  // result:
  // $update[1]['fname'] = 'First name student 1';
  // $update[1]['lname'] = 'Last name student 1';
  // $update[2]['fname'] = 'First name student 2';
  // $update[2]['lname'] = 'Last name student 2';
  // etc
}

After that, go through the update array:

foreach ($update as $id => $info) {
  $sql = "UPDATE students
          SET fname = '".$info['fname']."', lname = '".$info['lname']."'
          WHERE id = $id";
  mysql_query($sql);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜