[PDO] Meerdere rijen tegelijk updaten
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<div class="pagetitle">
Top 20 / Bewerken
</div>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$dw = $_POST['dw'];
$vw = $_POST['vw'];
$aw = $_POST['aw'];
$titel = $_POST['titel'];
$artiest = $_POST['artiest'];
$A = count($dw);
$conn = new PDO('mysql:host=localhost;dbname=#DBNAAM', '#DBUSER', '#DBPASS');
try {
$set_details = "UPDATE `top20`
SET `vw` = :vw,
`aw` = :aw,
`titel` = :titel,
`artiest` = :artiest
WHERE `dw` = :dw";
$STH = $conn->prepare($set_details);
$i = 0;
while($i < $A) {
$STH->bindParam(':dw', $dw, PDO::PARAM_INT);
$STH->bindParam(':vw', $vw, PDO::PARAM_INT);
$STH->bindParam(':aw', $aw, PDO::PARAM_INT);
$STH->bindParam(':titel', $titel, PDO::PARAM_STR);
$STH->bindParam(':artiest', $artiest, PDO::PARAM_STR);
$STH->execute();
$i++;
}
}
catch(PDOException $e) {
echo "I'm sorry, but there was an error updating the database.";
echo $e->getMessage();
}
$conn = null;
}
else {
$sql="SELECT dw, vw, aw, titel, artiest FROM top20 ORDER BY dw";
$result=mysql_query($sql);
// Count table rows
$count=mysql_num_rows($result);
?>
<table border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="#">
<tr>
<td>
<table border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center">DW</td>
<td align="center">VW</td>
<td align="center">AW</td>
<td align="center"><strong>Titel</strong></td>
<td align="center">Artiest</td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td>
<? echo $rows['dw']; ?>
<input name="dw" type="hidden" id="dw" value="<? echo $rows['dw']; ?>">
</td>
<td>
<input name="vw" type="text" id="vw" value="<? echo $rows['vw']; ?>" style="width:50px;">
</td>
<td>
<input name="aw" type="text" id="aw" value="<? echo $rows['aw']; ?>" style="width:50px;">
</td>
<td>
<input name="titel" type="text" id="titel" value="<? echo $rows['titel']; ?>">
</td>
<td>
<input name="artiest" type="text" id="artiest" value="<? echo $rows['artiest']; ?>">
</td>
</tr>
<?php } ?>
<tr>
<td colspan="5"><input type="submit" name="Submit" value="Update" class="button"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
}
?>
Top 20 / Bewerken
</div>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$dw = $_POST['dw'];
$vw = $_POST['vw'];
$aw = $_POST['aw'];
$titel = $_POST['titel'];
$artiest = $_POST['artiest'];
$A = count($dw);
$conn = new PDO('mysql:host=localhost;dbname=#DBNAAM', '#DBUSER', '#DBPASS');
try {
$set_details = "UPDATE `top20`
SET `vw` = :vw,
`aw` = :aw,
`titel` = :titel,
`artiest` = :artiest
WHERE `dw` = :dw";
$STH = $conn->prepare($set_details);
$i = 0;
while($i < $A) {
$STH->bindParam(':dw', $dw, PDO::PARAM_INT);
$STH->bindParam(':vw', $vw, PDO::PARAM_INT);
$STH->bindParam(':aw', $aw, PDO::PARAM_INT);
$STH->bindParam(':titel', $titel, PDO::PARAM_STR);
$STH->bindParam(':artiest', $artiest, PDO::PARAM_STR);
$STH->execute();
$i++;
}
}
catch(PDOException $e) {
echo "I'm sorry, but there was an error updating the database.";
echo $e->getMessage();
}
$conn = null;
}
else {
$sql="SELECT dw, vw, aw, titel, artiest FROM top20 ORDER BY dw";
$result=mysql_query($sql);
// Count table rows
$count=mysql_num_rows($result);
?>
<table border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="#">
<tr>
<td>
<table border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center">DW</td>
<td align="center">VW</td>
<td align="center">AW</td>
<td align="center"><strong>Titel</strong></td>
<td align="center">Artiest</td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td>
<? echo $rows['dw']; ?>
<input name="dw" type="hidden" id="dw" value="<? echo $rows['dw']; ?>">
</td>
<td>
<input name="vw" type="text" id="vw" value="<? echo $rows['vw']; ?>" style="width:50px;">
</td>
<td>
<input name="aw" type="text" id="aw" value="<? echo $rows['aw']; ?>" style="width:50px;">
</td>
<td>
<input name="titel" type="text" id="titel" value="<? echo $rows['titel']; ?>">
</td>
<td>
<input name="artiest" type="text" id="artiest" value="<? echo $rows['artiest']; ?>">
</td>
</tr>
<?php } ?>
<tr>
<td colspan="5"><input type="submit" name="Submit" value="Update" class="button"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
}
?>
Edit:
Code tussen code-tags geplaatst. Gelieve dit in het vervolg te doen. Zo blijft het forum overzichtelijk.
Gewijzigd op 28/09/2013 20:42:54 door - Ariën -
Waarom gebruik je PDO voor je update query en mysql voor je select query? Ergens denk ik dat je geen connectie hebt voor je mysql...
Ik neem aan dat de connectie van MySQL correct is, het selecteren en tonen van de gegevens gaat prima. totdat er op de 'update'knop gedrukt wordt, dan gebeurt er niets..