简体   繁体   English

通过$ _GET传递外键

[英]Pass foreign key via $_GET

I have an online courses CRUD application. 我有一个在线课程CRUD应用程序。 It has, among other pages, an instructor BIO page. 除其他页面外,它还有一个教师BIO页面。

First, the instructors are added, in an users table, with basic data: first_name, last_name, and email; 首先,在用户表中向教师添加基本数据:first_name,last_name和email; then a BIO can be added, optionally, for any instructor. 然后可以选择为任何教师添加BIO。 There is a second database table, called "bios", to serve this purpose. 为了这个目的,有一个名为“bios”的第二个数据库表。

I need to pass $user_id into the courses table, (as foreign KEY) an for that purpose i use $_GET : 我需要将$user_id传递到课程表中,(作为外来KEY)为此我使用$ _GET

<?php 
    $user_id = $_GET['id'];
    if(isset($_POST['submit-btn'])) {
        $no_courses = $_POST['no_courses'];
        $years_exp = $_POST['years_exp'];
        $fav_lang = $_POST['fav_lang'];
        $courses = $_POST['courses'];

        $sql = "INSERT INTO courses (user_id, no_courses, years_exp, fav_lang, courses) VALUES ('$user_id', '$no_courses', '$years_exp', '$fav_lang', '$courses')";

        if (mysqli_query($con, $sql)) {
            echo("<p>Instructor bio was added.</p>");
        } else {
            echo "Error: " . mysqli_error($con);
        }
    }
?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="add_bio">
        ...
</form>

Using $_GET seems convenient because in a table with a lot of rows, containing instructors, on the right most cell/column, I have a set of link buttons for CRUD operations, "Add bio" being one of those buttons. 使用$_GET似乎很方便,因为在一个包含很多行的表中,包含教师,在最右边的单元格/列上,我有一组用于CRUD操作的链接按钮,“添加生物”是其中一个按钮。

<a title="Add bio" href="add_bio.php?id=<?php echo $arr['id']?>"><span class="glyphicon glyphicon-plus-sign"></span></a>` 

But instead of passing the $user_id variable so that the bio can be added, the server throws these errors: 但是,不是传递$user_id变量以便可以添加bio,服务器会抛出这些错误:

Notice: Undefined index: id in E:\\xampp\\htdocs\\courses\\add_bio.php on line 7 Error: Cannot add or update a child row: a foreign key constraint fails 注意:未定义的索引:第7行的E:\\ xampp \\ htdocs \\ courses \\ add_bio.php中的id错误:无法添加或更新子行:外键约束失败

How can I pass the user's id if I want to keep the CRUD links mentioned above? 如果我想保留上面提到的CRUD链接,我该如何传递用户的ID?

Thank you! 谢谢!

You may have some issues with the content being submitted. 您可能对提交的内容有一些问题。

A great troubleshooting approach would be to: print_r($_GET); 一个很棒的故障排除方法是:print_r($ _ GET); or print_r($_POST); 或print_r($ _ POST);

Another option is to also: echo print_r($_GET, true); 另一种选择是:echo print_r($ _ GET,true); The Second argument (True) tells the print_r function to return as a string instead of output to the browser. 第二个参数(True)告诉print_r函数以字符串形式返回而不是输出到浏览器。

Review that output and verify you are actually getting an "ID" value in your GET and POST variables. 查看该输出并验证您实际上在GET和POST变量中获得了“ID”值。

Also, some security concerns, any data you take in from GET or POST or REQUEST or COOKIE should be "cleaned" you can look at: mysqli_real_escape_string 此外,一些安全问题,您从GET或POST或REQUEST或COOKIE中获取的任何数据都应该“清理”,您可以查看:mysqli_real_escape_string

Additionally, Never use PHP_SELF. 此外,永远不要使用PHP_SELF。 In almost all cases, it can be manipulated to execute a Cross Site Scripting Attack, and that is bad news. 在几乎所有情况下,都可以操纵它来执行跨站点脚本攻击,这是个坏消息。 if you want to submit the form to the current script, you can leave the "action" attribute as an empty string. 如果要将表单提交到当前脚本,可以将“action”属性保留为空字符串。 action="" 行动=“”

Hope this helps!!! 希望这可以帮助!!!

表单在提交按钮正上方缺少此行:

<input type="hidden" name="id" value="<?php $user_id;?>">

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

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