[英]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.