单击按钮后显示数据库的不同行

时间:2012-11-13 08:52:58

标签: php mysql

我有一个清真寺数据库,包含以下字段:mosque_id,mosque_name,prefecture,address,postal_code,website,email1,phone1。我想做的是以一种形式显示第一座清真寺的信息(mosque_id = 1),当我点击“下一步”时,将显示第二座清真寺的信息(mosque_id = 2)。

到目前为止,我已经能够使用mosque_id选择整行并显示信息,但我一直在努力寻找切换到下一行的方法。我是PHP和MYSQL的新手,任何帮助都会受到赞赏。

<?
$mosque_id = 1;
$result = mysql_query("SELECT * FROM Mosque WHERE mosque_id='$mosque_id'");
$row = mysql_fetch_assoc($result);
?>

<form action="insert.php" method="post">
Mosque Name: <?php print('<input type="text" name="firstname" value="' . $row['mosque_name'] . '"/><br>')?>
Prefecture: <?php print('<input type="text" name="firstname" value="' . $row['prefecture'] . '"/><br>')?>
Address: <?php print('<input type="text" value="' . $row['address'] . '"/><br>')?>
Postal Code: <?php print('<input type="text" value="' . $row['postal_code'] . '"/><br>')?>
Website: <?php print('<input type="text" value="' . $row['website'] . '"/><br>')?>
Email 1: <?php print('<input type="text" value="' . $row['email1'] . '"/><br>')?>
Contact number 1: <?php print('<input type="text" value="' . $row['phone1'] . '"/><br>')?>

<input type="button" value="Next Mosque" onclick=""/>
</form>

3 个答案:

答案 0 :(得分:4)

    <?php
$mosque_id = (isset($_GET['mosque_id']) ? intval($_GET['mosque_id']) : 1); // suppose you know that '1' is the first id in your table
$result = mysql_query("SELECT * FROM Mosque WHERE mosque_id>='$mosque_id' limit 2");
$row = mysql_fetch_assoc($result); // this is the row you're gonna display in the form
$row2 = mysql_fetch_assoc($result); // this will tell us about the next row
?>
<form action="insert.php" method="post">
Mosque Name: <?php print('<input type="text" name="firstname" value="' . $row['mosque_name'] . '"/><br>')?>
Prefecture: <?php print('<input type="text" name="firstname" value="' . $row['prefecture'] . '"/><br>')?>
Address: <?php print('<input type="text" value="' . $row['address'] . '"/><br>')?>
Postal Code: <?php print('<input type="text" value="' . $row['postal_code'] . '"/><br>')?>
Website: <?php print('<input type="text" value="' . $row['website'] . '"/><br>')?>
Email 1: <?php print('<input type="text" value="' . $row['email1'] . '"/><br>')?>
Contact number 1: <?php print('<input type="text" value="' . $row['phone1'] . '"/><br>')?>

<input type="button" value="Next Mosque" onclick="window.location = 'path-to-your-page?mosque_id=<?php echo $row2['mosque_id'];"/>
</form>

答案 1 :(得分:1)

首先:请使用mysqli函数或PDO类,而不是mysql函数,不推荐使用它们。

其次:使用长PHP标签

但你走在正确的轨道上:

将第一行替换为:

$mosque_id = (isset($_GET['mosque_id']) ? intval($_GET['mosque_id']) : 1);

最后

<input type="button" value="Next Mosque" onclick="javascript: document.location = '?mosque_id=<?php echo ($mosque_id + 1);?>"/>

你还需要在查询后写一些检查,如果这样的id存在,实际上,如果你进一步发展,你还需要检查下一个mosque_id,因为如果你删除了一些行,id-s的顺序可能不正确比如1,2,3,4但是1,3,7,9 (提示:对于下一个id使用WHERE mosque_id&gt; current_id ORDER BY mosque_id在MySQL中使用DESC LIMIT 1)

答案 2 :(得分:1)

只有一个隐藏的输入保存ID,并在页面加载时递增。然后,当您提交时,ID也将被提交。

<?
if (isset($_POST['id']) {
   $id = $_POST['id'];
}
else {
    $id = 1;
}

$result = mysql_query("SELECT * FROM Mosque WHERE mosque_id='$id'");
...
?>

<form action="insert.php" method="post">
...

<input type='hidden' name='id' value='<?php echo $mosque_id++; ?> />
<input value="Next Mosque" />
</form>

顺便说一下,使用PDO。它非常容易学习,并为您的应用程序增加了很高的安全性。

相关问题