重复最后的'id'细节

时间:2016-01-03 09:19:01

标签: php

我创建了一个用户可以提交建议的页面,但它只显示所有ID中上次提交的ID的详细信息。这是我的意思的图像:

enter image description here

这是我的代码:

<div align="center">
<?php   
include 'connect.php';
if(isset($_POST['submit'])){
    $title = $_POST['title'];
    $name = $_POST['name'];
    $post = $_POST['post'];

    if(empty($title) or empty($name) or empty($post)){
        $message = "Please fill in all fields";
    } else {
        mysql_query("INSERT INTO suggest (title, name, post) VALUES('".$title."', '".$name."', '".$post."')");
        $message = "SUGGESTION SUBMITTED!!!";
    }

    echo"$message";
}
?>
<form method="post" action="suggest.php">
<table width="80%">
<tr>
<td><b>Name:</b></td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td><b>Title:</b></td>
<td><input type="text" name="title" /></td>
</tr>
<tr>
<td><b>Your<br>Suggestion:</b></td>
<td><textarea name='post' rows='10' cols='40'></textarea></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="SUBMIT"/></td>
</tr>
</table>
</form>
<hr width="70%">
<?php

function getuser($id, $field) {
    $query = mysql_query("SELECT * FROM suggest ORDER BY id DESC");
    $run = mysql_fetch_array($query);
    return $run[$field];
}

$readq = mysql_query("SELECT id FROM suggest");
while($run_p = mysql_fetch_array($readq)){
    $id = $run_p['id'];
    $name = getuser($id, 'name');
    $title = getuser($id, 'title');
    $post = getuser($id, 'post');

?>
<table width="60%">
<tr>
<td><b><font color="blue"><?php echo $title; ?></font><br><br><?php echo $post; ?><br><font color="red">Suggestion From:</font> <font color="blue"><?php echo $name; ?></font></b><hr width="50%"></td>
</tr>
</table>
<?php
}
?>
</div>

我希望每篇文章都能展示自己的细节。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

您没有在函数中通过ID查询名称,因此重复了第一行..

//Wrong
function getuser($id, $field) {
    $query = mysql_query("SELECT * FROM suggest ORDER BY id DESC");
    $run = mysql_fetch_array($query);
    return $run[$field];
}

//Should be
function getuser($id, $field) {
    $query = mysql_query("SELECT * FROM suggest where id = '".$id."'");
    $run = mysql_fetch_array($query);
    return $run[$field];
}