单击超链接时查看详细的MySQL结果(View detailed MySQL Result when Hyperlink is Clicked)

编程入门 行业动态 更新时间:2024-10-26 18:16:47
单击超链接时查看详细的MySQL结果(View detailed MySQL Result when Hyperlink is Clicked)

我正在努力实现以下目标。 在主页上,MySQL查询返回表“parcid”的结果(但结果有限)。 下一步是,我想超链接一个字段(最好是“ID”)以使用相同的ID打开一个新页面,但返回更详细的结果。 下面的代码可以工作,但很明显我错过了某些地方,因为它返回的是所有结果,而不仅仅是点击ID的结果。 任何帮助将不胜感激。

主页:“ http:// localhost / ”

Pdetail网址:“ http:// localhost / pdetail / ”

查询“主页”页面

<?php // set database server access variables: $host = "localhost"; $user = "root"; $pass = ""; $db = "wordpress"; // open connection $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); // select database mysql_select_db($db) or die ("Unable to select database!"); // create query $query = "SELECT * FROM parcid"; // execute query $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); // see if any rows were returned if (mysql_num_rows($result) > 0) { // yes // print them one after another echo "<table cellpadding=1 >"; echo "<tr>"; echo "<td>"."ID"."</td>"; echo "<td>"."City"."</td>"; echo "<td>"."Destination City"."</td>"; echo "<td>"."Weight"."</td>"; echo "<td>"."Length"."</td>"; echo "<td>"."Width"."</td>"; echo "<td>"."Height"."</td>"; echo "<td>"."Type"."</td>"; echo "<td>"."Courier Option"."</td>"; echo "</tr>"; while($row = mysql_fetch_row($result)) { echo "<tr>"; echo '<td><a href="http://localhost/pdetail/'.$row[0].'">'.$row[0].'</a></td>'; echo "<td>" . $row[6]."</td>"; echo "<td>" . $row[12]."</td>"; echo "<td>" . $row[14]."</td>"; echo "<td>" . $row[15]."</td>"; echo "<td>" . $row[16]."</td>"; echo "<td>" . $row[17]."</td>"; echo "<td>" . $row[18]."</td>"; echo "<td>" . $row[19]."</td>"; echo "<td>" . $row[20]."</td>"; echo "</tr>"; } echo "</table>"; } else { // no // print status message echo "No rows found!"; } // free result set memory mysql_free_result($result); // close connection mysql_close($connection); ?>

查询“pdetailed”页面。

<?php // set database server access variables: $host = "localhost"; $user = "root"; $pass = ""; $db = "wordpress"; // open connection $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); // select database mysql_select_db($db) or die ("Unable to select database!"); $ID = $_GET['ID']; // create query $query = "SELECT * FROM parcid WHERE ID LIKE '%$ID%'"; // execute query $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); // see if any rows were returned if (mysql_num_rows($result) > 0) { // yes // print them one after another echo "<table cellpadding=1 >"; echo "<tr>"; echo "<td>"."ID"."</td>"; echo "<td>"."CID"."</td>"; echo "<td>"."From"."</td>"; echo "<td>"."Street"."</td>"; echo "<td>"."Suburb"."</td>"; echo "<td>"."Post Code"."</td>"; echo "<td>"."City"."</td>"; echo "<td>"."Province"."</td>"; echo "<td>"."Receiver"."</td>"; echo "<td>"."Destination Street"."</td>"; echo "<td>"."Destination Suburb"."</td>"; echo "<td>"."Destination Postcode"."</td>"; echo "<td>"."Destination City"."</td>"; echo "<td>"."Destination Province"."</td>"; echo "<td>"."Weight"."</td>"; echo "<td>"."Length"."</td>"; echo "<td>"."Width"."</td>"; echo "<td>"."Height"."</td>"; echo "<td>"."Type"."</td>"; echo "<td>"."Courier Option"."</td>"; echo "</tr>"; while($row = mysql_fetch_row($result)) { echo "<tr>"; echo "<td>" . $row[0]."</td>"; echo "<td>" . $row[1]."</td>"; echo "<td>" . $row[2]."</td>"; echo "<td>" . $row[3]."</td>"; echo "<td>" . $row[4]."</td>"; echo "<td>" . $row[5]."</td>"; echo "<td>" . $row[6]."</td>"; echo "<td>" . $row[7]."</td>"; echo "<td>" . $row[8]."</td>"; echo "<td>" . $row[9]."</td>"; echo "<td>" . $row[10]."</td>"; echo "<td>" . $row[11]."</td>"; echo "<td>" . $row[12]."</td>"; echo "<td>" . $row[13]."</td>"; echo "<td>" . $row[14]."</td>"; echo "<td>" . $row[15]."</td>"; echo "<td>" . $row[16]."</td>"; echo "<td>" . $row[17]."</td>"; echo "<td>" . $row[18]."</td>"; echo "<td>" . $row[19]."</td>"; echo "<td>" . $row[20]."</td>"; echo "</tr>"; } echo "</table>"; } else { // no // print status message echo "No rows found!"; } // free result set memory mysql_free_result($result); // close connection mysql_close($connection); ?>

I'm trying to achieve the following. On the homepage a MySQL query returns results from table "parcid"(but limited results). The next step is, I would like to hyperlink a field (preferably "ID") to open a new page using the same ID but returning more detailed results. The code below works but obviously I'm missing something somewhere as it is returning all results rather than only results for the clicked on ID. Any help will be greatly appreciated.

Home url: "http://localhost/"

Pdetail url: "http://localhost/pdetail/"

Query on "Home" page

