简体   繁体   English

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

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

I have a form that creates request. 我有一个创建请求的表格。 By clicking on the 'Create Request' button all the information is added to the 'Requests_Table'. 通过单击“创建请求”按钮,所有信息都将添加到“ Requests_Table”中。 This is working perfectly. 这很正常。 Each request has more than one item (at least one)... and each item is comprised of its own characteristics (that's why I want to store this info on a separate table 'SerialNumber_Table'. 每个请求都有一个以上的项目(至少一个)...,每个项目都具有自己的特征(这就是为什么我要将此信息存储在单独的表“ SerialNumber_Table”中的原因。

So, I need help inserting the Serial number information (Type,Model,Serial number) into 'SerialNumber_Table'. 因此,我需要帮助将序列号信息(类型,型号,序列号)插入“ SerialNumber_Table”。 I believe I'll also need to use mysql_insert_id(); 我相信我也需要使用mysql_insert_id(); to associate each serial number with the correct request_id. 将每个序列号与正确的request_id相关联。

` //Variables for Serial Numbers `//序列号的变量

    $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 -->

` How could I achieve such a task? `我怎么能完成这样的任务? If it is any help, I have posted the entire code for this page at http://aerco.com.br/stackoverflow/source.txt 如果有帮助,我已在http://aerco.com.br/stackoverflow/source.txt上发布了此页面的完整代码

Anyone can point me in the right direction here? 任何人都可以在这里指出正确的方向吗? Thanks in advance for any thoughts 预先感谢您的任何想法

I could not get what is yout exact need, but for my understanding, You can insert into one table using code and another using MySQL trigger. 我无法满足您的确切需求,但是据我所知,您可以使用代码插入一个表,而使用MySQL触发器插入另一个表。

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

would the user be the one to supply the serial or is it predefined? 用户是提供序列号的用户还是预定的? this should be the code you need for inserting 这应该是您需要插入的代码

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

request_id should be gotten from requests table. request_id应该从请求表中获取。

if my understanding is correct, insert after this code 如果我的理解是正确的,请在此代码后插入

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM