如何从MySQL表中的特定列中获取值,然后将其显示在新页面上[duplicate](How to get values from specific column in MySQL table, the

编程入门 行业动态 更新时间:2024-10-27 06:22:17
如何从MySQL表中的特定列中获取值,然后将其显示在新页面上[duplicate](How to get values from specific column in MySQL table, then display it on new page [duplicate])

这个问题在这里已经有了答案:

PHP和MySQL选择一个值 8个答案

所以我有一个简单的问题,我坚持下去。 我必须使用与提供的代码相同的逻辑,但是这次我必须创建一个新页面 - 让我们说"article.php" ,然后我必须使用"Show"按钮来更改"Delete" "Show"按钮。

这很容易,但我不能做以下事情:

当用户点击“SHOW”按钮时,链接必须将其重定向到“article.php”页面,然后显示所选COLUMN的所有VALUES值。 这将如何发生? 再次使用"foreach"循环或?...我真的不知道该怎么做。 任何帮助将不胜感激!

<?php include "app".DIRECTORY_SEPARATOR."config.php"; include "app".DIRECTORY_SEPARATOR."db-connection.php"; $foo_connection = db_connect($host, $user_name, $user_password, $database); $sql = "SELECT * FROM articles"; $result = mysqli_query($foo_connection, $sql); if(mysqli_num_rows($result) > 0){ print "<table> <tr> <th>Article ID</th> <th>Article heading</th> <th>Article text</th> <th>Article author</th> <th>Article published</th> <th>Article delete</th> </tr>"; foreach($result as $key => $cols){ print "<tr>". "<td>".$cols['id']."</td>". "<td>".$cols['heading']."</td>". "<td>".$cols['text']."</td>". "<td>".$cols['author']."</td>". "<td>".$cols['published']."</td>". "<td><a href='app/sql/delete.php?id=".$cols['id']."'>Delete</a></td>". "</tr>"; } print "</table>"; } else { print "0 results"; } mysqli_close($foo_connection);

This question already has an answer here:

PHP and MySQL Select a Single Value 9 answers

So I have a simple question and I'm stuck with it. I have to use the same logic like in the provided code, but this time I have to make a new page - let's say "article.php", then I have to change the "Delete" button with "Show" button.

This is easy, but I can't do the following thing:

When a user clicks on the "SHOW" button, the link must redirect him into the "article.php" page, and after that to display ALL of the VALUES for the selected COLUMN. How is this going to happen? Using "foreach" cycle again or?... I really don't know how to do it. Any help will be highly appreciated!

<?php include "app".DIRECTORY_SEPARATOR."config.php"; include "app".DIRECTORY_SEPARATOR."db-connection.php"; $foo_connection = db_connect($host, $user_name, $user_password, $database); $sql = "SELECT * FROM articles"; $result = mysqli_query($foo_connection, $sql); if(mysqli_num_rows($result) > 0){ print "<table> <tr> <th>Article ID</th> <th>Article heading</th> <th>Article text</th> <th>Article author</th> <th>Article published</th> <th>Article delete</th> </tr>"; foreach($result as $key => $cols){ print "<tr>". "<td>".$cols['id']."</td>". "<td>".$cols['heading']."</td>". "<td>".$cols['text']."</td>". "<td>".$cols['author']."</td>". "<td>".$cols['published']."</td>". "<td><a href='app/sql/delete.php?id=".$cols['id']."'>Delete</a></td>". "</tr>"; } print "</table>"; } else { print "0 results"; } mysqli_close($foo_connection);

最满意答案

在新页面中,从以下链接更改链接:

<a href='app/sql/delete.php?id=".$cols['id']."'>Delete</a>

至:

<a href='app/sql/articledetails.php?id=".$cols['id']."'>Show</a>

然后创建一个页面articledetails.php ,该页面显示基于id传递的参数的单个记录。

使用只选择一行的SQL语句:

$sql = "SELECT * FROM articlesWHERE id = ?";

然后将$_GET['id']值绑定到SQL语句并执行它。 然后根据需要显示该行。

In the new page, change the link from:

<a href='app/sql/delete.php?id=".$cols['id']."'>Delete</a>

To:

<a href='app/sql/articledetails.php?id=".$cols['id']."'>Show</a>

Then create a page articledetails.php that displays a single record based on the argument passed in id.

Use a SQL statement that selects just the one row:

$sql = "SELECT * FROM articlesWHERE id = ?";

Then bind the $_GET['id'] value to the SQL statement and execute it. Then display the row as needed.

更多推荐

本文发布于:2023-08-01 17:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1362245.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:将其   新页面   values   duplicate   MySQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!