按标识

时间:2015-07-27 08:10:53

标签: php mysql database

我有一个包含id,name,address,sector,financiar,link的表 在链接上,当我按下它时,我想向我显示所选行ID的2个表格,例如:id 1。 http://postimg.org/image/khelg1m0z/

结果:http://s28.postimg.org/srvcwj065/Capture2.jpg

现在它是一个带有搜索子句的静态页面,其中id为1,但我需要在每行上按id显示自动链接。



<?php
include "connect.php";
$sql = "select * from studenti where id='1'";


$query = mysql_query($sql) or die (mysql_error());

?>

<table width="70%" cellpadding="5" cellspace="5">
<tr><td>Id</td>
<td>Nume</td>
<td>Localitate</td>
<td>Judet</td>
<td>Sector Financiar</td>
<td>Link</td></tr>

<?php while ($row = mysql_fetch_array($query)) { ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['nume']; ?></td>
<td><?php echo $row['localitate']; ?></td>
<td><?php echo $row['judet']; ?></td>
<td><?php echo $row['sector_financiar']; ?></td>
<td><?php echo $row['link']; ?></td>



<?php } ?>
</table>

<?php
include "connect.php";
$sql1 = "select * from certificari where id='1' ";


$query = mysql_query($sql1) or die (mysql_error());

?>

<table width="70%" cellpadding="5" cellspace="5">
<tr><td>Id</td>
<td>Denumire certificare</td>
<td>Serie si numar certificare</td>
<td>Data certificarii</td>
<td>Valabilitate certificare</td>
<td>Sector Financiar</td></tr>
<?php while ($row = mysql_fetch_array($query)) { ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['nume']; ?></td>
<td><?php echo $row['serie_numar']; ?></td>
<td><?php echo $row['data']; ?></td>
<td><?php echo $row['valabilitate']; ?></td>
<td><?php echo $row['sector_financiar']; ?></td>


<?php } ?>
</table>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您不应使用mysql_函数,因为它们已被删除,并且非常容易受到SQL injection attacks的攻击。相反,我将在此答案中使用MySQLi,但如果您愿意,也可以使用PDO。

要根据用户选择的内容显示不同ID的记录,您可以在页面URL中传递ID。当您链接到该页面时,您将所需的ID添加到地址中,如下所示:

<a href="page.php?id=4">Link</a>

变量id的值现在page.php中的$_GET['id']可用。

您页面中的实际PHP看起来就像下面的代码。为简洁起见,我遗漏了一些HTML,但您可以将其添加到int。

//Connect to the DB. Might want to put this in your connect.php and include it.
$db = new mysqli('localhost', 'user', 'pass', 'db');

//Prepare the statement. The ? will be your ID.
$statment = $db->prepare("SELECT * FROM studenti WHERE id = ?");

//Bind the parameters, so that the first (and only) question mark is turned into your ID.
//The 'i' means integer, if you store the id as a string your should use 's' instead.
$statement->bind_param('i', $_GET['id']);

//Execute the query.
$statement->execute();

//Get the results.
$result = $statement->get_result();

//Iterate over it to create the output.
while ($row = $result->fetch_assoc()) {

    ?>
    <tr>
         <td><?php echo $row['id']; ?></td>
         <td><?php echo $row['nume']; ?></td>
         <td><?php echo $row['localitate']; ?></td>
         <td><?php echo $row['judet']; ?></td>
         <td><?php echo $row['sector_financiar']; ?></td>
         <td><?php echo $row['link']; ?></td>
    </tr>
    <?

}

//Then do the same thing for the table certificari.
//Note that you only need to connect to the DB once.

如果您需要更多指导,这对MySQLi beginners guide非常有帮助。