Jump to content

guestbook code help


the-botman

Recommended Posts

You don't need a index in your form.

   while ($rows = mysql_fetch_array($SqlResult)); 

	   ?>
	   <td align="center" bgcolor="#FFFFFF"><input name="need_delete[]" type="checkbox" id="checkbox[<? echo $rows['Gbk_Id']; ?>]" value="<? echo $rows['Gbk_Id']; ?>"></td> 

         <?php

Should be fine.

 

Remove_Message() is returing a string of "False".  It also has no database interaction, just sets a string in the form of a query.

Link to comment
Share on other sites

can you please tell me where i am going wrong on the table? see it does not display corrctly it shows the first message 100% and allows me to tick and delete but the second  message is not in the table and even if i tick it and click delete it does not do anything, i think thats part of my problem, how do i make them all show in the table with just one delete button at the bottom, right now there is one delete button next to each message.

Link to comment
Share on other sites

can you please tell me where i am going wrong on the table? see it does not display corrctly it shows the first message 100% and allows me to tick and delete but the second  message is not in the table and even if i tick it and click delete it does not do anything, i think thats part of my problem, how do i make them all show in the table with just one delete button at the bottom, right now there is one delete button next to each message.

sorry this is my form

	   $Spacing = "					   ";
	   echo $Spacing.'<center>'."\n";
	   echo $Spacing.'<br>'."\n";
	   echo $Spacing.'<table border="0" cellpadding="0" cellspacing="0" style="color: #666;font-size:11px;width:550px;">'."\n";
	   echo $Spacing.'	   <tr height="15" align="left"><td><span class="Form_Title">Guestbook Messages</span><HR width="100%" SIZE="1"></td></tr>'."\n";
	   echo $Spacing.'</table>'."\n";
	   echo $Spacing.'<br>'."\n";
	   echo $Spacing.'<form name="form1" method="post" action="">'."\n";   
	   echo $Spacing.'<table border="0" cellpadding="0" cellspacing="1" style="font-size:9pt;width:450px;">'."\n";
		   echo $Spacing.'<tr>'."\n";
	   echo $Spacing.'<td>'."\n";
	   echo $Spacing.'<tr style="background-color:#666;color:#FFF;"><td style="background-color:#FFF;color:#FFF;"></td>'."\n"; 
	   echo '<td><b>Delete</b></td>'."\n"; 
	   echo '<td><b>  Date</b></td>'."\n"; 
	   echo '<td><b>  Time</b></td>'."\n"; 
       echo '<td align="center"><b>Message</b></td>'."\n";
	   echo '</tr>'."\n";
   
   if ($RowCnt > 0) {
   $i = 0;
   while ($i < $RowCnt) {
	   $Gbk_Id = mysql_result($SqlResult,$i,"Gbk_Id");
	   $Gbk_Date = mysql_result($SqlResult,$i,"Gbk_Date");
	   $Gbk_Time = mysql_result($SqlResult,$i,"Gbk_Time");
	   $Gbk_Name = mysql_result($SqlResult,$i,"Gbk_Name");
	   $Gbk_Message = mysql_result($SqlResult,$i,"Gbk_Message");

	  $Spacing = "						   ";

	   echo $Spacing.'<tr style="background-color:#F5F5F5;color:#666;">';
	   echo '<td></td>';
	   echo '<td><input name="need_delete[]" type="checkbox" id="checkbox'.$Gbk_Id.'" value="'.$Gbk_Id.'"></td>'."\n";		          
	   echo '<td style="width:80px;">  '.$Gbk_Date.'</td>';
	   echo '<td style="width:60px;">  '.$Gbk_Time.'</td>';
	   echo '<td>  <b>'.$Gbk_Name.'</b><br>  '.$Gbk_Message.'</td>';
           echo '<br></table>'."\n"; 
	   echo '<td><input name="delete" type="submit" id="delete" value="" class="Form_Btn_delete"></td>'."\n";  
           echo '</form>'."\n"; 
           echo '</td>'."\n"; 
           echo '</tr>'."\n"; 

	   $i++;

Link to comment
Share on other sites

ok after trying every different way i got this working :) i am posting it for two reasons, 1. so others can learn from it and 2. for you guys to tell me if there is any bugs i should fix

<?php
 include 'GbManager_Header.php'; 

$db_name="a4228367_bhawap"; // Database name 
$tbl_name="page_guestbook"; // Table name 
mysql_connect($GLOBALS["DbIP"].':'.$GLOBALS["DbPort"],$GLOBALS["DbUser"],$GLOBALS["DbPword"]); 
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
$Id = "page_guestbook";

  include 'GbManager_Footer.php';
if ($count > 0) {
$Spacing = "					 ";
 echo $Spacing.'<center>'."\n";
 echo $Spacing.'<br>'."\n";
 echo $Spacing.'<table border="0" cellpadding="0" cellspacing="0" style="color: #666;font-size:11px;width:550px;">'."\n";
 echo $Spacing.'	 <tr height="15" align="left"><td><span class="Form_Title">Guestbook Messages</span><HR width="100%" SIZE="1"></td></tr>'."\n";
 echo $Spacing.'</table>'."\n";

    echo '<form name="form1" method="post" action="Guestbook.php?Cmd=Sign&Post=True">';
echo '<br>';
echo '<table border="0" cellpadding="0" cellspacing="1" style="font-size:9pt;width:550px;">';
echo '<tr style="background-color:#666;color:#FFF;"><td style="background-color:#FFF;color:#FFF;"></td><td><b>  Delete</b></td><td><b>  Name</b></td><td><b>  Time</b></td><td><b>  Date</b></td><td align="center"><b>Messages</b></td></tr>';

while ($rows = mysql_fetch_array($result)): 

echo '<tr style="background-color:#F5F5F5;color:#666;">';
echo '<td style="background-color:#FFF;"></td>';

?>
    <td style="width:10px;"><input name="need_delete[<? echo $rows['Gbk_Id']; ?>]" type="checkbox" id="checkbox[<? echo $rows['Gbk_Id']; ?>]" value="<? echo $rows['Gbk_Id']; ?>"></td> 
    <td style="width:80px;"><? echo $rows['Gbk_Name']; ?></td> 
    <td style="width:60px;"><? echo htmlspecialchars($rows['Gbk_Time']); ?></td> 
    <td><? echo htmlspecialchars($rows['Gbk_Date']); ?></td>
<td><? echo htmlspecialchars($rows['Gbk_Message']); ?></td> 
<?php
    echo '</tr>'; 
endwhile;
    echo '<tr>'; 
echo '<td align="center" style="width:50px;">';
    echo '<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" color="#666666" type="submit" id="delete" value="" class="Form_Btn_delete" /></td></td>'; 
    echo '</tr>';
echo '</form>'; 
    echo '</td>'; 
    echo '</tr>'; 
echo '</table>';
echo '<br>';
echo '<br>';
echo '</center>';
  } else {
	  $Spacing = "						  "; 
	  echo '<tr><td></td><td><font face="Verdana" size="2"><b>No Entries Found.</b></font><br></td></tr>'."\n"; 
  }
  
     if(isset($_POST['need_delete'])) {
$i = 0; 
while(list($key, $val) = each($_POST['need_delete'])) {
    $sql = "DELETE FROM $tbl_name WHERE Gbk_Id='$val'"; 
    mysql_query($sql); 
  } 
  if($i > 0){ 
    echo '<meta http-equiv="refresh" content="0;URL=Guestbook.php?Cmd=Sign&Post=True">'; 
  }
}  
?> 


Link to comment
Share on other sites

while(list($key, $val) = each($_POST['need_delete'])) {

    $sql = "DELETE FROM $tbl_name WHERE Gbk_Id='$val'";

    mysql_query($sql);

  }

 

There are two DONTS in the above section of code

[*]DON'T use $_POST data directly in your queries, cleanse it first

[*]DON'T run queries in loops

See my reply (#3 in this thread) for a better solution

Link to comment
Share on other sites

function Remove_Message() {
 $Result = "False";
 while(list($key, $val) = each($_POST['need_delete'])) {
 $DbRes = MySqlCmd("DELETE FROM page_guestbook WHERE Gbk_Id='$val'");
 }
 if ($DbRes != "1") {
	 $Result = "Database Error.";
 }
 return $Result;
}

the above code works 100% to delete multiple rows from the database

how do i use this code instead?

 

$ids_to_delete = array_filter(array_map('intval', $_POST['need_delete']));   // clean up the post data

$sql = "DELETE FROM $tblname WHERE id IN (" . join(',', $ids_to_delete) . ")";

i tried it this way

$ids_to_delete = array_filter(array_map('intval', $_POST['need_delete']));   // clean up the post data

$DbRes = MySqlCmd("DELETE FROM page_guestbook WHERE id IN (" . join(',', $ids_to_delete) . ")");

but it does not work

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.