简体   繁体   English

向表添加外键ID

[英]Add a foreign key id to a table

I'm building a simple bug tracker tool, but I have a problem. 我正在构建一个简单的错误跟踪器工具,但是有一个问题。

When you create a new project, you get redirected to the project page, there you can add a new bug to the project. 创建新项目时,将重定向到项目页面,您可以在其中向项目添加新的错误。 You have a 'projects' table and a 'bugs' table in the MySQL db (phpmyadmin). 在MySQL数据库(phpmyadmin)中,您有一个“项目”表和一个“错误”表。

The new bug will be added in the 'bugs' table, but how can I add the project id to the bugs table? 新的错误将添加到“错误”表中,但是如何将项目ID添加到错误表中?

I already added 'fk_project_id' as a foreign key to the 'bugs' table. 我已经将'fk_project_id'作为外键添加到'bugs'表中。

this is a code snippet from the project page (here, you select all the info from the project): 这是项目页面中的代码片段(在这里,您可以从项目中选择所有信息):

$id = $_GET['id'];
$con=mysqli_connect("localhost","root","","bugslap");
if (mysqli_connect_errno())  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM projects where projects_id = $id");
mysqli_close($con);`

and here you will be redirected to a form where you can add a new bug: 在这里,您将被重定向到可以添加新错误的表单:

<a href="newbug.php">add a new bug</a>

when you submit the form, it will run this script (bug.class.php): 提交表单时,它将运行以下脚本(bug.class.php):

$name        = $_POST['name'];
$descr       = $_POST['description'];   
$leader      = $_POST['leader'];    
$img         = $_POST['img'];

$sql="INSERT INTO bugs (name, description, leader, img, registration_date,fk_project_id)
VALUES ('$name', '$descr', '$leader', '$img', NOW())";
$result = mysql_query($sql); 

if($result){
header('Location: ../projectpage.php');
}
else {
echo "Oops, there is something wrong. Try again later.";
}

mysql_close();

Then you should be redirected to the project page, where the bug is added to the project. 然后应将您重定向到项目页面,在该页面中将错误添加到项目中。 I want to show all the bug info (description,image,...) on this project page. 我想在此项目页面上显示所有错误信息(描述,图像等)。

You have to provide the project id to the newbug.php page; 您必须在newbug.php页面上提供项目ID。 for example with a GET parameter. 例如使用GET参数。 Then you can easily fetch it from newbug.php and pass it on to your class. 然后,您可以轻松地从newbug.php获取它,并将其传递给您的类。

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

相关问题 Laravel将外键添加到表 - Laravel add foreign key to a table sql-是否可以将外键添加到引用同一表上的id colum的表 - sql - Is it possible to add foreign key to table which references to id colum on the same table 抓取当前登录用户的用户ID并将其添加为其他表中的外键值将返回null-MySQL-CodeIgniter - Grabbing user id of current logged user and add it as foreign key value in a different table returns null - MySQL - CodeIgniter 测试是否将特定ID用作任何外表条目中的外键 - test if a specific id is used as foreign key in any foreign table entry 使用外键id获取外表名 - Use foreign key id to get a foreign table name 如何将PK作为现有表的外键添加? - How to Add PK As Foreign Key To Existing Table? 如何在现有表中添加外键? - How to add a foreign key in an existing table? SQL:在Laravel中,修改表`familymembers`添加约束`familymembers_user_id_foreign`外键(`user_id`)引用`all_users`(`id`) - SQL: alter table `familymembers` add constraint `familymembers_user_id_foreign` foreign key (`user_id`) references `all_users` (`id`) in laravel 检索表的最后插入ID以在另一个表中用作外键 - retrieving last inserted ID of a table for using as Foreign key in another table 如何从另一个表主键添加mysql外键? - How to Add mysql foreign key from another table primary key?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM