MySql/PHP - 如何同时将数据插入 2 个不同的表中(使用 Array)


MySql/PHP - How can I insert data into 2 different tables at the same time (using Array)

我有一个创建请求的表单。通过单击"创建请求"按钮,所有信息都将添加到"Requests_Table"中。这工作得很好。每个请求都有多个项目(至少一个)...每个项目都由自己的特征组成(这就是为什么我想将此信息存储在单独的表"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/stackoverflow/source.txt 发布了此页面的完整代码

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

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

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

用户是提供串行的人还是预定义的? 这应该是您插入所需的代码

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

request_id应该从请求表中获取。

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

$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}'
                )";