![](/img/trans.png)
[英]How can i insert data in two different tables in mysql database at the same time?
[英]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}'
)";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.