Id wordt soms meegenomen bij update script
Hopelijk komen we er samen achter, alle hulp is welkom. b.v.d.
(En ja, ik open vaak php opnieuw in mijn scripts, maar dat terzijde)
Hieronder de scripts:
Code (php)
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
<?php
foreach($nieuws as $row) {?>
<h2><?php echo $row['kop']; ?>
<a href="<?php echo ADMIN_PATH; ?>nieuws/nieuws_wijzigen.php?nieuws_id=<?php echo $row['nieuws_id']; ?>"><img src="<?php echo LIB_PATH; ?>lib/images/edit.png" alt="Wijzigen" /></a>
<a href="<?php echo ADMIN_PATH; ?>admin_actions/delNieuwsitem.php?nieuws_id=<?php echo $row['nieuws_id']; ?>"><img src="<?php echo LIB_PATH; ?>lib/images/delete.jpg" alt="Verwijder" onclick="return confirm('Weet u zeker dat u dit item wilt verwijderen?')" /></a>
</h2>
<?php } ?>
foreach($nieuws as $row) {?>
<h2><?php echo $row['kop']; ?>
<a href="<?php echo ADMIN_PATH; ?>nieuws/nieuws_wijzigen.php?nieuws_id=<?php echo $row['nieuws_id']; ?>"><img src="<?php echo LIB_PATH; ?>lib/images/edit.png" alt="Wijzigen" /></a>
<a href="<?php echo ADMIN_PATH; ?>admin_actions/delNieuwsitem.php?nieuws_id=<?php echo $row['nieuws_id']; ?>"><img src="<?php echo LIB_PATH; ?>lib/images/delete.jpg" alt="Verwijder" onclick="return confirm('Weet u zeker dat u dit item wilt verwijderen?')" /></a>
</h2>
<?php } ?>
De selectie query die bij bovestaand script hoort is:
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
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
<?php
function getAllNieuws($limit) {
$query = "
SELECT
n.nieuws_id, n.kop, n.subkop, n.datum, n.bericht, COUNT(r.nieuws_reacties_id) AS aantalReacties, r.naam, r.datum, r.bericht AS reactie, r.nieuws_id
FROM
nieuws n
LEFT JOIN
nieuws_reacties r
ON
r.nieuws_id = n.nieuws_id
GROUP BY
n.nieuws_id
LIMIT
:limit
";
$stmt = $this->db->prepare($query);
$stmt->bindParam(':limit', $limit, PDO::PARAM_INT);
$stmt->execute();
$array = $stmt->fetchAll();
return $array;
}
?>
function getAllNieuws($limit) {
$query = "
SELECT
n.nieuws_id, n.kop, n.subkop, n.datum, n.bericht, COUNT(r.nieuws_reacties_id) AS aantalReacties, r.naam, r.datum, r.bericht AS reactie, r.nieuws_id
FROM
nieuws n
LEFT JOIN
nieuws_reacties r
ON
r.nieuws_id = n.nieuws_id
GROUP BY
n.nieuws_id
LIMIT
:limit
";
$stmt = $this->db->prepare($query);
$stmt->bindParam(':limit', $limit, PDO::PARAM_INT);
$stmt->execute();
$array = $stmt->fetchAll();
return $array;
}
?>
Het nieuws wijzigings formulier is als volgt (nieuws_wijzigen.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
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
<form id="react" action="<?php echo ADMIN_PATH; ?>admin_actions/editNieuwsitem.php" method="post">
<table>
<tr>
<th><label for="kop">Kop:</label></th>
<td><input type="text" id="kop" name="kop" class="text" value="<?php echo $row['kop']; ?>" /></td>
</tr>
<tr>
<th><label for="subkop">Subkop:</label></th>
<td><input type="text" id="subkop" name="subkop" class="text" value="<?php echo $row['subkop']; ?>" /></td>
</tr>
<tr>
<th valign="top" style="padding-top:10px;"><label for="bericht">Bericht:</label></th>
<td><textarea id="bericht" name="bericht" rows="5" cols="35"><?php echo $row['bericht']; ?></textarea></td>
<script type="text/javascript">CKEDITOR.replace('bericht', { toolbar : 'MyToolbar' });</script>
</tr>
</table>
<input type="hidden" name="nieuws_id" value="<?php echo $row['nieuws_id']; ?>" style="display:none;" />
<input type="hidden" name="datum" value="<?php echo $row['datum']; ?>" style="display:none;" />
<br /><br />
<div><input type="submit" class="submit" value="submit" name="submit" /></div>
</form>
<table>
<tr>
<th><label for="kop">Kop:</label></th>
<td><input type="text" id="kop" name="kop" class="text" value="<?php echo $row['kop']; ?>" /></td>
</tr>
<tr>
<th><label for="subkop">Subkop:</label></th>
<td><input type="text" id="subkop" name="subkop" class="text" value="<?php echo $row['subkop']; ?>" /></td>
</tr>
<tr>
<th valign="top" style="padding-top:10px;"><label for="bericht">Bericht:</label></th>
<td><textarea id="bericht" name="bericht" rows="5" cols="35"><?php echo $row['bericht']; ?></textarea></td>
<script type="text/javascript">CKEDITOR.replace('bericht', { toolbar : 'MyToolbar' });</script>
</tr>
</table>
<input type="hidden" name="nieuws_id" value="<?php echo $row['nieuws_id']; ?>" style="display:none;" />
<input type="hidden" name="datum" value="<?php echo $row['datum']; ?>" style="display:none;" />
<br /><br />
<div><input type="submit" class="submit" value="submit" name="submit" /></div>
</form>
Tot slot hier het update script (editNieuwsitem.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
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
<?php
try {
$db = new PDO('mysql:host=localhost;dbname=xx', 'xx', 'xx');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$nieuws_id = $_POST['nieuws_id'];
//var_dump($nieuws_id);
$kop = $_POST['kop'];
$subkop = $_POST['subkop'];
$bericht = $_POST['bericht'];
$stmt = $db->prepare("
UPDATE
nieuws
SET
kop = :kop,
subkop = :subkop,
datum = NOW(),
bericht = :bericht
WHERE
nieuws_id = :nieuws_id
");
$stmt->bindParam(':nieuws_id', $nieuws_id, PDO::PARAM_INT);
$stmt->bindParam(':kop', $kop, PDO::PARAM_STR, 100);
$stmt->bindParam(':subkop', $subkop, PDO::PARAM_STR, 100);
$stmt->bindParam(':bericht', $bericht, PDO::PARAM_STR);
$stmt->execute();
$db = NULL;
header("Location: ../../admin/nieuws");
exit();
}
# catch errors
catch(PDOException $e) {
if(isset($db)) {
$db->rollBack();
}
echo '<pre>';
echo 'Regelnummer: '.$e->getLine().'<br />';
echo 'Bestand: '.$e->getFile().'<br />';
echo 'Foutmelding: '.$e->getMessage().'<br />';
echo '</pre>';
}
?>
try {
$db = new PDO('mysql:host=localhost;dbname=xx', 'xx', 'xx');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$nieuws_id = $_POST['nieuws_id'];
//var_dump($nieuws_id);
$kop = $_POST['kop'];
$subkop = $_POST['subkop'];
$bericht = $_POST['bericht'];
$stmt = $db->prepare("
UPDATE
nieuws
SET
kop = :kop,
subkop = :subkop,
datum = NOW(),
bericht = :bericht
WHERE
nieuws_id = :nieuws_id
");
$stmt->bindParam(':nieuws_id', $nieuws_id, PDO::PARAM_INT);
$stmt->bindParam(':kop', $kop, PDO::PARAM_STR, 100);
$stmt->bindParam(':subkop', $subkop, PDO::PARAM_STR, 100);
$stmt->bindParam(':bericht', $bericht, PDO::PARAM_STR);
$stmt->execute();
$db = NULL;
header("Location: ../../admin/nieuws");
exit();
}
# catch errors
catch(PDOException $e) {
if(isset($db)) {
$db->rollBack();
}
echo '<pre>';
echo 'Regelnummer: '.$e->getLine().'<br />';
echo 'Bestand: '.$e->getFile().'<br />';
echo 'Foutmelding: '.$e->getMessage().'<br />';
echo '</pre>';
}
?>
Gewijzigd op 01/01/1970 01:00:00 door Marco
ik snap eigenlijk niet zo goed wat er nou mis gaat