-1

我正在尝试将表单中的数据提交到两个单独的表。

这是错误:它可以很好地插入表 1,但表 2 数组数据作为“数组”进入数据库。

这是我进入 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>";


            }
            }
4

1 回答 1

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 循环中的值。

于 2013-08-13T00:30:10.203 回答