简体   繁体   English

PHP、HTML 和 SQL:如何获取表行号并作为参数发送

[英]PHP, HTML and SQL: How to get table row number and send as parameter

I connect to my database and get the table.我连接到我的数据库并获取表。 I create an HTML table to list all of my corporations as well as an anchor tag for "Read" "Update" and "Delete" at the end of each row.我创建了一个 HTML 表来列出我所有的公司,并在每一行的末尾为“读取”、“更新”和“删除”添加一个锚标记。 If I click on "Read" next to one of the corporations, how can I get that specific corporation (or that specific row number) so I can pass it as a parameter.如果我单击其中一家公司旁边的“阅读”,我如何获得该特定公司(或该特定行号)以便我可以将其作为参数传递。 I want to display the information of that corporation on that row.我想在该行显示该公司的信息。 Thanks!谢谢! 在此处输入图片说明 ` `

    try {
    $sql = "SELECT corp FROM corps";
    $sql = $db->prepare($sql);
    $sql->execute();
    $corps = $sql->fetchAll(PDO::FETCH_ASSOC);

    if ($sql->rowCount() > 0) {
        $table = "<table>" . PHP_EOL;
        foreach ($corps as $corp) {
            $table .= "<tr><td>" . $corp['corp'] . "</td><td>" . '<a href="../Lab3">Read</a>' . "</td><td>" . '<a href="../Lab3">Update</a>' . "</td><td>" . '<a href="../Lab3">Delete</a>' . "</td></tr>";
        }
        $table .= "</table>" . PHP_EOL . ' <a href="../Lab3">Create</a>';

    } else {
        $table = "No Corporations.". PHP_EOL;
    }

} catch (PDOException $e){
    die("There was a problem getting the Corporations");
}`

Base yourself on the following code to produce the desired actions.根据以下代码生成所需的操作。

First check if a (GET) array is (not) empty and equal to an action, and use double quoted values (with escaped double quotes) to echo out the $corp variable(s) correctly.首先检查 (GET) 数组是否为(非)空且等于一个操作,并使用双引号值(带有转义双引号)正确地回显$corp变量。

Note that I added PHP_EOL at the end of $table .= "<tr><td>"... in order to produce clean HTML which helps to debug when viewing HTML source.请注意,我在$table .= "<tr><td>"...的末尾添加了PHP_EOL以生成干净的 HTML,这有助于在查看 HTML 源代码时进行调试。 It's just as good a debugging tool when it comes to something like this, believe me.相信我,当涉及到这样的事情时,它也是一个很好的调试工具。

Having code show up in one long line, is very hard to work with.将代码排成一长行,很难处理。

Side notes: I will leave the "Create" link at your discretion since that wasn't mentioned in the question and could be too broad a subject to cover.旁注:我将根据您的判断保留“创建”链接,因为问题中没有提到这一点,而且主题可能过于宽泛而无法涵盖。

I can also assume that your Lab3 (folder) is using an index file and is to be used to perform all of these actions.我还可以假设您的Lab3 (文件夹)正在使用索引文件,并将用于执行所有这些操作。

If ../Lab3?action does not work for you, then you may need to use something like如果../Lab3?action对您不起作用,那么您可能需要使用类似
../Lab3/index.php?action... or ../Lab3/your_file.php?action... instead. ../Lab3/index.php?action...../Lab3/your_file.php?action...代替。

$table = "<table>" . PHP_EOL;
foreach ($corps as $corp) {

    $table .= "<tr><td>" . $corp['corp'] . "</td><td>" . "<a href=\"../Lab3?action=read&corp=$corp\">Read</a>" . "</td><td>" . "<a href=\"../Lab3?action=update&corp=$corp\">Update</a>" . "</td><td>" . "<a href=\"../Lab3?action=delete&corp=$corp\">Delete</a>" . "</td></tr>" . PHP_EOL;
}
$table .= "</table>" . PHP_EOL . ' <a href="../Lab3">Create</a>';


if(!empty($_GET['action'])){

    if($_GET['action'] == 'read'){
        echo $read = "<br>Record to read: " . $_GET['corp'];
        // You can place your query here
    }

    if($_GET['action'] == 'update'){
        echo $update = "<br>Record to update: " . $_GET['corp'];
        // You can place your query here
    }

    if($_GET['action'] == 'delete'){
        echo $delete = "<br>Record to delete: " . $_GET['corp'];
        // You can place your query here
    }


}

However, and I must state that this is open to an SQL injection and you would need to use a prepared statement.但是,我必须声明这对 SQL 注入是开放的,您需要使用准备好的语句。 You're already using PDO, so that's a good start.您已经在使用 PDO,所以这是一个好的开始。

Note: Using $var = (int)$_GET['integer_array'];注意:使用$var = (int)$_GET['integer_array']; (as an example), also helps to prevent from an SQL injection, given if that value is an integer. (作为示例),如果该值是整数,也有助于防止 SQL 注入。 If not, then you can't use (int) against a string value.如果不是,则不能对字符串值使用(int)

References:参考:

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

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