我正在尝试将表单中的数据提交到两个单独的表中。
这是错误:它很好地插入到表1中,但是table2数组数据作为“数组”进入数据库。
以下是我进入table1的字段:
$start = $_POST['start'];
$end = $_POST['end'];
$customer = $_POST['customer'];
$manufacturer = $_POST['manufacturer'];
$rep = $_POST['rep'];
$notes = $_POST['notes'];
我的数组字段进入table2:
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>";
}
}
答案 0 :(得分:1)
看看这是否有帮助:
更新: 由于OP的原始代码没有为$ GroupID提供正确的值,因此这是解决该问题的一种方法:(这是基于OP为每个插入查询需要不同的GroupID值的假设)
$GroupID_arr = Array();
$rows = $stmt->fetchAll();
foreach($rows as $row) {
$GroupID_arr[] = $row['newGroupID'];
}
if ($stmt = $mysqli->prepare("INSERT into table2 (GroupID, item_number, item_description, pack) VALUES (?, ?, ?, ?)")){
foreach($_POST['item'] as $i => $item) {
// since item, description and pack are multi-dimensional arrays,
// this is how you 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: Could not prepare SQL statement 2.";
}
这是代码的特定部分。如果这不起作用,那么代码的其他部分可能会出错。 另外,查看代码的其他部分,我看到你将得到的$ GroupID的值不正确,因为你覆盖了foreach循环中的值。