Cory94bailly Posted June 4, 2008 Share Posted June 4, 2008 I am trying to add 2 extra columns at the beginning of my table so admins can edit/delete one by one... My code: <head> <title>Recently Wanted</title> <link href="style.css" rel="stylesheet" type="text/css" /> </head> <body> <br /> <div id="menu"> <? include('menu.html'); ?> </div> <div id="content"> <?php require('config.php'); date_default_timezone_set('America/New_York'); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <br /> <center> <h1>Current Date: <? echo date('m-d-Y'); ?></h1> <h1>Current time: <? echo date("g:i a"); ?></h1> <table border="1" cellpadding="15"> <?php mysql_connect("$path", "$username", "$password") or die(mysql_error()); mysql_select_db("$database") or die(mysql_error()); $query = "SELECT * FROM recentwanted"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ echo "<tr>"; echo "<td>"; echo $row['date']; echo "</td>"; echo "<td>"; echo $row['time']; echo "</td>"; echo "<td>"; echo $row['name']; echo "</td>"; echo "<td>"; echo $row['game']; echo "</td>"; echo "<td>"; echo $row['aimbot']; echo "</td>"; echo "<td>"; echo $row['gui']; echo "</td>"; echo "<td>"; echo $row['killspam']; echo "</td>"; echo "<td>"; echo $row['ip']; echo "</td>"; echo "<td>"; echo $row['hostname']; echo "</td>"; echo "</tr>"; } ?> </table> </div> Here's another script of mine: <head> <title>Edit/Delete News</title> <link href="style.css" rel="stylesheet" type="text/css" /> </head> <body> <br /> <div id="menu"> <? include('menu.html'); ?> </div> <div id="content"> <?php require 'config.php'; // Make a MySQL Connection mysql_connect($path, $username, $password) or die(mysql_error()); mysql_select_db($database) or die(mysql_error()); $query1 = "SELECT * FROM news ORDER BY sticky, id ASC"; $result1 = mysql_query($query1) or die(mysql_error()); function printNews($result) { if ( mysql_num_rows($result) < 1 ) { echo "<h1>No news!</h1>"; } else { echo "<table cellspacing=\"2\" cellpadding=\"5\">\n"; while($news = mysql_fetch_assoc($result)) { $sticky = ($news['sticky'] == 'y') ? "<font color='red'><u>Announcement:</u></font> " : ''; echo <<<HTML <tr> <th align="left">{$sticky}<b>{$news['title']}</b></th> <td rowspan="2" valign="bottom" align="left"> <a href="news_edit.php?id={$news['id']}"><img border="0" src="../images/edit.png" alt="Edit" /></a><br /> <a href="news_delete.php?id={$news['id']}"><img border="0" src="../images/delete.png" alt="Edit" /></a> </td> </tr> <tr> <td>{$news['news']}</td> </tr> HTML; } echo "\n</table><br />"; } } printNews($result1); ?> I want something like that but to fit into my other script.. Also, I don't even know how to add checkboxes so that if you select a checkbox for say 5 of them, it will only delete those 5, not all... Thanks all! (Any help is appreciated...) Quote Link to comment Share on other sites More sharing options...
craygo Posted June 4, 2008 Share Posted June 4, 2008 Well should be easy since only check boxes that are actually checked get passed. Easiest thing to do is create an array of the id's of the rows you want to delete. while($row = mysql_fetch_array($result)){ echo "<tr>"; echo "<td>"; echo "<input type=\"checkbox\" name=\"del[]\" value=\"{$row['id']}\" />"; echo "</td>"; echo "<td>"; echo $row['date']; echo "</td>"; echo "<td>"; echo $row['time']; echo "</td>"; echo "<td>"; echo $row['name']; echo "</td>"; echo "<td>"; echo $row['game']; echo "</td>"; echo "<td>"; echo $row['aimbot']; echo "</td>"; echo "<td>"; echo $row['gui']; echo "</td>"; echo "<td>"; echo $row['killspam']; echo "</td>"; echo "<td>"; echo $row['ip']; echo "</td>"; echo "<td>"; echo $row['hostname']; echo "</td>"; echo "</tr>"; } and on the page that processes the delete foreach($_POST['del'] as $id){ $sql = "DELETE FROM `recentwanted` WHERE `id` = '$id' LIMIT 1"; mysql_query($sql) or die(mysql_error()); } Just a quick help Ray Quote Link to comment Share on other sites More sharing options...
Cory94bailly Posted June 4, 2008 Author Share Posted June 4, 2008 I fixed that up a bit and I get this: Warning: Invalid argument supplied for foreach() in /home/content/m/a/r/markbailly/html/fcs/admin/recentwanted.php on line 3 Script: <?php if(isset($_POST['deleteselected'])) { foreach($_POST['del'] as $id){ $sql = "DELETE FROM `recentwanted` WHERE `id` = '$id' LIMIT 1"; mysql_query($sql) or die(mysql_error()); } } ?> <head> <title>Recently Wanted</title> <link href="style.css" rel="stylesheet" type="text/css" /> </head> <body> <br /> <div id="menu"> <? include('menu.html'); ?> </div> <div id="content"> <?php require('config.php'); date_default_timezone_set('America/New_York'); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <br /> <center> <h1>Current Date: <? echo date('m-d-Y'); ?></h1> <h1>Current time: <? echo date("g:i a"); ?></h1> <table border="1" cellpadding="15"> <?php mysql_connect("$path", "$username", "$password") or die(mysql_error()); mysql_select_db("$database") or die(mysql_error()); $query = "SELECT * FROM recentwanted"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ echo "<tr>"; echo "<td>"; echo "<input type='checkbox' name='del[]' value='{$row['id']}' />"; echo "</td>"; echo "<td>"; echo $row['date']; echo "</td>"; echo "<td>"; echo $row['time']; echo "</td>"; echo "<td>"; echo $row['name']; echo "</td>"; echo "<td>"; echo $row['game']; echo "</td>"; echo "<td>"; echo $row['aimbot']; echo "</td>"; echo "<td>"; echo $row['gui']; echo "</td>"; echo "<td>"; echo $row['killspam']; echo "</td>"; echo "<td>"; echo $row['ip']; echo "</td>"; echo "<td>"; echo $row['hostname']; echo "</td>"; echo "</tr>"; } ?> </table> <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"> <input type="submit" name="deleteselected" value="Delete Selected"> </form> </div> Quote Link to comment Share on other sites More sharing options...
discomatt Posted June 4, 2008 Share Posted June 4, 2008 check to make sure it's an array first <?php if ( isset($_POST['del']) && is_array($_POST['del']) ) { foreach ( $_POST['del'] as $id ) { # .... } } ?> Quote Link to comment Share on other sites More sharing options...
Cory94bailly Posted June 4, 2008 Author Share Posted June 4, 2008 Code now: <?php if ( isset($_POST['del']) && is_array($_POST['del']) ) { foreach ( $_POST['del'] as $id ) { $sql = "DELETE FROM `recentwanted` WHERE `id` = '$id' LIMIT 1"; mysql_query($sql) or die(mysql_error()); } } ?> <head> <title>Recently Wanted</title> <link href="style.css" rel="stylesheet" type="text/css" /> </head> <body> <br /> <div id="menu"> <? include('menu.html'); ?> </div> <div id="content"> <?php require('config.php'); date_default_timezone_set('America/New_York'); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <br /> <center> <h1>Current Date: <? echo date('m-d-Y'); ?></h1> <h1>Current time: <? echo date("g:i a"); ?></h1> <table border="1" cellpadding="15"> <?php mysql_connect("$path", "$username", "$password") or die(mysql_error()); mysql_select_db("$database") or die(mysql_error()); $query = "SELECT * FROM recentwanted"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ echo "<tr>"; echo "<td>"; echo "<input type='checkbox' name='del[]' value='{$row['id']}' />"; echo "</td>"; echo "<td>"; echo $row['date']; echo "</td>"; echo "<td>"; echo $row['time']; echo "</td>"; echo "<td>"; echo $row['name']; echo "</td>"; echo "<td>"; echo $row['game']; echo "</td>"; echo "<td>"; echo $row['aimbot']; echo "</td>"; echo "<td>"; echo $row['gui']; echo "</td>"; echo "<td>"; echo $row['killspam']; echo "</td>"; echo "<td>"; echo $row['ip']; echo "</td>"; echo "<td>"; echo $row['hostname']; echo "</td>"; echo "</tr>"; } ?> </table> <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"> <input type="submit" name="deleteselected" value="Delete Selected"> </form> </div> I click submit and it just does nothing. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 4, 2008 Share Posted June 4, 2008 Well, do you have any boxes checked? Also, you have no output, so obviously it's not going to say anything. Quote Link to comment Share on other sites More sharing options...
revraz Posted June 4, 2008 Share Posted June 4, 2008 echo $sql and see what it contains. I'm guessing $id is empty Quote Link to comment Share on other sites More sharing options...
Cory94bailly Posted June 4, 2008 Author Share Posted June 4, 2008 Well, do you have any boxes checked? Also, you have no output, so obviously it's not going to say anything. Yep, I tried 1 box checked then 2... I know, but it never deletes them.. echo $sql and see what it contains. I'm guessing $id is empty I did that and it just showed the button.. lol Quote Link to comment Share on other sites More sharing options...
revraz Posted June 4, 2008 Share Posted June 4, 2008 Use an IF ($submit) clause to detect if the submit button was pressed, since the form is on the same page you only want to delete if the button was pressed. Quote Link to comment Share on other sites More sharing options...
realjumper Posted June 4, 2008 Share Posted June 4, 2008 Here's some code that you may find useful: <table align="center" border="0"> <?php $query = mysql_query("SELECT * FROM table WHERE id='$row[id]'"); while($r = mysql_fetch_array($query)){ echo "<tr><td>"; echo $r['email']." </td><td><input type='checkbox' name='select[]' value='".$r['id']."'></td></tr>"; //just print a title and a text box. note the [], this creates an array of all selected checkboxes. } ?> <tr> <td><input type="submit" name="submit" value="Delete Selected Email Addresses"></td> </tr> </table> $selected = implode(",", $_POST['select']); //this would make the array into something like this 1,3,7,8 etc... if($selected == ''){ echo "Nothing selected!"; }else{ mysql_query("DELETE FROM table WHERE id in ($selected)"); //deletes all from table if id is in variable echo "Deleted records!"; } Quote Link to comment Share on other sites More sharing options...
Cory94bailly Posted June 4, 2008 Author Share Posted June 4, 2008 Here's some code that you may find useful: I bet someone can but sadly, I can't Quote Link to comment Share on other sites More sharing options...
realjumper Posted June 4, 2008 Share Posted June 4, 2008 Here's some code that you may find useful: I bet someone can but sadly, I can't Hmmm....I created a check box delete system using this code which works great Quote Link to comment Share on other sites More sharing options...
Cory94bailly Posted June 4, 2008 Author Share Posted June 4, 2008 Hmmm....I created a check box delete system using this code which works great Ok but I'm not really that good with php... It's sad, I can't even tear that tiny bit apart. Quote Link to comment Share on other sites More sharing options...
realjumper Posted June 4, 2008 Share Posted June 4, 2008 Ok but I'm not really that good with php... It's sad, I can't even tear that tiny bit apart. I'm no 'guru' either, but I made it work quite easily. Make a new page, call it 'test.php' and use the code I supplied. Have a play with it....you can do it :-) Quote Link to comment Share on other sites More sharing options...
Cory94bailly Posted June 4, 2008 Author Share Posted June 4, 2008 I'm no 'guru' either, but I made it work quite easily. Make a new page, call it 'test.php' and use the code I supplied. Have a play with it....you can do it :-) I'm working with it... <?php require('config.php'); date_default_timezone_set('America/New_York'); mysql_connect("$path", "$username", "$password") or die(mysql_error()); mysql_select_db("$database") or die(mysql_error()); if ( isset($_POST['del']) && is_array($_POST['del']) ) { // foreach ( $_POST['del'] as $id ) { // $sql = "DELETE FROM `recentwanted` WHERE `id` = '$id' LIMIT 1"; // mysql_query($sql) or die(mysql_error()); // } //} $selected = implode(",", $_POST['select']); //this would make the array into something like this 1,3,7,8 etc... if($selected == ''){ echo "Nothing selected!"; }else{ mysql_query("DELETE FROM recentwanted WHERE id in ($selected)"); echo "Deleted records!"; } } ?> <head> <title>Recently Wanted</title> <link href="style.css" rel="stylesheet" type="text/css" /> </head> <body> <br /> <div id="menu"> <? include('menu.html'); ?> </div> <div id="content"> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <br /> <center> <h1>Current Date: <? echo date('m-d-Y'); ?></h1> <h1>Current time: <? echo date("g:i a"); ?></h1> <table border="1" cellpadding="15"> <?php $query = "SELECT * FROM recentwanted"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ echo "<tr>"; echo "<td>"; echo "<input type='checkbox' name='selected[]' value='$row[id]' />"; echo "</td>"; echo "<td>"; echo $row['date']; echo "</td>"; echo "<td>"; echo $row['time']; echo "</td>"; echo "<td>"; echo $row['name']; echo "</td>"; echo "<td>"; echo $row['game']; echo "</td>"; echo "<td>"; echo $row['aimbot']; echo "</td>"; echo "<td>"; echo $row['gui']; echo "</td>"; echo "<td>"; echo $row['killspam']; echo "</td>"; echo "<td>"; echo $row['ip']; echo "</td>"; echo "<td>"; echo $row['hostname']; echo "</td>"; echo "</tr>"; } ?> </table> <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"> <input type="submit" name="deleteselected" value="Delete Selected"> </form> </div> ^^Code... I check a box and click submit and nothing changes. Quote Link to comment Share on other sites More sharing options...
realjumper Posted June 4, 2008 Share Posted June 4, 2008 Look at where your form tag is......the only thing in your form is the submit (delete) button Quote Link to comment Share on other sites More sharing options...
Cory94bailly Posted June 6, 2008 Author Share Posted June 6, 2008 Look at where your form tag is......the only thing in your form is the submit (delete) button And..? I don't get it Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 6, 2008 Share Posted June 6, 2008 The only thing in the form is the submit button, not the checkboxes. Move the <form> tag to ABOVE the php loop. Quote Link to comment Share on other sites More sharing options...
Cory94bailly Posted June 6, 2008 Author Share Posted June 6, 2008 <?php require('config.php'); date_default_timezone_set('America/New_York'); mysql_connect("$path", "$username", "$password") or die(mysql_error()); mysql_select_db("$database") or die(mysql_error()); if ( isset($_POST['del']) && is_array($_POST['del']) ) { // foreach ( $_POST['del'] as $id ) { // $sql = "DELETE FROM `recentwanted` WHERE `id` = '$id' LIMIT 1"; // mysql_query($sql) or die(mysql_error()); // } //} $selected = implode(",", $_POST['select']); //this would make the array into something like this 1,3,7,8 etc... if($selected == ''){ echo "Nothing selected!"; }else{ mysql_query("DELETE FROM recentwanted WHERE id in ($selected)"); echo "Deleted records!"; } } ?> <head> <title>Recently Wanted</title> <link href="style.css" rel="stylesheet" type="text/css" /> </head> <body> <br /> <div id="menu"> <? include('menu.html'); ?> </div> <div id="content"> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <br /> <center> <h1>Current Date: <? echo date('m-d-Y'); ?></h1> <h1>Current time: <? echo date("g:i a"); ?></h1> <table border="1" cellpadding="15"> <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"> <?php $query = "SELECT * FROM recentwanted"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ echo "<tr>"; echo "<td>"; echo "<input type='checkbox' name='selected[]' value='$row[id]' />"; echo "</td>"; echo "<td>"; echo $row['date']; echo "</td>"; echo "<td>"; echo $row['time']; echo "</td>"; echo "<td>"; echo $row['name']; echo "</td>"; echo "<td>"; echo $row['game']; echo "</td>"; echo "<td>"; echo $row['aimbot']; echo "</td>"; echo "<td>"; echo $row['gui']; echo "</td>"; echo "<td>"; echo $row['killspam']; echo "</td>"; echo "<td>"; echo $row['ip']; echo "</td>"; echo "<td>"; echo $row['hostname']; echo "</td>"; echo "</tr>"; } ?> </table> <input type="submit" name="deleteselected" value="Delete Selected"> </form> </div> Still does nothing. Quote Link to comment Share on other sites More sharing options...
realjumper Posted June 6, 2008 Share Posted June 6, 2008 What do you get if you echo $selected? Quote Link to comment Share on other sites More sharing options...
sasa Posted June 6, 2008 Share Posted June 6, 2008 change 1st part of your script to <?php require('config.php'); date_default_timezone_set('America/New_York'); mysql_connect("$path", "$username", "$password") or die(mysql_error()); mysql_select_db("$database") or die(mysql_error()); if ( isset($_POST['selected']) && is_array($_POST['selected']) ) {// name in your chkbox is selected! not del or select // foreach ( $_POST['del'] as $id ) { // $sql = "DELETE FROM `recentwanted` WHERE `id` = '$id' LIMIT 1"; // mysql_query($sql) or die(mysql_error()); // } //} $selected = implode(",", $_POST['selected']); // <-- change this line too if($selected == ''){ echo "Nothing selected!"; }else{ mysql_query("DELETE FROM recentwanted WHERE id in ($selected)"); echo "Deleted records!"; } } ?> 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.