简体   繁体   English

显示MySQL中特定行的数据

[英]Show data from a specific row in MySQL

I'm building a simple bug tracking tool. 我正在构建一个简单的错误跟踪工具。 When you create a new project, all the info you fill in in the form, gets stored in the database. 创建新项目时,您填写表格中的所有信息都将存储在数据库中。

When you create the new project you get redirected to a unique project page. 创建新项目时,您将被重定向到唯一的项目页面。 On top of the page it shows the name of the project, but it's not the name of the project I just created, it always shows the name of the first project in the MySQL table. 在页面顶部,它显示了项目的名称,但不是我刚刚创建的项目的名称,它始终显示MySQL表中第一个项目的名称。

How can I show the name of the project I just created? 如何显示刚创建的项目的名称?

With this query I retrieve the data from the database. 通过此查询,我从数据库中检索数据。

$query = "SELECT CONCAT(name) 
AS name FROM projects";
$result = @mysql_query ($query)

With this I show the project name, but it always shows the name of the first record in the table. 这样,我将显示项目名称,但它始终显示表中第一条记录的名称。

    <?php 
    if ($row = mysql_fetch_array ($result))
        echo '<h5>' . $row['name'] . '</h5>'; 
    ?>

It isn't yet SQL Injection prove and is far from complete... But I'm really struggling with this problem. 尚未得到SQL Injection的证明,还远远不够完成……但是我真的在这个问题上苦苦挣扎。

You need an AUTO_INCREMENT field on your table for a unique identifier (at least, you really should). 您需要在表上的AUTO_INCREMENT字段中获得唯一标识符(至少,您确实应该如此)。 Then you can do something like this: 然后,您可以执行以下操作:

<?php
$sql = new MySQLi('localhost', 'root', '', 'database');

$sql->query('INSERT INTO `projects` (`name`) VALUES ("Test Project");');
$projectID = $sql->insert_id; // Returns the auto_increment field value of the last insert query performed
// So this assumes you have a field in your table called "id" in this example

$res = $sql->query('SELECT CONCAT(`name`) AS `name` FROM `projects` WHERE `id` = '.$projectID.';');

if ($row = $res->fetch_assoc()) {
    echo '<h5>'.$row['name'].'</h5>';
}
?>

Since you were calling for a redirect to the unique project page, you should have something like this: header("Location: project.php?id=$projectID"); 由于您需要重定向到唯一的项目页面,因此应该具有以下内容: header("Location: project.php?id=$projectID");

Then, on project.php, you can attempt to fetch the project with the query above, only your query's WHERE clause should be something like: 然后,在project.php上,您可以尝试使用上述查询来获取项目,只有查询的WHERE子句应该类似于:

'`id` = '.intval($_GET['id']).';'

Technically, you could pass all the project info along to the next page as a request or a session cookie and save yourself a query altogether. 从技术上讲,您可以将所有项目信息作为请求或会话cookie传递到下一页,并完全保存查询。 Just make sure you keep the id handy so it's easy to update the record. 只需确保将id随身携带,即可轻松更新记录。

Try using ORDER BY . 尝试使用ORDER BY

$query = "SELECT CONCAT(name) 
AS name FROM projects ORDER BY id DESC";

This would show the most recent project (assuming you have an ID column). 这将显示最新的项目(假设您有一个ID列)。

However, a much better way is to have an ID variable on the page. 但是,更好的方法是在页面上具有ID变量。

$query = "SELECT CONCAT(name) 
AS name FROM projects WHERE id=?";

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

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