I made a page where users can submit suggestions, but it only shows the details of the last submitted id in all ids. Here is an image of what I mean:
Here is my code:
<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>
I want every post to show its own details. How can I achieve this?
You are not querying the name by ID in your function hence its repeating the top row..
//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];
}
</div>