フォームから 2 つの別々のテーブルにデータを送信しようとしています。
エラーは次のとおりです。テーブル 1 には正常に挿入されますが、テーブル 2 の配列データは " Array " としてデータベースに入ります。
テーブル1に入る私のフィールドは次のとおりです。
$start = $_POST['start'];
$end = $_POST['end'];
$customer = $_POST['customer'];
$manufacturer = $_POST['manufacturer'];
$rep = $_POST['rep'];
$notes = $_POST['notes'];
テーブル2に入る私の配列フィールド:
item[]
description[]
pack[]
どんな助けでも大歓迎です。以下は、これまでに開発したコードです。
if ($start == '' || $end == '')
{
$error = '<div class="alert alert-error">
<a class="close" data-dismiss="alert">×</a>
<strong>Error!</strong> Please fill in all required fields!
</div>';
}
else
{
$sql = "SELECT COALESCE(MAX(GroupID), 0) + 1 AS newGroupID FROM table1";
try
{
$stmt = $db->prepare($sql);
$stmt->execute();
}
catch(PDOException $ex)
{
die("Failed to run query: " . $ex->getMessage());
}
$rows = $stmt->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->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: Could 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'] as $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: Could not prepare SQL statement 2.";
}
$mysqli->error;
$mysqli->close();
$success = "<div class='alert alert-success'>New agreement items added!</div>";
}
}