简体   繁体   English

使用PHP语句将MySQL插入到多个表

[英]mySQL INSERT to multiple tables using PHP statments

I am struggling to insert mySQL data into multiple tables using PHP statements. 我正在努力使用PHP语句将mySQL数据插入多个表中。

I have two tables: 我有两个表:

user 用户

user_ID (PK) user_ID(PK)

first_name 名字

last_name

email 电子邮件

password 密码

profile 轮廓

profile_ID (PK) profile_ID(PK)

user_ID (FK) user_ID(FK)

profile_image profile_image

profile_image_name profile_image_name

I update the user table like so: 我这样更新用户表:

$sql = "INSERT INTO user (first_name, last_name, email, password)
    VALUES
    ('$_POST[first_name]', '$_POST[last_name]', '$_POST[email]', '$_POST[password]')";

The user_ID is set to auto increment in the database, so when this code is executed the inserted data is automatically assigned to a unique PK, and stored in a session. user_ID在数据库中设置为自动递增,因此执行此代码时,插入的数据将自动分配给唯一的PK,并存储在会话中。 At this same point though (creating the account) I need to insert the user_ID into the profile table as the FK. 但是,在同一时间(创建帐户),我需要将user_ID作为FK插入到配置文件表中。 That way when the user uploads their profile image, it will be assigned to they will have a record in the profile table. 这样,当用户上载其个人资料图像时,该图像将被分配给他们在个人资料表中有一条记录。

For example, on the image upload page I'm using this code: 例如,在图片上传页面上,我使用以下代码:

        $user_ID = $_SESSION['login'];


        $ins1 = mysqli_query($db, "UPDATE profile SET `profile_image_name`='" . $profile_image_name . "' WHERE `user_ID`='$user_ID'");
        $ins2 = mysqli_query($db, "UPDATE profile SET `profile_image`='" . $profile_image . "' WHERE `user_ID`='$user_ID'");

This code only works if the user_ID FK is already present in the profile table so how do I insert it using php statements like the one I did earlier? 仅当配置文件表中已经存在user_ID FK时,此代码才有效,因此如何像以前一样使用php语句插入它? I tried using the first approach again but it doesn't seem to be working... 我尝试再次使用第一种方法,但似乎不起作用...

You can use insert_id for get last PK auto increment that you insert on Mysql 您可以使用insert_id获取您在Mysql上插入的最后一个PK自动增量

http://php.net/manual/mysqli.insert-id.php http://php.net/manual/mysqli.insert-id.php

Insert creates a new record, update changes an existing record. 插入会创建新记录,更新会更改现有记录。 With your current scheme you need to INSERT into profile. 使用当前方案,您需要插入概要文件。 Obviously this would mean that you can't use the same code to create a profile as you use to change a profile. 显然,这意味着您不能使用与更改配置文件相同的代码来创建配置文件。 But there is a further option REPLACE has the same syntax as INSERT, but will overwrite an existing record. 但是还有另一个选择,REPLACE的语法与INSERT相同,但是会覆盖现有记录。

However if each user only has a single profile, the details should be held in a single table, not two. 但是,如果每个用户只有一个配置文件,则详细信息应保存在一个表中,而不是两个表中。

Comment: Storing passwords as plain text is a very bad idea, go read up on password hashing. 评论:将密码存储为纯文本是一个非常糟糕的主意,请继续阅读密码哈希。 You should also have a Google for SQL injection; 您还应该拥有一个用于SQL注入的Google; your authentication is trivial to bypass and easy to destroy. 您的身份验证很容易绕开,并且容易销毁。

Try this: 尝试这个:

INSERT INTO
`profile`(`user_ID`,`profile_image`,`profile_image_name`)
VALUES
('$user_ID','$profile_image','$profile_image_name')
ON DUPLICATE KEY UPDATE 
`profile_image`='$profile_image',`profile_image_name`='$profile_image_name'

user_ID must be unique user_ID必须是唯一的

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

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