简体   繁体   English

如何更新数据库中已插入并随后删除的值

[英]How to update values in database which are already inserted and later deleted

I have a table in database which has only one column and it stores the name of al the projects. 我在数据库中只有一张表,其中存储了所有项目的名称。 I have one registration page which displays all these projects where user can select any . 我有一个注册页面,其中显示所有这些项目,用户可以在其中选择任何项目。 Any changes done on table reflects on my registration page. 表格上所做的任何更改都会反映在我的注册页面上。

User when selects the project and registers then all the information of the user is stored in another table that Database. 用户在选择项目并注册时,用户的所有信息将存储在数据库的另一个表中。 My problem is if I do any changes in the table with projects name(delete or insert a name), it reflects on the registration page but it does not reflect on the user's database. 我的问题是,如果我在项目名称的表中进行了任何更改(删除或插入名称),它都会反映在注册页面上,但不会反映在用户数据库上。

For example suppose one user has selected p1,p2 and registered and later if I delete p2 from the database, it does gets deleted from user's database, only its deleted from registration page. 例如,假设一个用户选择了p1,p2并进行了注册,然后如果我从数据库中删除了p2,它确实会从用户的数据库中删除,只有从注册页面中删除了它。 I want this change to be reflected everywhere. 我希望这种变化能够在所有地方得到体现。 How do I implement this? 我该如何实施?

    <?php
    include('connection2.php');
    if(isset($_POST['project'])) {
           $pro=implode(',', $_POST['project']);

    $fname=$_POST['fname'];
    $lname=$_POST['lname'];
    $age=$_POST['age'];
    $gender=$_POST['gender'];
    $username=$_POST['username'];
    $username=$_POST['username'];
    $password=$_POST['password'];

    mysql_query("INSERT INTO test(fname, lname, age, gender, username, password,projects)VALUES
    ('$fname', '$lname', '$age', '$gender', '$username', AES_ENCRYPT
    ('$password','.b.'), '$projects')");
    }
    header("location: registration2.php?remark=success");
    mysql_close($con);
?> 

If you are using the same database, you can use the id of your project as foreign key. 如果使用相同的数据库,则可以将项目的ID用作外键。 It actually depends upon your database structure. 它实际上取决于您的数据库结构。 If you are storing as something like 如果您以类似的方式存储

user_1, project_1
user_1, project_2

In place of project_1 , you can use the id of your project table, and use cascade delete while deleting from your project table 您可以使用项目表的ID代替project_1,并在从project表中cascade delete时使用cascade delete

userId, userName, project_id

where project_id will be your foreign key. 其中project_id将是您的外键。

(You can also use trigger as suggested in the comment section. Although for this operation i will suggest cascade delete. If your operations are more complex then this, you can go for triggers or even stored procedure ) (您也可以按照注释部分中的建议使用触发器。尽管对于此操作,我建议级联删除。如果您的操作比较复杂,则可以使用triggers甚至stored procedure

This SO POST has more details on how cascade delete works SO POST详细介绍了级联删除的工作方式

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

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