【问题标题】:MySql/PHP - How can I insert data into 2 different tables at the same time (using Array)MySql/PHP - 如何同时将数据插入到 2 个不同的表中(使用数组)
【发布时间】:2026-02-09 19:15:01
【问题描述】:

我有一个创建请求的表单。通过单击“创建请求”按钮,所有信息都将添加到“请求表”中。这是完美的工作。每个请求都有多个项目(至少一个)......并且每个项目都包含自己的特征(这就是为什么我想将此信息存储在单独的表“SerialNumber_Table”中。

所以,我需要帮助将序列号信息(类型、型号、序列号)插入“SerialNumber_Table”。我相信我还需要使用 mysql_insert_id(); 将每个序列号与正确的 request_id 相关联。

` //序列号的变量

    $machineType = mysql_prep($_POST['machineType']);
    $machineModel = mysql_prep($_POST['machineModel']);
    $serialNumber = mysql_prep($_POST['serialNumber']);

            <fieldset class="row2">
            <h1><?php echo $LANG['hardware_details']; ?></h1>
            <p style="margin: 10px 0 10px 0; ">
                <p style="margin: 10px 0 10px 0; font-size:12px; padding: 0;"><?php echo $LANG['delete_machines']; ?></p>
                <input type="button" value="<?php echo $LANG['add_machines']; ?>" onClick="addRow('dataTable')" style="width: 100px; padding: 2px 5px 2px 5px; background: #e1e1e1; color: #000; font-weight: bold; font-size: 12px; cursor: pointer;" /> 
                <input type="button" value="<?php echo $LANG['remove_machines']; ?>" onClick="deleteRow('dataTable')" style="width: 130px; margin-left: 10px; padding: 2px 5px 2px 5px; background: #e1e1e1; color: #000; font-weight: bold; font-size: 12px; cursor: pointer;" /> 
            </p>
           <table id="dataTable" class="form" border="1">
              <tbody>
                <tr>
                    <td><input type="checkbox" style=" width: 30px; "/></td>
                    <td>
                        <label style="margin-left: 10px;"><?php echo $LANG['machineType']; ?></label>
                        <input type="text" value="" id="machineType" name="machineType[]" style="width: 70px; margin: 5px 10px 10px 10px; ">
                     </td>
                     <td>
                        <label for="" style="margin-left: 10px;"><?php echo $LANG['machineModel']; ?></label>
                        <input type="text" value="" id="machineModel" name="machineModel[]" style="width: 70px; margin: 5px 10px 10px 10px;">
                     </td>
                     <td>
                        <label for="" style="margin-left: 10px;"><?php echo $LANG['serialNumbers']; ?></label>
                        <input type="text" value="" id="serialNumber" name="serialNumber[]" style="width: 120px; margin: 5px 10px 10px 10px;">
                     </td>
                </tr>
                </tbody>
            </table>
            <div class="clear"></div>
        </fieldset>

            <!-- End Dynamic Forms for Serial Numbers -->

` 我怎么能完成这样的任务? 如果有任何帮助,我已在http://aerco.com.br/*/source.txt 发布此页面的整个代码

任何人都可以在这里指出正确的方向吗? 提前感谢您的任何想法

【问题讨论】:

    标签: php mysql arrays insert


    【解决方案1】:

    我无法得到您的确切需求,但据我了解,您可以使用代码插入一个表,然后使用 MySQL 触发器插入另一个表。

    在这里解释:http://dev.mysql.com/doc/refman/5.0/en/triggers.html

    【讨论】:

      【解决方案2】:

      用户是提供序列号的人还是预定义的?这应该是您需要插入的代码

      $query = "INSERT INTO serialnumber_table(Type,Model,Serial number,request_id) VALUES ($machineType, $machineModel,$serial_number,request_id)
      

      request_id 应该从 requests 表中获取。

      如果我的理解是正确的,在这段代码之后插入

      $query = "INSERT INTO requests(
                          request_date, requester_name, requester_email, client_name, client_country, opportunity_number, machine_quantity, severity, specialist, created_by, sales_connect
                      ) VALUES (
                          '{$request_date}', '{$requester_name}', '{$requester_email}', '{$client_name}', '{$client_country}', '{$opportunity_number}', '{$machine_quantity}', '{$severity}', '{$specialist}', '{$created_by}', '{$sales_connect}'
                      )"; 
      

      【讨论】: