checkbox help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Think Pink
Forum Contributor
Posts: 106
Joined: Mon Aug 02, 2004 3:29 pm

checkbox help

Post by Think Pink »

hello everybody.
i have a problem. i have index.php that returns up to 7 rows in a mysql database. each row has a checkbox. what i want to do is when i check 1-2 or more checkboxes, the apropriate rows in the database are deleted. I don't want to do this manualy row by row. I want to be able to delete more than one row at once. I hope u understand my problem. sorrry for bad english
Thx in advance.

index.php

Code: Select all

<?php
// informatii privind conectarea la $db 

// selectarea informartiilor
$sql = "SELECT oferta_generala.id_oferta_generala, pc.nume_pc FROM oferta_generala, pc WHERE (oferta_generala.id_pc = pc.id_pc) ORDER BY oferta_generala.data_anunt DESC LIMIT 7";
$result = mysql_query($sql);

print "<form name='stergere' action='results.php' method='post'>";

// determina nr de inregistrari returnate
$number = mysql_num_rows($result);

// Afiseaza informatiile
print "<table cellpadding=2 cellspacing='2'>
<tr>
<TD bgcolor='#669999'>&nbsp;</TD>
<TD bgcolor='#669999' width='100'>&nbsp;<b>pc</b></TD></TR>";
for($i=0; $i<$number; $i++) {
$id_oferta_generala = mysql_result($result, $i, "id_oferta_generala");
$nume_pc = mysql_result($result, $i, "nume_pc");

// alterneaza culoarea la randuri
if ($i%2 == 0) {
print "<tr bgcolor='Gainsboro'>";
} else {
print "<tr>";
}
print "<TD><INPUT TYPE='CHECKBOX' NAME='alege' VALUE='$id_oferta_generala' ALIGN='middle'></TD>
<TD>$nume_pc</TD></tr>";
}
print "</table>";

print "<INPUT TYPE='SUBMIT' value='Delete' align='middle'></form>";

mysql_close();
?>
results.php

Code: Select all

<?php
// informatii privind conectarea la $db 

if (!isset($_POST['alege'])) {    
 echo "Trebuie sa alegi cel putin o inregistrare"; 
 } else {    
 foreach ($_POST['alege'] as $id_oferta_generala) {       
 $sql="DELETE FROM oferta_generala WHERE id_oferta_generala='".$_POST['id_oferta_generala']."'";
 mysql_query($sql);
 }
}
?>
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Code: Select all

DELETE FROM table WHERE id = '5' OR id = '3';
Etc and just use the values from the check box in the query.
User avatar
Think Pink
Forum Contributor
Posts: 106
Joined: Mon Aug 02, 2004 3:29 pm

checkbox help

Post by Think Pink »

ok. i finally got it. thx

the answer was :

Code: Select all

<?php
print "<TD><INPUT TYPE='CHECKBOX' NAME='alege' VALUE='$id_oferta_generala' ALIGN='middle'></TD>?>
and

Code: Select all

<?php
$sql="DELETE FROM oferta_generala WHERE id_oferta_generala='".$id_oferta_generala."'"; 
?>
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

for a lil more info to help you get your toes in the right direction, please read this lil thing I made:

viewtopic.php?t=20198

:wink:
Post Reply