<?php // set database server access variables: $host = "localhost"; $user = "root"; $pass = ""; $db = "wordpress"; // open connection $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); // select database mysql_select_db($db) or die ("Unable to select database!"); // create query $query = "SELECT * FROM parcid"; // execute query $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); // see if any rows were returned if (mysql_num_rows($result) > 0) { // yes // print them one after another echo "<table cellpadding=1 >"; echo "<tr>"; echo "<td>"."ID"."</td>"; echo "<td>"."City"."</td>"; echo "<td>"."Destination City"."</td>"; echo "<td>"."Weight"."</td>"; echo "<td>"."Length"."</td>"; echo "<td>"."Width"."</td>"; echo "<td>"."Height"."</td>"; echo "<td>"."Type"."</td>"; echo "<td>"."Courier Option"."</td>"; echo "</tr>"; while($row = mysql_fetch_row($result)) { echo "<tr>"; echo '<td><a href="http://localhost/pdetail/'.$row[0].'">'.$row[0].'</a></td>'; echo "<td>" . $row[6]."</td>"; echo "<td>" . $row[12]."</td>"; echo "<td>" . $row[14]."</td>"; echo "<td>" . $row[15]."</td>"; echo "<td>" . $row[16]."</td>"; echo "<td>" . $row[17]."</td>"; echo "<td>" . $row[18]."</td>"; echo "<td>" . $row[19]."</td>"; echo "<td>" . $row[20]."</td>"; echo "</tr>"; } echo "</table>"; } else { // no // print status message echo "No rows found!"; } // free result set memory mysql_free_result($result); // close connection mysql_close($connection); ?>

Query on"pdetailed" page.

<?php // set database server access variables: $host = "localhost"; $user = "root"; $pass = ""; $db = "wordpress"; // open connection $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); // select database mysql_select_db($db) or die ("Unable to select database!"); $ID = $_GET['ID']; // create query $query = "SELECT * FROM parcid WHERE ID LIKE '%$ID%'"; // execute query $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); // see if any rows were returned if (mysql_num_rows($result) > 0) { // yes // print them one after another echo "<table cellpadding=1 >"; echo "<tr>"; echo "<td>"."ID"."</td>"; echo "<td>"."CID"."</td>"; echo "<td>"."From"."</td>"; echo "<td>"."Street"."</td>"; echo "<td>"."Suburb"."</td>"; echo "<td>"."Post Code"."</td>"; echo "<td>"."City"."</td>"; echo "<td>"."Province"."</td>"; echo "<td>"."Receiver"."</td>"; echo "<td>"."Destination Street"."</td>"; echo "<td>"."Destination Suburb"."</td>"; echo "<td>"."Destination Postcode"."</td>"; echo "<td>"."Destination City"."</td>"; echo "<td>"."Destination Province"."</td>"; echo "<td>"."Weight"."</td>"; echo "<td>"."Length"."</td>"; echo "<td>"."Width"."</td>"; echo "<td>"."Height"."</td>"; echo "<td>"."Type"."</td>"; echo "<td>"."Courier Option"."</td>"; echo "</tr>"; while($row = mysql_fetch_row($result)) { echo "<tr>"; echo "<td>" . $row[0]."</td>"; echo "<td>" . $row[1]."</td>"; echo "<td>" . $row[2]."</td>"; echo "<td>" . $row[3]."</td>"; echo "<td>" . $row[4]."</td>"; echo "<td>" . $row[5]."</td>"; echo "<td>" . $row[6]."</td>"; echo "<td>" . $row[7]."</td>"; echo "<td>" . $row[8]."</td>"; echo "<td>" . $row[9]."</td>"; echo "<td>" . $row[10]."</td>"; echo "<td>" . $row[11]."</td>"; echo "<td>" . $row[12]."</td>"; echo "<td>" . $row[13]."</td>"; echo "<td>" . $row[14]."</td>"; echo "<td>" . $row[15]."</td>"; echo "<td>" . $row[16]."</td>"; echo "<td>" . $row[17]."</td>"; echo "<td>" . $row[18]."</td>"; echo "<td>" . $row[19]."</td>"; echo "<td>" . $row[20]."</td>"; echo "</tr>"; } echo "</table>"; } else { // no // print status message echo "No rows found!"; } // free result set memory mysql_free_result($result); // close connection mysql_close($connection); ?>

最满意答案

如果您只想要$ ID标识的单行,那么查询就是

$query = "SELECT * FROM parcid WHERE ID = '$ID'";

此外,当您知道应该只从查询中接收单个结果行时,您不需要在while循环中运行mysql_fetch_row($result) 。

请不要使用mysql_数据库扩展 ,它已被弃用(在PHP7中永远消失)特别是如果你只是学习PHP,花费精力学习PDO或mysqli_数据库扩展, 这里有一些帮助来决定使用哪个

除非您使用框架,否则此声明可能是错误的

echo '<td><a href="http://localhost/pdetail/'.$row[0].'">'.$row[0].'</a></td>';

也许它应该是

echo '<td><a href="http://localhost/pdetail?ID='.$row[0].'">'.$row[0].'</a></td>';

If you want just the single row identified by $ID then the query is

$query = "SELECT * FROM parcid WHERE ID = '$ID'";

Also when you know you should only receive a single result row from a query, you do not need to run the mysql_fetch_row($result) in a while loop.

Please dont use the mysql_ database extension, it is deprecated (gone for ever in PHP7) Especially if you are just learning PHP, spend your energies learning the PDO or mysqli_ database extensions, and here is some help to decide which to use

Unless you are using a framework this statement might be wrong

echo '<td><a href="http://localhost/pdetail/'.$row[0].'">'.$row[0].'</a></td>';

Maybe it should be

echo '<td><a href="http://localhost/pdetail?ID='.$row[0].'">'.$row[0].'</a></td>';

更多推荐

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

发布评论

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

>www.elefans.com

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