UPDATE werkt/lukt niet
Als je een checkbox aanvinkt (of meerdere) en je klikt op de knop 'Delete' dan worden die regels uit de tabel verwijderd.
Nu wil ik de knop 'Delete' vervangen door 'Update', dus als je een checkbox (of meerdere) aanvinkt, en als je dan op 'Update' klikt, dat 'status' automatisch gewijzigd wordt naar 'ja'.
Voorbeeld zoals het nu is:
http://www.pro-aviation.nl/test/database/index.php
Hieronder het gehele script. Het 'Delete' gedeelte staat helemaal onderaan. Ik kom er echter niet uit om daar een 'Update' van de maken.
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
80
81
82
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
80
81
82
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
$host="localhost"; // Host name
$username="xxxx"; // Mysql username
$password="xxxx"; // Mysql password
$db_name="xxxx"; // Database name
$tbl_name="xxxx"; // 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="8" bgcolor="#FFFFFF"><strong><input name="delete" type="submit" id="delete" value="Delete Selection">
<a href="index.php?p=form&mode=add">Add New</a> </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>Special</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Update</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" 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['speciaal']; ?></td>
<td bgcolor="#FFFFFF"><a href="<? echo "index.php?p=form&id=".$rows['id']."&mode=update"; ?>">Update</a></td>
</tr>
<?php
}
?>
<tr>
<td align="center" bgcolor="#FFFFFF"></td>
<td colspan="8" align="left" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete Selection"></td>
</tr>
<?
// Check if delete button active, start this
if(isset($_POST['delete'])){
foreach($_POST["checkbox"] as $key => $value)
{
$sql = "DELETE FROM $tbl_name WHERE id='$value'";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=index.php\">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
error_reporting(E_ALL);
ini_set("display_errors", 1);
$host="localhost"; // Host name
$username="xxxx"; // Mysql username
$password="xxxx"; // Mysql password
$db_name="xxxx"; // Database name
$tbl_name="xxxx"; // 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="8" bgcolor="#FFFFFF"><strong><input name="delete" type="submit" id="delete" value="Delete Selection">
<a href="index.php?p=form&mode=add">Add New</a> </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>Special</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Update</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" 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['speciaal']; ?></td>
<td bgcolor="#FFFFFF"><a href="<? echo "index.php?p=form&id=".$rows['id']."&mode=update"; ?>">Update</a></td>
</tr>
<?php
}
?>
<tr>
<td align="center" bgcolor="#FFFFFF"></td>
<td colspan="8" align="left" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete Selection"></td>
</tr>
<?
// Check if delete button active, start this
if(isset($_POST['delete'])){
foreach($_POST["checkbox"] as $key => $value)
{
$sql = "DELETE FROM $tbl_name WHERE id='$value'";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=index.php\">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
Moest een nieuwe button toevoegen
Daarna het onderste stuk aanpassen
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
if( isset( $_POST['update'] ) )
{
foreach($_POST["checkbox"] as $key => $value)
{
$sql = "UPDATE $tbl_name SET status='ja' WHERE id='$value'";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=index.php\">";
}
}
?>
if( isset( $_POST['update'] ) )
{
foreach($_POST["checkbox"] as $key => $value)
{
$sql = "UPDATE $tbl_name SET status='ja' WHERE id='$value'";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=index.php\">";
}
}
?>
Een datamodel is niet dynamisch, je hebt dus nooit en te nimmer een variabele nodig om de tabelnaam in te zetten. Wanneer dat wel het geval is, doe je iets fout, dan klopt jouw datamodel waarschijnlijk van geen ene meter.
status='ja' is trouwens ook bijzonder, ik kan me niets voorstellen bij zo'n status. "inbestelling", "wacht op antwoord", dat zijn statussen waar je iets mee kunt, maar met "ja" ?
Je doet ook niets aan beveiliging, checkboxen slinger je zonder enige vorm van beveiliging in de queries, SQL-injection ligt dan ook op de loer. Gebruik mysql_real_escape_string() om hier wat aan te doen, het zou lullig zijn om ineens de complete database naar de bliksem te helpen...
$_POST["jepostactie"]
en
$rows['jerij']
Nou heb ik weer even iets zitten proberen. Een deel werkt wel, maar de update (ja alweer) doet het niet. Ik weet zo even niet waar de fout zit, kan het niet ontdekken. Als ik een vakje aanklik en dan op 'Update' klik, dan gebeurd er niets.
Als ik bijvoorbeeld de eerste regel aanvink (de checkbox) en dan op 'Update' klik, kom ik weer op dezelfde pagina terecht, alleen blijft die regel wel staan, terwijl de bedoeling is dat bij het updaten de status naar 'ja' wordt gezet en dat die dus niet zichtbaar is.
Hier het script:
detail_toegevoegd.php
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
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
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table border="0" cellpadding="0" cellspacing="" width="800" STYLE="font-size: 12px" STYLE="border-style: solid; border-color: black; border-width: 1px;">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="8" bgcolor="#FFFFFF"><strong><input name="update" type="submit" id="update" value="Update Selection">
<a href="index.php?p=queue_form&mode=add">Add New</a> </strong></td>
</tr>
<tr align="center">
<td width="2%" height="12" bgcolor="7b8a8f" class="style2" id="table_border_bottom" STYLE="border-top-style: solid; border-color: black; border-width: 1px;"> </td>
<td width="2%" height="12" bgcolor="7b8a8f" class="style2" STYLE="border-top-style: solid; border-color: black; border-width: 1px;" id="table_border_bottom">ID </td>
<td width="7%" height="12" bgcolor="7b8a8f" class="style2" STYLE="border-top-style: solid; border-color: black; border-width: 1px;"id="table_border_bottom"><div align="left">Registratie</div></td>
<td width="18%" height="12" bgcolor="7b8a8f" class="style2" STYLE="border-top-style: solid; border-color: black; border-width: 1px;"id="table_border_bottom"><div align="left">Toestel</div></td>
<td width="19%" height="12" bgcolor="7b8a8f" class="style2" STYLE="border-top-style: solid; border-color: black; border-width: 1px;"id="table_border_bottom"><div align="left">Maatschappij </div></td>
<td width="28%" height="12" bgcolor="7b8a8f" class="style2" STYLE="border-top-style: solid; border-color: black; border-width: 1px;"id="table_border_bottom"><div align="left">Special</div></td>
<td width="2%" height="12" bgcolor="696a6a" class="style2" STYLE="border-top-style: solid; border-color: black; border-width: 1px;"id="table_border4_bottom"><div align="center"> </div></td>
</tr>
<?php
if(isset($_GET['maatschappij']) && is_numeric($_GET['maatschappij']))
{
$id = $_GET['maatschappij'];
}
else
{
$maatschappij = 'Ryanair';
}
mysql_connect( "localhost", "xxxx", "xxxx" );
mysql_select_db( "xxxx" );
error_reporting( E_ALL ^ E_NOTICE );
$query = "SELECT * FROM fotodb WHERE status='ja' AND maatschappij='". $maatschappij ."'";
$res = mysql_query($query) or die(mysql_error());
while($result = mysql_fetch_array($res)){
echo '
<table border="0" align="center" width="800" cellpadding="1" cellspacing="0">
<tr>
<td width="2%" align="center" bgcolor="dfe0e0" class="style3"><input name="checkbox[]" type="checkbox" value="'.$result['id'].'"></td>
<td width="2%" height="12" bgcolor="dfe0e0" class="style3">' .$result['id'] .'</td>
<td width="7%" height="12" bgcolor="dfe0e0" class="style3">' .$result['registratie'] .'</td>
<td width="18%" height="12" bgcolor="dfe0e0" class="style3">' .$result['toestel'] .'</td>
<td width="19%" height="12" bgcolor="dfe0e0" class="style3">' .$result['maatschappij'] .'</td>
<td width="28%" height="12" bgcolor="dfe0e0" class="style3" id="speciaal">' .$result['speciaal'] .'</td>
<td width="2%" bgcolor="bec3c3" class="style3" id="table_border4">' .$result['status'] .'</td></tr>';
}
?>
<tr>
<td align="center" bgcolor="#FFFFFF" STYLE="border-top-style: solid; border-color: black; border-width: 1px;"> </td>
<td colspan="14" align="left" bgcolor="#FFFFFF" STYLE="border-top-style: solid; border-color: black; border-width: 1px;">
<input name="update" type="submit" id="update" value="Update Selection">
</td>
</tr>
<?php
if( isset( $_POST['update'] ) )
{
foreach($_POST["checkbox"] as $key => $value)
{
$sql = "UPDATE $tbl_name SET status='ja' WHERE id='$value'";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=index.php\">";
}
}
?>
<tr>
<td><form name="form1" method="post" action="">
<table border="0" cellpadding="0" cellspacing="" width="800" STYLE="font-size: 12px" STYLE="border-style: solid; border-color: black; border-width: 1px;">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="8" bgcolor="#FFFFFF"><strong><input name="update" type="submit" id="update" value="Update Selection">
<a href="index.php?p=queue_form&mode=add">Add New</a> </strong></td>
</tr>
<tr align="center">
<td width="2%" height="12" bgcolor="7b8a8f" class="style2" id="table_border_bottom" STYLE="border-top-style: solid; border-color: black; border-width: 1px;"> </td>
<td width="2%" height="12" bgcolor="7b8a8f" class="style2" STYLE="border-top-style: solid; border-color: black; border-width: 1px;" id="table_border_bottom">ID </td>
<td width="7%" height="12" bgcolor="7b8a8f" class="style2" STYLE="border-top-style: solid; border-color: black; border-width: 1px;"id="table_border_bottom"><div align="left">Registratie</div></td>
<td width="18%" height="12" bgcolor="7b8a8f" class="style2" STYLE="border-top-style: solid; border-color: black; border-width: 1px;"id="table_border_bottom"><div align="left">Toestel</div></td>
<td width="19%" height="12" bgcolor="7b8a8f" class="style2" STYLE="border-top-style: solid; border-color: black; border-width: 1px;"id="table_border_bottom"><div align="left">Maatschappij </div></td>
<td width="28%" height="12" bgcolor="7b8a8f" class="style2" STYLE="border-top-style: solid; border-color: black; border-width: 1px;"id="table_border_bottom"><div align="left">Special</div></td>
<td width="2%" height="12" bgcolor="696a6a" class="style2" STYLE="border-top-style: solid; border-color: black; border-width: 1px;"id="table_border4_bottom"><div align="center"> </div></td>
</tr>
<?php
if(isset($_GET['maatschappij']) && is_numeric($_GET['maatschappij']))
{
$id = $_GET['maatschappij'];
}
else
{
$maatschappij = 'Ryanair';
}
mysql_connect( "localhost", "xxxx", "xxxx" );
mysql_select_db( "xxxx" );
error_reporting( E_ALL ^ E_NOTICE );
$query = "SELECT * FROM fotodb WHERE status='ja' AND maatschappij='". $maatschappij ."'";
$res = mysql_query($query) or die(mysql_error());
while($result = mysql_fetch_array($res)){
echo '
<table border="0" align="center" width="800" cellpadding="1" cellspacing="0">
<tr>
<td width="2%" align="center" bgcolor="dfe0e0" class="style3"><input name="checkbox[]" type="checkbox" value="'.$result['id'].'"></td>
<td width="2%" height="12" bgcolor="dfe0e0" class="style3">' .$result['id'] .'</td>
<td width="7%" height="12" bgcolor="dfe0e0" class="style3">' .$result['registratie'] .'</td>
<td width="18%" height="12" bgcolor="dfe0e0" class="style3">' .$result['toestel'] .'</td>
<td width="19%" height="12" bgcolor="dfe0e0" class="style3">' .$result['maatschappij'] .'</td>
<td width="28%" height="12" bgcolor="dfe0e0" class="style3" id="speciaal">' .$result['speciaal'] .'</td>
<td width="2%" bgcolor="bec3c3" class="style3" id="table_border4">' .$result['status'] .'</td></tr>';
}
?>
<tr>
<td align="center" bgcolor="#FFFFFF" STYLE="border-top-style: solid; border-color: black; border-width: 1px;"> </td>
<td colspan="14" align="left" bgcolor="#FFFFFF" STYLE="border-top-style: solid; border-color: black; border-width: 1px;">
<input name="update" type="submit" id="update" value="Update Selection">
</td>
</tr>
<?php
if( isset( $_POST['update'] ) )
{
foreach($_POST["checkbox"] as $key => $value)
{
$sql = "UPDATE $tbl_name SET status='ja' WHERE id='$value'";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=index.php\">";
}
}
?>