Home > Backend Development > PHP Tutorial > mysql - php怎么同时向2张表里插入数据

mysql - php怎么同时向2张表里插入数据

WBOY
Release: 2016-06-06 20:34:26
Original
1644 people have browsed it

情况是这个样子的:我要做一个发消息的表,因为接受人可能是多个,所以又给接收人一单独的表,(这种方案好还是全部都放到一张表里好点呢?)2张表的字段如下:
mysql - php怎么同时向2张表里插入数据

mysql - php怎么同时向2张表里插入数据

message_id是第一张表的主键,
如果收件人有好几个,第二张表:mysql - php怎么同时向2张表里插入数据

而第一张表:mysql - php怎么同时向2张表里插入数据

我已经写了一部分代码了,就是不知道怎么把message_id的值传到第二张表,求指导,谢谢

<code>if(isset($_POST['submitted'])) {
    $message = $_POST['message'];
    $send_people = $_POST['send_people'];
    session_start();
           $user_id = $_SESSION['user_id'];
    $q = "insert into send_message(user_id,user_name,message_content) VALUES ('" . $user_id . "','" . $user_name . "','" . $message . "')";
    $r = mysqli_query($dbc, $q);
    printf("%s",mysqli_error($dbc));
        for ($i = 0; $i </code>
Copy after login
Copy after login

回复内容:

情况是这个样子的:我要做一个发消息的表,因为接受人可能是多个,所以又给接收人一单独的表,(这种方案好还是全部都放到一张表里好点呢?)2张表的字段如下:
mysql - php怎么同时向2张表里插入数据

mysql - php怎么同时向2张表里插入数据

message_id是第一张表的主键,
如果收件人有好几个,第二张表:mysql - php怎么同时向2张表里插入数据

而第一张表:mysql - php怎么同时向2张表里插入数据

我已经写了一部分代码了,就是不知道怎么把message_id的值传到第二张表,求指导,谢谢

<code>if(isset($_POST['submitted'])) {
    $message = $_POST['message'];
    $send_people = $_POST['send_people'];
    session_start();
           $user_id = $_SESSION['user_id'];
    $q = "insert into send_message(user_id,user_name,message_content) VALUES ('" . $user_id . "','" . $user_name . "','" . $message . "')";
    $r = mysqli_query($dbc, $q);
    printf("%s",mysqli_error($dbc));
        for ($i = 0; $i </code>
Copy after login
Copy after login

如果 message_id 是自增的话用

<code>sql</code><code>mysql_insert_id
</code>
Copy after login

链接描述

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template