Dear friends, How can i display messages from mysql database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ashfaqbinnazar
Forum Newbie
Posts: 5
Joined: Thu Apr 29, 2010 1:02 am

Dear friends, How can i display messages from mysql database

Post by ashfaqbinnazar »

Dear friends,
How can i display messages from mysql database in each rows with delete and reply button.
Thanks in advance.
Here is my code.
i have created a page like scrapbook.
and splitted the scrapbook into two,ie my scrapbook and my friends scrapbook.
$toid is active only when i visit others profile.
if $toid is blank then display my scrqapbook otherwise my friend scrapbook.

Code: Select all

 <td><table width="100%" height="99" border="1">
          <?php 
               if($toid=="" or $toid==$id)
                {
                 $rst = mysql_query("SELECT * FROM messages where toid='$id' ORDER BY id DESC")
                 or die(mysql_error());  
                   $count=mysql_num_rows($rst);
                 while($count>0)
                 {
                 $count--;
                 $row = mysql_fetch_array($rst);
                  $delid=$row['id'];
                  $x=$row['msg'];
                   $from=$row['fromid'];
                   $row7=fromname($from);
                  $fromname=$row7['username'];
                  $row4=userdata($id);
                  ?>
                 <?php 
                   echo "<table width='100%' border='1'>
                   <tr>
                   <td width='10%'><a href='scrapbook.php?toid=$from'>$fromname</a></td> 
                   <td width='75%'>$x<br>
                   <input name='delete' type='submit' id='delete' value='Delete'>
                  <input name='reply' type='submit' id='reply' value='Reply'>
                   </td>
                   </tr>
                   </table>";
                   $delid=$row['id'];
                    $delete_query = "DELETE FROM messages WHERE id = '$delid'";
                     if($delete){
                     mysql_query($delete_query) or die(mysql_error());
                     }
                   $msg="Done";}} 
                 else
                 {
                 $id=$toid;
                 {
                 $rst3 = mysql_query("SELECT * FROM messages where toid='$id' ORDER BY id DESC")
                 or die(mysql_error());  
                   $count2=mysql_num_rows($rst3);
                 while($count2>0)
                 {
                 $count2--;
                 $row3 = mysql_fetch_array($rst3);
                $delid2=$row['id'];
                  $y=$row3['msg']; 
                  $from=$row3['fromid'];
                  $row6=fromname($from);
               $fromname=$row6['username'];
                  echo "<table width='100%' height='109' border='1'>
                  <tr valign='bottom'><td width='14%' height='103'>$fromname</td> 
                 <td valign='top' width='86%'>$y</td>
                   </tr></table>";
                  }}}?></table>
               </td>

User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Dear friends, How can i display messages from mysql data

Post by social_experiment »

Code: Select all

<?php if($toid=="" or $toid==$id) ?>
You should decide on a different way of discerning between your and your friend's scrapbook. If '$toid' is blank
you will not get any results from the database.

Code: Select all

<?php $delete_query = "DELETE FROM messages WHERE id = '$delid' ";
if ($delete) {
 mysql_query($delect_query) or die(mysql_error());
} ?>
What is the value of '$delete'? Rather check the query, already in 'mysql_query()'.

Code: Select all

<?php $delete_query = mysql_query("DELETE FROM messages WHERE id = '".mysql_real_escape_string($delid)."'");
if ($delete_query) { //success message }
else {
 //error message
} ?> 
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
rahulzatakia
Forum Commoner
Posts: 59
Joined: Fri Feb 05, 2010 12:01 am
Location: Ahmedabad

Re: Dear friends, How can i display messages from mysql data

Post by rahulzatakia »

Hi, try out the following code.. and give the response if it is working or not...

Code: Select all


<?php
if($_POST['delete'] != "")
{
	
	$delid=$_POST['delid'];
	$delete_query = "DELETE FROM messages WHERE id = '$delid'";
	if($delete){
	mysql_query($delete_query) or die(mysql_error());
}
?>
<td><table width="100%" height="99" border="1">
<form id="form1" name="form1" action="" method="post"> 
<?php
if($toid=="" or $toid==$id)
{
$rst = mysql_query("SELECT * FROM messages where toid='$id' ORDER BY id DESC")
or die(mysql_error()); 
$count=mysql_num_rows($rst);
while($count>0)
{
$count--;
$row = mysql_fetch_array($rst);
$delid=$row['id'];
$x=$row['msg'];
$from=$row['fromid'];
$row7=fromname($from);
$fromname=$row7['username'];
$row4=userdata($id);
?>
<?php
echo "<table width='100%' border='1'>
<tr>
<td width='10%'><a href='scrapbook.php?toid=$from'>$fromname</a></td>
<td width='75%'>$x<br>
<input name='delid' type='hidden' id='delid' value='".$delid."'>
<input name='delete' type='submit' id='delete' value='Delete'>
<input name='reply' type='submit' id='reply' value='Reply'>
</td>
</tr>
</table>";
}
$msg="Done";}}
else
{
$id=$toid;
{
$rst3 = mysql_query("SELECT * FROM messages where toid='$id' ORDER BY id DESC")
or die(mysql_error()); 
$count2=mysql_num_rows($rst3);
while($count2>0)
{
$count2--;
$row3 = mysql_fetch_array($rst3);
$delid2=$row['id'];
$y=$row3['msg'];
$from=$row3['fromid'];
$row6=fromname($from);
$fromname=$row6['username'];
echo "<table width='100%' height='109' border='1'>
<tr valign='bottom'><td width='14%' height='103'>$fromname</td>
<td valign='top' width='86%'>$y</td>
</tr></table>";
}}}?>
</form></table></td>

ashfaqbinnazar
Forum Newbie
Posts: 5
Joined: Thu Apr 29, 2010 1:02 am

Re: Dear friends, How can i display messages from mysql data

Post by ashfaqbinnazar »

Sorry,
the code is not working..
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Dear friends, How can i display messages from mysql data

Post by social_experiment »

Again, as with the original code example this

Code: Select all

<?php if($delete) ?>
will not work because what is '$delete'??
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
rahulzatakia
Forum Commoner
Posts: 59
Joined: Fri Feb 05, 2010 12:01 am
Location: Ahmedabad

Re: Dear friends, How can i display messages from mysql data

Post by rahulzatakia »

hi..
i have tried the code and its working fine with me.. so let me know what error you are getting...
Post Reply