简体   繁体   English

按下链接时运行MySQL查询

[英]Run a MySQL query when a link get pressed

I have this PHP code that creates a table from my query. 我有此PHP代码,可从查询中创建表。 In this table, I would like to create links based on the output (path). 在此表中,我想基于输出(路径)创建链接。 When clicked on these, they would trigger another query to run. 单击这些时,它们将触发另一个查询运行。 See my code and further explanation below. 请参阅下面的代码和进一步说明。

The code looks like this: 代码如下:

<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "DB1");

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Attempt select query execution
$sql = "SELECT * FROM testDB";
if($result = mysqli_query($link, $sql)){
    if(mysqli_num_rows($result) > 0){
        echo "<table>";
            echo "<tr>";
                echo "<th>file</th>";
                echo "<th>path</th>";
                echo "<th>type</th>";
            echo "</tr>";
        while($row = mysqli_fetch_array($result)){
            echo "<tr>";
                echo "<td>" . $row['file'] . "</td>";
                echo "<td>" . $row['path'] . "</td>";
                echo "<td>" . $row['type'] . "</td>";
            echo "</tr>";
        }
        echo "</table>";
        // Free result set
        mysqli_free_result($result);
    } else{
        echo "No records matching your query were found.";
    }
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

// Close connection
mysqli_close($link);
?>

So when running this I get a table with output containing file, path, type. 因此,在运行此命令时,我得到一个表,该表的输出包含文件,路径,类型。 What I would like to do now is to make path as a link in the table and when clicked, this query should execute giving a new table: SELECT * FROM testDB WHERE path = 'PATH FROM THE LINK'. 我现在想做的是将路径作为表中的链接,当单击该查询时,应执行该查询以提供一个新表: SELECT * FROM testDB WHERE path = 'PATH FROM THE LINK'.

I have tried to implement this by using prepared statements, changing my code to: 我试图通过使用准备好的语句来实现此目的,将代码更改为:

if (isset($_GET["path"])) {
    $sql1 = "SELECT `file`, `path`, `type` FROM testDB WHERE path=?";
    $stmt = $link->prepare($sql1);
    $stmt->bind_param("s", $_GET["path"]);
    $result = $stmt->execute();
} else {
    $sql = "SELECT * FROM testDB";
    $result = $link->query($sql);
}
if ($result) {
    if ($result->num_rows) {

and then in the table add: 然后在表中添加:

echo "<td><a href='http://myurl.com/test.php?path=" . $row['path'] . "'>" . $row['path'] . "<a/></td>";

but clicking the link, which should execute SELECT file, path, type FROM testDB WHERE path=? 但是单击链接(应该执行SELECT file, path, type FROM testDB WHERE path=? always give "No records matching your query were found." 始终给出“未找到与您的查询匹配的记录”。 What could be the problem? 可能是什么问题呢? Also, please be aware that I am having backslashes in path, but this is getting escaped thanks to the prepared statement, right? 另外,请注意,我在路径中使用反斜线,但是由于准备好的语句,所以可以避免这种情况,对吗? Any help is appreciated! 任何帮助表示赞赏!

try using step by step 尝试逐步使用

 $stmt->execute();
 $result = $stmt->fetch(); // or fetchAll for multiple arrays
 $connection = null;

m getting using this so try it... 我正在使用它,所以请尝试...

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

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