NoooSmyth
NoooSmyth

Reputation: 91

PHP MySQL inserting multiple values from drop box selections

I have a table populated by a MySQL table, and I am trying to insert the selected rows from the table into another table.

The table look like below: image.

There are first three fields in the table are coming from two other tables depending on the Field values.

And the drop down selections are provided by user using:

$sql = "SELECT project_proposals.id, project_proposals.title, project_proposals.description, project_proposals.academicname, flux_student_records.studentname, flux_student_records.id, flux_student_records.programme, flux_student_records.academicdiscipline FROM project_proposals JOIN flux_student_records ON project_proposals.academicdiscipline = flux_student_records.academicdiscipline 
WHERE project_proposals.academicdiscipline = '$academicdiscipline' AND flux_student_records.studentname = '$studentname'";

$retval = mysql_query($sql) or die(mysql_error());

What will be the best way to insert these multiple selected rows into another table please? Thanks.

Upvotes: 0

Views: 759

Answers (2)

rrobben
rrobben

Reputation: 483

You can loop trough an post and then do some thing with that information. So for example:

    <?php

function readDataForwards($dbh) {
  $sql = 'SELECT * FROM yourdb';
  try {
    $stmt = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
    $stmt->execute();
    while ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) {
      $data = $row[0] . "\t" . $row[1] . "\t" . $row[2] . "\n";
      print $data;
      #insert the data into another database
      #you can also filter some duplicated items that you already 
      #have in the database
    }
    $stmt = null;
  }
  catch (PDOException $e) {
    print $e->getMessage();
  }
}

    ?>

this would output: the rows in the database but it could also insert the rows into your database.

Upvotes: 0

leo
leo

Reputation: 366

"select into" is the way to go.

    SELECT into YourNewTable select project_proposals.id, project_proposals.title, project_proposals.description, project_proposals.academicname, flux_student_records.studentname, flux_student_records.id, flux_student_records.programme, flux_student_records.academicdiscipline FROM project_proposals JOIN flux_student_records ON project_proposals.academicdiscipline = flux_student_records.academicdiscipline 
WHERE project_proposals.academicdiscipline = '$academicdiscipline' AND flux_student_records.studentname = '$studentname'";

Hope you find this usefull.

Upvotes: 1

Related Questions