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:
<?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:
<?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;
}
?>
Het nieuws wijzigings formulier is als volgt (nieuws_wijzigen.php):
<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>
Tot slot hier het update script (editNieuwsitem.php):
<?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>';
}
?>