php - Insert POST data to table and POST array to seperate table -


i trying submit data form 2 separate tables.

here's error: inserts table 1 fine table2 array data goes database "array".

here fields going table1:

  $start = $_post['start'];   $end = $_post['end'];   $customer = $_post['customer'];   $manufacturer = $_post['manufacturer'];   $rep = $_post['rep'];   $notes = $_post['notes']; 

my array fields going table2:

  item[]   description[]   pack[] 

any appreciated. below code have developed far:

 if ($start == '' || $end == '')                     {                             $error = '<div class="alert alert-error">                 <a class="close" data-dismiss="alert">×</a>                 <strong>error!</strong> please fill in required fields!             </div>';                     }                     else                     {                         $sql = "select coalesce(max(groupid), 0) + 1 newgroupid table1"; try      {      $stmt = $db->prepare($sql);      $stmt->execute();  }  catch(pdoexception $ex)  {      die("failed run query: " . $ex->getmessage());  }   $rows = $stmt->fetchall();   foreach($rows $row) { $groupid = $row['newgroupid']; }  $mysqli = new mysqli("localhost", "user", "pw", "mydb");     if (mysqli_connect_errno()) {         die(mysqli_connect_error());     }     $start = $_post['start'];   $end = $_post['end'];   $customer = $_post['customer'];   $manufacturer = $_post['manufacturer'];   $rep = $_post['rep'];   $notes = $_post['notes'];      if ($stmt = $mysqli->prepare("insert table1 (groupid, start, end, customer, manufacturer, rep, notes) values (?, ?, ?, ?, ?, ?, ?)"))                             {                                     $stmt->bind_param("issssss", $groupid, $start, $end, $customer, $manufacturer, $rep, $notes);                                     $stmt->execute();                                     $stmt->close();                             }                              else                             {                                     echo "error: not prepare sql statement 1.";                             }                               $mysqli->error;                             $mysqli->close();                             $success = "<div class='alert alert-success'>new agreement added.</div>";                                $mysqli = new mysqli("localhost", "user", "pw", "mydb");                             if (mysqli_connect_errno()) {                                 die(mysqli_connect_error());                             }                              if ($stmt = $mysqli->prepare("insert table2 (groupid, item_number, item_description, pack) values (?, ?, ?, ?)"))                             {                                     foreach($_post['item'] $i => $item) {                                         $item = $_post['item'];                                         $description = $_post['description'];                                         $pack = $_post['pack'];                                     }                                      $stmt->bind_param("isss", $groupid, $item, $description, $pack);                                     $stmt->execute();                                     $stmt->close();                             }                              else                             {                                     echo "error: not prepare sql statement 2.";                             }                               $mysqli->error;                             $mysqli->close();                             $success = "<div class='alert alert-success'>new agreement items added!</div>";               }             } 

see if helps:

update: since op's original code not give correct value $groupid, 1 way resolve that: (this based on assumption op needs different values groupid each insert queries)

$groupid_arr = array(); $rows = $stmt->fetchall();  foreach($rows $row) {     $groupid_arr[] = $row['newgroupid']; }  if ($stmt = $mysqli->prepare("insert table2 (groupid, item_number, item_description, pack) values (?, ?, ?, ?)")){     foreach($_post['item'] $i => $item) {         // since item, description , pack multi-dimensional arrays,         // how reference them         $item = $_post['item'][$i];         $description = $_post['description'][$i];         $pack = $_post['pack'][$i];         $groupid = $groupid_arr[$i];         $stmt->bind_param("isss", $groupid, $item, $description, $pack);         $stmt->execute();     }     $stmt->close(); } else{     echo "error: not prepare sql statement 2."; } 

this specific part of code. if not work there might errors in other parts of code. also, looking @ other parts of code, see value $groupid not correct since overriding value in foreach loop.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -