【问题标题】:Mysql insert in two tables where the first table has foreign key and update the first tableMysql在第一个表有外键的两个表中插入并更新第一个表
【发布时间】:2015-11-12 19:37:58
【问题描述】:

例如,如果我从品牌插入到宝马汽车,我在 db 中有这个。我如何使用 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">&times;</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。

【问题讨论】:

  • 你应该重新安排你的问题..你在路上把我弄丢了..
  • 我认为一个代码示例会让你的问题更容易理解。
  • 你应该重写那个问题,它不可读你可能需要一个叫做 ON UPDATE CASCADE dba.stackexchange.com/questions/74627/…
  • 我有一个带有单个表的经典插入。我把 CASCADE 放在那里。
  • 我必须承认我也在路上迷路了@MateiMihai

标签: php mysql database


【解决方案1】:

也许您需要使用以下方式请求您刚刚插入的品牌的 ID:

mysql_insert_id();

这样您就可以得到刚刚插入的记录的 ID,并将该 ID 用于其他表中的 make。

【讨论】:

  • 谢谢伙计,它与我没有使用 mysql_insert_id() 的 id bun 一起工作。这个想法很好。
猜你喜欢
  • 2012-05-13
  • 1970-01-01
  • 2015-05-29
  • 2018-09-15
  • 1970-01-01
  • 2015-11-17
  • 2021-08-24
  • 1970-01-01
相关资源
最近更新 更多