/    Sign up×
Community /Pin to ProfileBookmark

delete multiple records based on checkbox

Hi guys,

currently i have a script where i use mysql_fetch_array() to diplay all the records and i have added an checkbox next to each record..

[CODE]
while($row=mysql_fetch_array($result))
{

?>
<tr>
<td width=”24″ height=”0″>
<p align=”center”><font face=”Arial” size=”2″>&nbsp;<input type=”checkbox” name=”remove” value=”<?= $row[‘code’] ?>”></font></p>
</td>
<td width=”28″ height=”0″>
<p align=”center”><font face=”Arial” size=”2″>&nbsp;<?= $i++?></font></p>
</td>
<td width=”106″ height=”0″>
<p align=”center”><font face=”Arial” size=”2″><?= $row[‘store’]?></font></p>
</td>
<td width=”168″ height=”0″>
<p align=”center”><font face=”Arial” size=”2″><?= $row[’email’]?></font></p>
</td>
<td width=”170″ height=”0″>
<p align=”center”><font face=”Arial” size=”2″>&nbsp;&nbsp;<?= $row[‘name’]?></font></p>
</td>
<td width=”112″ height=”0″>
<p align=”center”><font face=”Arial” size=”2″>&nbsp;&nbsp;<?= $row[‘date’]?></font></p>
</td>
<td width=”87″ height=”0″>
<p align=”center”><font face=”Arial” size=”2″>&nbsp;<?= $row[‘amount’]?></font></p>
</td>
<td width=”92″ height=”0″>
<p align=”center”><font face=”Arial” size=”2″><?= $row[‘cc_status’]?></font></p>
</td>
<td width=”132″ height=”0″>
<p align=”center”><font face=”Arial” size=”2″><a href=”transactions2.php?status=approved”>Approved</a>
|<a href=”transactions2.php?status=rejected”> Rejected</a></font></p>
</td>
</tr>
<? } ?>
<tr>
<td width=”967″ height=”4″ colspan=”9″>
<p><font face=”Arial” size=”2″>&nbsp;<input type=”submit” name=”clean” value=”Remove”>&nbsp;|
Remove checked transaction record(s)</font></p>
</td>
</tr>
<tr>
<td width=”967″ colspan=”9″ bgcolor=”#FFCC66″>
<p><a href=”../main.php”><font face=”Arial” size=”2″>Back to main </font></a><font face=”Arial” size=”2″>&nbsp;|
Contact
</font><a href=”mailto:[email protected]”><font face=”Arial” size=”2″>person-in-charge</font></a></p>
</td>
</tr>
</table>
[/CODE]

now my problem is, i wanna delete all the records that the checkbox beside the row is checked… i tried to use DELETE query but it seems like only able to delete one row.. if i check more than one checkbox, it doesn’t work.. if i use mysql_fetch_array() follow by the DELETE query, all my records will be removed even the records i didn’t select. can u guys give me an idea how to delete multiple records based to the checkbox..

[CODE]
<?php
session_start();
header (“cache-control/:private”);
include_once “include/db.php”;
include_once “include/check-session.php”;
$result = mysql_query(“SELECT * FROM transaction ORDER BY `date`”) or die(mysql_error());

$remove = mysql_QUERY(“DELETE FROM transaction WHERE `code`=’$remove'”) or die(mysql_error());

?>
[/CODE]

please advise.

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@Khalid_AliJan 30.2004 — you will have to write your SQL delete statement as such that it deletes ll records whre something is common..

something like

"DELETE FROM tableName WHERE id='$id'";
Copy linkTweet thisAlerts:
@crh3675Jan 30.2004 — You need to rename your checkboxes to have unique names:
<i>
</i>&lt;input type="checkbox" name="remove1" value="&lt;?= $row['code'] ?&gt;"&gt;

&lt;input type="checkbox" name="remove2" value="&lt;?= $row['code'] ?&gt;"&gt;


Then, in your PHP

<i>
</i>&lt;?php
session_start();
header ("cache-control/rivate");
include_once "include/db.php";
include_once "include/check-session.php";
$result = mysql_query("SELECT * FROM transaction ORDER BY <span><code>date</code></span>") or die(mysql_error());

foreach($_POST as $key=&gt;$value){ // or $_GET as $key =&gt; $value
if (eregi("^remove",$key)){ // Parse each posted variable to see if variable name starts with "remove"
$remove = mysql_QUERY("DELETE FROM transaction WHERE <span><code>code</code></span>='$value'") or die(mysql_error());
}
}

?&gt;
Copy linkTweet thisAlerts:
@AliciaauthorFeb 05.2004 — it is still not working,, is there any other way i can use to make this job done..

please advise..
Copy linkTweet thisAlerts:
@crh3675Feb 05.2004 — What exactly isn ot working? Are there errors? Try outputting the sql to the screen to see what is coming up.

<i>
</i>$result = mysql_query("SELECT * FROM transaction ORDER BY <span><code>date</code></span>") or die(mysql_error());

print mysql_num_rows($result); // For Debug

foreach($_POST as $key=&gt;$value){ // or $_GET as $key =&gt; $value
if (eregi("^remove",$key)){ // Parse each posted variable to see if variable name starts with "remove"
$sql="delete from transaction where code='$value'";

<i> </i> print $sql; // For Debug

<i> </i> $remove = mysql_QUERY($sql) or die(mysql_error());
<i> </i>}
}



If $_POST is not working, replace it with $HTTP_POST_VARS;
×

Success!

Help @Alicia spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...