Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete multiple database entry with form checkbox 1

Status
Not open for further replies.

iranor

Programmer
Jun 17, 2004
174
CA
I have a page that get data in a mysql table , named mail.
It select messages owned by player and then display it , while adding a checkbox named msgid to each one , having the value the message_id.


$select[0] = "SELECT * FROM mail";
$request[0] = mysql_query($select[0]);
$i=0;
while($results[0] = mysql_fetch_array($request[0])) {
$i++;
echo '<tr>'.
'<td class=row1 width=50% height=40><span class=genmed><a href=index.php?page=notes&type=viewnote&id='.$results[0]['id'].'>'.$results[0]['author'].'</a></span></td>'.
'<td class=row1 width=50% height=40><span class=genmed>'.$results[0]['subject'].'</span></td>'.
'<td class=row1 width=50% height=40><span class=genmed><input type=checkbox name=msgid value='.$results[0]['id'].'></span></td>'.
'</tr>';
}

Here I have problem. How do you add the msgid variable in an array , because if the user select multiple checkbox , the script don't delete them (the entrys).

My query is like this :

$pid="1";
mysql_connect($serveur,$nom,$passe) or die('MySQL error!');
mysql_select_db($base) or die('MySQL error!');
$select[0] = "DELETE * FROM mail_link WHERE player_id='$pid' AND message_id='$msgid'";
$request[0] = mysql_query($select[0]);

I simply want the script to print data on the page , adding a checkbox for each one of them , name being msgid and value being the id of the message. If example I ckeck 5 checkbox and click on Delete , I want the script to delete these five one...
 
There are 2 problems here:
1. You need to name the input element specifically to send an array:
<input type="checkbox" name="msgid[]" value="whatever">
2. Your SQL statement could have the id's in a list or loop through the retrieved array or build a SQL string that lists them with OR statements in the WHERE clause.

Why are you putting your results into request[0] etc. instead of plain vars?
 
What you need to do is have all of your delete checkboxes in an array, and then loop through the array afterwards and remove them from the database.

For the example to work, $results[0]['id'] should be the primary key.

i.e. use something similar to:

Code:
echo "<input type=\"checkbox\" name=\"delarray[."$results[0]['id']."]\" value=\"checked\">";

Then php will return an array full of checked boxes, so use something like:

Code:
if (isset($_POST["delarray"])) {
   foreach ($_POST["delarray"] as $id => $value) {
      if (strcmp($value, "checked") == 0)
         mysql_query("DELETE * FROM mail_link WHERE player_id='$pid' AND message_id='$id'");
   }
}

...which will loop through the array and remove the checked items.
 
Well , i need to do an array. This script is gonna be used in a mailing system for a online game with more than 15 000 users...

Tell me the best way for this situation ;) I'll appreciate an exemple of a form and an exemple of the query.
 
Looks like I was writing while people were posting.

I also made a typo in the example (whoops)

Code:
echo "<input type=\"checkbox\" name=\"delarray[[COLOR=red]".[/color]$results[0]['id']."]\" value=\"checked\">";

Hopefully this allows me to use color in code.
 
I can't make it delete the entries.

The code is

mysql_connect("localhost","root","b1qwerdss") or die('MySQL error!');
mysql_select_db("neomaster_filedb") or die('MySQL error!');
if (isset($_POST["delarray"])) {
foreach ($_POST["delarray"] as $id => $value) {
if (strcmp($value, "checked") == 0)
mysql_query("DELETE * FROM mail_link WHERE player_id='$pid' AND message_id='$id'");
}
}

and the code for showing them is

$select[0] = "SELECT message_id FROM mail_link WHERE player_id='$pid'";
$request[0] = mysql_query($select[0]);
while($res = mysql_fetch_array($request[0])){
$select[1] = "SELECT * FROM mail WHERE id =$res[0]";
$request[1] = mysql_query($select[1]);
while($results[1] = mysql_fetch_array($request[1])) {
echo '<tr>'.
'<td class=row1 width=50% height=40><span class=genmed><a href=index.php?page=notes&type=viewnote&id='.$results[1]['id'].'>'.$results[1]['author'].'</a></span></td>'.
'<td class=row1 width=50% height=40><span class=genmed>'.$results[1]['subject'].'</span></td>'.
'<td class=row1 width=50% height=40><span class=genmed>';
echo "<input type=\"checkbox\" name=\"delarray[".$results[1]['id']."]\" value=\"checked\"></span></td></tr>";
}
}

.The output of delarray is <input type="checkbox" name="delarray[3]" value="checked"> but if I select one simgle choice or any number , nothing happens.
 
1. You have no error checking for your SQL queries. Any syntax errors go unshown:
Code:
DELETE * FROM ....
is invalid SQL. The asterisk is superfluous. I suggest that you fix the SQL and add error checking.
Code:
 $result = mysql_query($SQL) OR die("Query error: ".mysql_error());

2. The HTML portion does not need any offsets. All you need is
Code:
<input type="checkbox" name="delarray[]" value="recordIDhere">
The value attribute has to contain the message id. That means the output will look somewhat like this:
Code:
<input type="checkbox" name="delarray[]" value="125">
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top