![](/img/trans.png)
[英]mysql insert into two tables using auto_increment value from the first table as a foreign key in the second
[英]Mysql insert in two tables where the first table has foreign key and update the first table
例如,如果我從品牌插入寶馬汽車,我在數據庫中就會有這個。我如何使用sql更新寶馬ID的id_make和我的代碼(我知道代碼很混亂,沒有資格)
http://i.stack.imgur.com/GQQg1.png
這是我的表格:
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Add Car:</h4>
</div>
<div class="modal-body ">
<form action="index.php?page=carlist" method="POST" autocomplete="off">
<div class="form-group">
<label for="marca">Marca:</label>
<!-- <input type="text" class="form-control" name="marca" placeholder="Marca" data-toggle="modal" data-target="#marca"> -->
<div class="input-group">
<div class="input-group-btn">
<input type="text" class="form-control dropdown-toggle " data-toggle="dropdown" placeholder="
<?php
if (!isset($brd['name']))
{
echo "Brand";
} else
echo $brd['name'];
?>" name="marca" readonly="readonly">
<ul class="dropdown-menu scrollable">
<?php foreach ($brds as $brd)
{ ?>
<li><a href='#'><?php echo $brd['name']; ?></a></li>
<?php } ?>
</ul>
</div>
<!-- /btn-group -->
</div>
<!-- /input-group -->
</div>
<div class="form-group">
<label for="model">Model:</label>
<input type="text" class="form-control" name="model" placeholder="Model">
</div>
<div class="form-group">
<label for="color">Color:</label>
<input type="test" class="form-control" name="color" placeholder="Color">
</div>
<div class="form-group">
<label for="release_date">Release date:</label>
<div class=" date">
<input type="date" class="form-control" name="release_date" placeholder="Release date"
id='release_date'>
<!-- <span class="input-group-addon add-on"><span class="glyphicon glyphicon-calendar"></span></span> -->
</div>
</div>
<div class="form-group">
<label for="Value">Value:</label>
<input type="test" class="form-control" name="value" placeholder="Value">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<input type="submit" name="addCar" class="btn btn-primary" value="Add car">
</div>
</form>
</div>
</div>
(來自品牌輸入的php將顯示db中的所有品牌)
當我從列表中選擇品牌並放入型號,顏色,價值,發布日期時,我按添加汽車,然后php在db上建立連接,然后在表格模型上插入汽車的模型,然后我需要從中添加ID列表中選擇了巫婆汽車品牌,然后使用汽車ID更新模型id_make。
也許您需要使用以下命令請求剛剛插入的品牌的ID:
mysql_insert_id();
這樣,您將獲得剛剛插入的記錄的ID,並將該ID用於其他表中的品牌。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.