$ row ['id']只能得到一个值

时间:2013-05-23 06:54:20

标签: php mysql database row record

我正在尝试获取数据库中的所有记录并且它已成功,但我只能获得1个id值。我需要在我的数据库中获取所有id并调用javascript函数。在我的代码中,我只能得到最后一条记录的id。我怎么能得到它们呢?

<?php 
$con = mysql_connect('localhost','root','')
or die(mysql_error());
mysql_select_db ("database_name");

$query = "SELECT * FROM news ORDER BY date DESC LIMIT 0 , 3";
$result = mysql_query($query);
echo "<table>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<th>" . $row['header'] . "</th>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['date'] . "</td>";
echo "</tr>";
echo "<tr>";
$position=0; 
$message= $row['desc'];
$post = substr($message,$position,300); 
echo "<td>" . $post . "...</td>";
echo "</tr>";
echo "<tr>";
$id = $row['id'];
echo "<tr><td>";

/*The start of chaos */
echo "<form action='' method='post'  onSubmit='ReadMore()'>";
for($ctr=1;$ctr<=3;$ctr++){
$id=$ctr;  //This should change and increment the value of id, isn't it?

echo "<input type='hidden' name='more' value=".$id." id='more'/>";
}
echo "<input type='submit' name='read' value='Read More'>
</form>
</td></tr>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?> 

这里发生的是它将显示来自数据库的记录:$ row ['header'],$ row ['date'],$ row ['desc']然后是一个循环中显示Read More的按钮。我想要发生的是在点击'read more'后调用一个函数,具体取决于id。

例如,

___________________________
| This is the header      |
| Date                    |
| Description             |
| Read More Button        | //this button should display $id = 3
___________________________
| This is the header      |
| Date                    |
| Description             |
| Read More Button        | //this button should display $id = 2
___________________________
| This is the header      |
| Date                    |
| Description             |
| Read More Button        | //this button should display $id = 1
___________________________

这可能吗?或者我错过了什么。我的代码只显示所有按钮的$ id = 1。对不起,我很抱歉。

1 个答案:

答案 0 :(得分:0)

you may try like this
make the id field name as array like more[] and bring the form out side loop as shown 
no need of for loop there to get the ids u already using while loop
bring submit button also outside loop

如果您想要显示该帖子的详细信息,那么为什么再次使用表单你可以简单地将其链接到详细信息页面n可以传递id而无需将其作为表单提交。

检查下面的代码

<?php 
$con = mysql_connect('localhost','root','')
or die(mysql_error());
mysql_select_db ("database_name");

$query = "SELECT * FROM news ORDER BY date DESC LIMIT 0 , 3";

$result = mysql_query($query);

//echo "<form action='' method='post'  onSubmit='ReadMore()'>";
echo "<table>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<th>" . $row['header'] . "</th>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['date'] . "</td>";
echo "</tr>";
echo "<tr>";
$position=0; 
$message= $row['desc'];
$post = substr($message,$position,300); 
echo "<td>" . $post . "...</td>";
echo "</tr>";
echo "<tr>";
$id = $row['id'];
echo "<tr><td>";

/*The start of chaos */

//for($ctr=1;$ctr<=3;$ctr++){
//$id=$ctr;  //This should change and increment the value of id, isn't it?

//echo "<input type='hidden' name='more[]' value=".$id." id='more[]'/>";
//}
 echo "<tr><td><a href='post_details.php?post_id=".$id."'>Read More</a></td></tr>";

</td></tr>";
echo "</tr>";
}

echo "</table>";
//echo "</form>";
mysql_close($con);
?>