meerdere rijen verwijderen mbv checkbox
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
$host="localhost"; // Host name
$username="*****"; // Mysql username
$password="*****"; // Mysql password
$db_name="*****"; // Database name
$tbl_name="*****"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="800" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="7" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Registration</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Aircraft</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>C/N</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Airline</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Location</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>URL</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo
$rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['registratie']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['toestel']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['serial']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['maatschappij']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['locatie']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['url']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="8" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete"
value="Delete"></td>
</tr>
<?
// Check if delete button active, start this
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
$host="localhost"; // Host name
$username="*****"; // Mysql username
$password="*****"; // Mysql password
$db_name="*****"; // Database name
$tbl_name="*****"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="800" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="7" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Registration</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Aircraft</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>C/N</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Airline</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Location</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>URL</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo
$rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['registratie']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['toestel']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['serial']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['maatschappij']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['locatie']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['url']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="8" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete"
value="Delete"></td>
</tr>
<?
// Check if delete button active, start this
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
Als ik namelijk een checkbox aanklik en dan op 'delete' klik, gebeurd er niets. Het record wordt niet verwijderd.
GRIJP EEN BRANDEND CRUSIFIX EN GA NAAR HET HUIS VAN DEGENE DIE REGISTER_GLOBALS HEEFT BEDACHT
hmm hoe kan ik het wel werkende krijgen of kan dat niet?
Veranderen in
if(isset($_POST["delete")){
En als er nu op enter word gedrukt doet je form het niet dus als je toch aan het verbeteren bent :
if($_SERVER["REQUEST_METHOD"] == "POST")
Ik krijg nog niets verwijderd, maar dat was ook al gezegt.
Misschien een stomme vraag, maar waar moet ik if($_SERVER['REQUEST_METHOD'] == 'POST') neerzetten?
Rust in Vrede .....
(bij wijze van spreken want als je het doet heb je voorlopig geen rust :P)
Code (php)
1
Notice: Undefined variable: checkbox in /home/vhosts/aviation-pics.com/subdomains/fotodatabase/httpdocs/test/delete_multiple.php on line 63
Als ik naar die regel ga, kom ik op de volgende tekst uit:
$del_id = $checkbox[$i];
Wat is hier fout aan waardoor die een foutmelding geeft?
$checkbox bestaat niet.
probeer het eens met $_POST["checkbox"]
dan krijg ik 'Undefined index: checkbox in.........'
Dan bestaat die POST waarde niet.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
$host="localhost"; // Host name
$username="*****"; // Mysql username
$password="*****"; // Mysql password
$db_name="*****"; // Database name
$tbl_name="*****"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="800" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="7" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Registration</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Aircraft</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>C/N</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Airline</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Location</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>URL</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[<?=$rows['id'];?>]" type="checkbox" value="<?=$rows['id'];?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['registratie']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['toestel']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['serial']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['maatschappij']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['locatie']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['url']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="8" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?
// Check if delete button active, start this
if(isset($_POST['delete'])){
for($i=0;$i<$count;$i++){
$del_id = $_POST["checkbox[]"];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
error_reporting(E_ALL);
ini_set("display_errors", 1);
$host="localhost"; // Host name
$username="*****"; // Mysql username
$password="*****"; // Mysql password
$db_name="*****"; // Database name
$tbl_name="*****"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="800" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="7" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Registration</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Aircraft</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>C/N</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Airline</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Location</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>URL</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[<?=$rows['id'];?>]" type="checkbox" value="<?=$rows['id'];?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['registratie']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['toestel']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['serial']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['maatschappij']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['locatie']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['url']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="8" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?
// Check if delete button active, start this
if(isset($_POST['delete'])){
for($i=0;$i<$count;$i++){
$del_id = $_POST["checkbox[]"];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
Ik snap er nou al helemaal niets meer van waarom het niet werkt. Iemand een idee?
Gewijzigd op 01/01/1970 01:00:00 door Frank -
Code (php)
1
2
3
4
5
6
7
2
3
4
5
6
7
<?php
for($i=0;$i<$count;$i++){
$del_id = $_POST["checkbox[]"];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}
?>
for($i=0;$i<$count;$i++){
$del_id = $_POST["checkbox[]"];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}
?>
Moet worden (ongeveer)
Code (php)
Gewijzigd op 01/01/1970 01:00:00 door Jacco Engel
Gebruik dus mysql_affected_rows() om te zien of een rij is verwijderd, dat de query technisch lukt, zegt namelijk helemaal niets.
Zet je leesbril op. ik zeg ook ongeveer.
en
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
<?
// Check if delete button active, start this
if(isset($_POST['delete'])){
for($i=0;$i<$count;$i++){
$del_id = $_POST["checkbox"];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}
[/code]
ls ik nou 1 regel selecteer en op 'Delete' druk dan wordt die netjes verwijderd. Maar selecteer ik 2 regels, dan wordt degene met de hoogste ID (ik selecteer bijvoorbeeld 2 en 6, dan wordt 6 verwijderd) verwijderd.
Het werkt nou dus wel, maar hoe kan ik ervoor zorgen dat er meerdere regels verwijderd wordt?
// Check if delete button active, start this
if(isset($_POST['delete'])){
for($i=0;$i<$count;$i++){
$del_id = $_POST["checkbox"];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}
[/code]
ls ik nou 1 regel selecteer en op 'Delete' druk dan wordt die netjes verwijderd. Maar selecteer ik 2 regels, dan wordt degene met de hoogste ID (ik selecteer bijvoorbeeld 2 en 6, dan wordt 6 verwijderd) verwijderd.
Het werkt nou dus wel, maar hoe kan ik ervoor zorgen dat er meerdere regels verwijderd wordt?
en
foreach($_POST["checkbox"] as $key => $value)
{
// doe wat
}
En vervolgens met een foreach loop