Note that there are some explanatory texts on larger screens.

plurals
  1. POInsert POST data to table and POST array to seperate table
    primarykey
    data
    text
    <p>I am trying to submit data from a form to two separate tables. </p> <p><strong>Here's the error:</strong> It inserts into table 1 fine but table2 array data goes into the database as "<strong>Array</strong>".</p> <p>Here are my fields going into table1:</p> <pre><code> $start = $_POST['start']; $end = $_POST['end']; $customer = $_POST['customer']; $manufacturer = $_POST['manufacturer']; $rep = $_POST['rep']; $notes = $_POST['notes']; </code></pre> <p>My array fields going into table2:</p> <pre><code> item[] description[] pack[] </code></pre> <p>Any help is appreciated. Below is the code I have developed thus far:</p> <pre><code> if ($start == '' || $end == '') { $error = '&lt;div class="alert alert-error"&gt; &lt;a class="close" data-dismiss="alert"&gt;×&lt;/a&gt; &lt;strong&gt;Error!&lt;/strong&gt; Please fill in all required fields! &lt;/div&gt;'; } else { $sql = "SELECT COALESCE(MAX(GroupID), 0) + 1 AS newGroupID FROM table1"; try { $stmt = $db-&gt;prepare($sql); $stmt-&gt;execute(); } catch(PDOException $ex) { die("Failed to run query: " . $ex-&gt;getMessage()); } $rows = $stmt-&gt;fetchAll(); foreach($rows as $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-&gt;prepare("INSERT table1 (GroupID, start, end, customer, manufacturer, rep, notes) VALUES (?, ?, ?, ?, ?, ?, ?)")) { $stmt-&gt;bind_param("issssss", $groupID, $start, $end, $customer, $manufacturer, $rep, $notes); $stmt-&gt;execute(); $stmt-&gt;close(); } else { echo "ERROR: Could not prepare SQL statement 1."; } $mysqli-&gt;error; $mysqli-&gt;close(); $success = "&lt;div class='alert alert-success'&gt;New agreement added.&lt;/div&gt;"; $mysqli = new mysqli("localhost", "user", "pw", "mydb"); if (mysqli_connect_errno()) { die(mysqli_connect_error()); } if ($stmt = $mysqli-&gt;prepare("INSERT table2 (GroupID, item_number, item_description, pack) VALUES (?, ?, ?, ?)")) { foreach($_POST['item'] as $i =&gt; $item) { $item = $_POST['item']; $description = $_POST['description']; $pack = $_POST['pack']; } $stmt-&gt;bind_param("isss", $GroupID, $item, $description, $pack); $stmt-&gt;execute(); $stmt-&gt;close(); } else { echo "ERROR: Could not prepare SQL statement 2."; } $mysqli-&gt;error; $mysqli-&gt;close(); $success = "&lt;div class='alert alert-success'&gt;New agreement items added!&lt;/div&gt;"; } } </code></pre>
    singulars
    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.
 

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