ted_chou12 Posted January 5, 2007 Share Posted January 5, 2007 I couldnt find the last post, so I reposted this again:this is my query:[code]<form name="deletepm" action="" method="post"><?php$sql = "SELECT * FROM privatemessages WHERE type='outbox' AND username='$username'";$res = mysql_query($sql) or die (mysql_error());while ($r = mysql_fetch_array($res)){echo "<tr>\n";echo "<td align=center><img src=\"images/". $r['entryicon'] .".gif\"></td>\n";echo "<td align=center><input type=checkbox name=id value=\"". $r['id'] ."\"></td>\n";//this is the check boxecho "<td>". $user ."</td>\n";echo "<td>". $subject ."</td>\n";echo "<td>". $date ."</td>\n";echo "</tr>\n";}?><input class="button" name="delete" type="submit" value="Delete Entry" onClick="javascript: return checkDelete()" /></form>[/code]If submit button is deleted, this action below will take place:[code]<?php if(isset($_POST['delete'])){$id = $_POST['id'];require("../mysqlconnection.php");$delete = mysql_query("DELETE FROM privatemessages WHERE id='$id' AND username='$username'");if($delete === false){echo"<p><b>Sorry, the database is temporary down, please come back later!</b></p>";}else{header("location: confirm.php?confirm=pm_delete");}}?>[/code]However, checkboxes can be multiple selected, that is be selected for at least one. But my code does not run for however much that are selected, I dont know if looping the action will be a nice idea, btw, I posted this before, someone did helped me, but I still couldnt get this rightThanksTed Quote Link to comment Share on other sites More sharing options...
matto Posted January 5, 2007 Share Posted January 5, 2007 Change this line[code]echo "<td align=center><input type=checkbox name=id value=\"". $r['id'] ."\"></td>\n";[/code]to this[code]echo "<td align=center><input type=checkbox name=id[$counter] value=\"". $r['id'] ."\"></td>\n";[/code]add this above your while ($r...[code]$counter=0;[/code]add this above your end php tag in your select query[code]$counter++;[/code]You will then need to add something like this to your delete action:[code]for(i=0;$>$amount_of_check_boxes; $i++) { if(isset($_POST['id'][$i]) { mysql_query("DELETE FROM privatemessages WHERE id='" . $_POST['$id'][$i] . "' AND username='$username'"); }}[/code]should give you a rough idea..... ;) Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted January 5, 2007 Author Share Posted January 5, 2007 hey, thanks. ;Dbut it appears:Parse error: syntax error, unexpected '=', expecting ';' in ../inbox.php on line 62which is this line:for(i=0;$>$amount_of_check_boxes; $i++) {I dont see why...Ted Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted January 5, 2007 Share Posted January 5, 2007 did you meanfor([b]$[/b]i=0;$[b]i[/b]>$amount_of_check_boxes; $i++) {emphasis mine of course :DI haven't read the rest of your code so I don't know if this will fix any problems that you are having other than the one you just said Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted January 5, 2007 Author Share Posted January 5, 2007 nope doesnt work :'( it doesnt delete at all.This is how i put it together:<?php for($i=0;$i>$amount_of_check_boxes; $i++) {if(isset($_POST['delete'][$i])){$id = $_POST['id'];while ($id != ""){require("../mysqlconnection.php");$delete = mysql_query("DELETE FROM privatemessages WHERE id='" . $_POST['$id'][$i] . "' AND username='$username'");if($delete === false){echo"<p><b>Sorry, the database is temporary down, please come back later!</b></p>";}else{header("location: confirm.php?confirm=pm_delete");}}}}?>But ... :'( Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted January 5, 2007 Share Posted January 5, 2007 let me toss yah a chunk of code before I head off to bed...form.. edited a little[code=php:0]<form name="deletepm" action="" method="post"><?php$sql = "SELECT * FROM privatemessages WHERE type='outbox' AND username='$username'";$res = mysql_query($sql) or die (mysql_error());while ($r = mysql_fetch_array($res)){echo "<tr>\n";echo "<td align=center><img src=\"images/". $r['entryicon'] .".gif\"></td>\n";echo "<td align=center><input type='checkbox' name='id[]' value=\"". $r['id'] ."\"></td>\n";//this is the check boxecho "<td>". $user ."</td>\n";echo "<td>". $subject ."</td>\n";echo "<td>". $date ."</td>\n";echo "</tr>\n";}?><input class="button" name="delete" type="submit" value="Delete Entry" onClick="javascript: return checkDelete()" /></form>[/code]code... editted a lot[code=php:0]<?php if(isset($_POST['delete'])){ require("../mysqlconnection.php"); foreach($_POST['id'] as $id) { $delete = mysql_query("DELETE FROM privatemessages WHERE id='$id' AND username='$username'") or die('<p><b>Sorry, the database is temporary down, please come back later!</b></p>'); }}header("location: confirm.php?confirm=pm_delete");?>[/code]give that one a try :P I'd really suggested filtering $id though... something like this in the loop...[code=php:0]if(!is_numeric($id)) { die('Hack Attempt');}[/code]gl! Quote Link to comment Share on other sites More sharing options...
matto Posted January 5, 2007 Share Posted January 5, 2007 Just a few things:The variable $amount_of_check_boxes was simply an example, this could be a hidden form value that contains the number of tickboxes you have.eg: (this would need to go below your while loop)[code]<input type="hidden" name="amount_of_check_boxes" value="<?=$counter?>">[/code]This line:[code]for($i=0;$i>$amount_of_check_boxes; $i++) {[/code]would actually become:[code]for($i=0;$i<$_POST['amount_of_check_boxes']; $i++) {[/code]This line:[code]if(isset($_POST['delete'][$i]) {[/code]would actually become:[code]if(isset($_POST['id'][$i]) {[/code]and would be nested in your original[code]//if the form has been submittedif(isset($_POST['delete'])) { //if one of the checkboxes has been ticked if(isset($_POST['id'][$i]) { ... [/code]looks like genericnumber1 has covered it also..... Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted January 5, 2007 Author Share Posted January 5, 2007 thanks, but i dont understand, if input type is hidden, how are you suppose to get which ones to delete or not?Ted Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted January 5, 2007 Author Share Posted January 5, 2007 oh, I understand why now, but it still wouldnt work, and there seems to be some text boxes above, i dont know why, but they are appearing numbers....this is what I have now, i dont know if I had put it together correctly: [code]<?php for($i=0;$i<$_POST['amount_of_check_boxes']; $i++) { require("../mysqlconnection.php"); if(isset($_POST['delete'])) { if(isset($_POST['delete'])){ require("../mysqlconnection.php"); foreach($_POST['id'] as $id) { $delete = mysql_query("DELETE FROM privatemessages WHERE id='$id' AND username='$username'") or die('<p><b>Sorry, the database is temporary down, please come back later!</b></p>'); }}}}?>[/code][code]while ($r = mysql_fetch_array($res)){ // Echo out the records with wordwrap, remove if you like echo "<tr>\n"; echo "<td align=center><img src=\"images/". $r['entryicon'] .".gif\"></td>\n"; echo "<td align=center>><input type='checkbox' name='id[]' value=\"". $r['id'] ."\"></td>\n"; echo "<td>". $user ."</td>\n"; echo "<td>". $subject ."</td>\n"; echo "<td>". $date ."</td>\n"; echo "</tr>\n"; $counter++;?> <input type=\"hidden\" name=\"amount_of_check_boxes\" value=\"<?=$counter?>\"><?}[/code] Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted January 5, 2007 Share Posted January 5, 2007 uhh actually what I posted was meant to be the only thing on your page :P[code=php:0]<?php if(isset($_POST['delete'])){ require("../mysqlconnection.php"); foreach($_POST['id'] as $id) { mysql_query("DELETE FROM privatemessages WHERE id='$id' AND username='$username'") or die('<p><b>Sorry, the database is temporary down, please come back later!</b></p>'); }}header("location: confirm.php?confirm=pm_delete");?>[/code]and the script should work fine ;) Off to bed I go! good luck Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted January 5, 2007 Author Share Posted January 5, 2007 nope, sorry, still doesnt work, i am getting crazy...can anyone help me out with this please?ThanksTed Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted January 5, 2007 Author Share Posted January 5, 2007 can anyone help me with this please???ThanksTed Quote Link to comment Share on other sites More sharing options...
matto Posted January 6, 2007 Share Posted January 6, 2007 any chance you can post what you have now? Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted January 6, 2007 Author Share Posted January 6, 2007 [code]<?php if(isset($_POST['delete'])){require("../mysqlconnection.php");foreach($_POST['id'] as $id) {mysql_query("DELETE FROM privatemessages WHERE id='$id' AND username='$username'") or die('<p><b>Sorry, the database is temporary down, please come back later!</b></p>');}header("location: confirm.php?confirm=pm_delete");}//form starts here, since genericnumber1 didnt mention about the checkbox name, so I used the original.$counter=0;while ($r = mysql_fetch_array($res)){echo "<tr>\n";echo "<td align=center><img src=\"images/". $r['entryicon'] .".gif\"></td>\n";echo "<td align=center><input type=checkbox name=id[$counter] value=\"". $r['id'] ."\"></td>\n";echo "<td>". $user ."</td>\n";echo "<td>". $subject ."</td>\n";echo "<td>". $date ."</td>\n";echo "</tr>\n";$counter++;}[/code] Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted January 7, 2007 Author Share Posted January 7, 2007 can anyone help me with this please?Thanks Quote Link to comment Share on other sites More sharing options...
matto Posted January 7, 2007 Share Posted January 7, 2007 what's happening when you run this now?can you also post your html form..... Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted January 7, 2007 Author Share Posted January 7, 2007 The problem is it doesnt delete at all. Maybe instead of starting with harder form, we should start with something easier, like:[code]A<input type=checkbox name=delete value=\"delete1\">B<input type=checkbox name=delete value=\"delete2\">C<input type=checkbox name=delete value=\"delete3\">[/code]how would you loop something like this?Thanks Quote Link to comment Share on other sites More sharing options...
matto Posted January 7, 2007 Share Posted January 7, 2007 if you post everything you have now I will help you get this working.... Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted January 7, 2007 Author Share Posted January 7, 2007 This is everything on my page:[code] <h3>My Inbox</h3> <script language="JavaScript"> <!-- Modified By: Steve Robison, Jr. (stevejr@ce.net) --> <!-- Begin var checkflag = "false"; function check(field) { if (checkflag == "false") { for (i = 0; i < field.length; i++) { field[i].checked = true;} checkflag = "true"; return "Uncheck All"; } else { for (i = 0; i < field.length; i++) { field[i].checked = false; } checkflag = "false"; return "Check All"; } } // End --> </script> <?php if(isset($_POST['delete'])){ require("../mysqlconnection.php"); foreach($_POST['id'] as $id) { mysql_query("DELETE FROM privatemessages WHERE id='$id' AND username='$username'") or die('<p><b>Sorry, the database is temporary down, please come back later!</b></p>'); } header("location: confirm.php?confirm=pm_delete");} ?> <form name="deletepm" action="" method="post"> <?php //order sort $order = $_GET['order']; if ($order == "senderasc") {$orderby = "ORDER BY username"; $menu1 = "v";} elseif ($order == "senderdes") {$orderby = "ORDER BY username DESC"; $menu1 = "^";} elseif ($order == "subjectasc") {$orderby = "ORDER BY entrytitle"; $menu2 = "v";} elseif ($order == "subjectdes") {$orderby = "ORDER BY entrytitle DESC"; $menu2 = "^";} elseif ($order == "dateasc") {$orderby = "ORDER BY entrydate"; $menu3 = "v";} else {$order = "datedes";$orderby = "ORDER BY entrydate DESC"; $menu3 = "^";} if ($order == "senderasc") {$ordersendermenu = "senderdes";} else {$ordersendermenu = "senderasc";} if ($order == "subjectasc") {$ordersubjectmenu = "subjectdes";} else {$ordersubjectmenu = "subjectasc";} if ($order == "dateasc") {$orderdatemenu = "datedes";} else {$orderdatemenu = "dateasc";} // connect to mysql below require("../mysqlconnection.php"); // Set how many rows to display on each page $limit = 20; // Query the database to get the total number of rows $query_count = "SELECT * FROM privatemessages WHERE username='$username' AND type='inbox'"; $result_count = mysql_query($query_count) or die (mysql_error()); $totalrows = mysql_num_rows($result_count); if(isset($_GET['page'])){ // Checks if the $page variable is empty (not set) $page = $_GET['page']; // If it is empty, we're on page 1 } else { $page = 1; } // Set the start value $startvalue = $page * $limit - ($limit); // Query the database and set the start row and limit $sql = "SELECT * FROM privatemessages WHERE username='$username' AND type='inbox' $orderby LIMIT $startvalue, $limit"; $res = mysql_query($sql) or die (mysql_error()); echo "<table border=0 align=center cellpadding=\"0\" cellspacing=\"1\" width=\"100%\"> <tr height=\"28em\"> <td width=\"28em\" align=center background=\"gradient.gif\"><img src=\"images/xx.gif\"></td> <td width=\"28em\" align=center background=\"gradient.gif\"> <input type=checkbox value=\"Check All\" onClick=\"this.value=check(action)\"></td> <td width=\"80em\" background=\"gradient.gif\"> <a href=\"".$_SERVER['PHP_SELF']."?page=".$page."&order=".$ordersendermenu."\"><b>".$menu1." Sender</b></a></td> <td width=\"160em\" background=\"gradient.gif\"> <a href=\"".$_SERVER['PHP_SELF']."?page=".$page."&order=".$ordersubjectmenu."\"><b>".$menu2." Subject</b></a></td> <td width=\"70em\" background=\"gradient.gif\"> <a href=\"".$_SERVER['PHP_SELF']."?page=".$page."&order=".$orderdatemenu."\"><b>".$menu3." Date</b></a></td> </tr>"; // Do a quick check to see if there are any records to display if(mysql_num_rows($res) == 0){ echo "<tr> <td colspan=3 align=center>No messages found!!</td> </tr>"; } // Start loop through records $counter=0; while ($r = mysql_fetch_array($res)){ $date = strtotime($r['entrydate']) + $row['timeoffset']; $date = date('Y-m-d',$date); $subject = substr($r['entrytitle'], 0, 30); if (strlen($r['entrytitle']) > 30){$subject = "$subject..";} $user = substr($r['user'], 0, 10); if (strlen($r['user']) > 10){$user = "$user..";} if ($r['flag'] == "unread") {$color = "#cccccc";} else {$color = "#f2f2f2";} // Echo out the records with wordwrap, remove if you like echo "<tr>\n"; echo "<td align=center><img src=\"images/". $r['entryicon'] .".gif\"></td>\n"; echo "<td align=center><input type=checkbox name=id value=\"". $r['id'] ."\"></td>\n"; echo "<td>". $user ."</td>\n"; echo "<td>". $subject ."</td>\n"; echo "<td>". $date ."</td>\n"; echo "</tr>\n"; $counter++;} // Close the table echo "</table>"; // Start links for pages echo "<p align=center>"; // Sets link for previous 25 and return to page 1 if($page != 1){ $pageprev = ($page - 1); echo "<a href=\"".$_SERVER['PHP_SELF']."?page=1&order=$order\"><<</a> "; echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$pageprev&order=$order\">PREV </a> "; }else{ echo "PREV "; } // Find out the total number of pages depending on the limit set $numofpages = $totalrows / $limit; $totalpages = ceil($numofpages); // Loop thru all the pages and echo out the links for($i = 1; $i <= $numofpages; $i++){ if($i == $page){ echo "[".$i."] "; }else{ echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i&order=$order\">$i</a> "; } } // Check for straglers after the limit blocks if(($totalrows % $limit) != 0){ if($i == $page){ echo "[".$i."] "; }else{ echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i&order=$order\">$i</a> "; } } // Print out the Next 25 and Goto Last page links if(($totalrows - ($limit * $page)) > 0){ $pagenext = ($page + 1); echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$pagenext&order=$order\">NEXT </a> "; echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$totalpages&order=$order\">>></a> "; }else{ echo("NEXT"); } echo "</p>"; // Free results mysql_free_result($res); // Close mysql connection mysql_close($mysql_conn);?> <input class="button" name="delete" type="submit" value="Delete Entry" onClick="javascript: return checkDelete()" /> </form>[/code] Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted January 7, 2007 Author Share Posted January 7, 2007 where the "// Start loop through records" is the start of my form, is not actually html code, but html in php, because I am echoing them out in pages.Ted Quote Link to comment Share on other sites More sharing options...
matto Posted January 7, 2007 Share Posted January 7, 2007 You foreach() is expecting an arraythis line:[code]echo "<td align=center><input type=checkbox name=id value=\"". $r['id'] ."\"></td>\n";[/code]s/be[code]echo "<td align=center><input type=checkbox name=id[$counter] value=\"". $r['id'] ."\"></td>\n";[/code]the id var is now an array.... :) Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted January 7, 2007 Author Share Posted January 7, 2007 thanks :D, I will try it. Quote Link to comment Share on other sites More sharing options...
matto Posted January 7, 2007 Share Posted January 7, 2007 Also just happened to notice this line..[code]<form name="deletepm" action="" method="post">[/code]probably s/be[code]<form name="deletepm" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">[/code] Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted January 7, 2007 Author Share Posted January 7, 2007 okay. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.