Updatere database
Jeg er ved at prøve at lave en opdatering fra flere sider i en kode, jeg har bygget hjemmesiden op i functions, hvor menupunkterne 2, 3, 4, 5 og 6 har under punkter.Hvert overpunkt er tildelt variablen $id, eks:
Forside ($id=1)
Kode:
function home($id,$idx,$action) {
$resultat = mysql_query("SELECT title,content FROM content WHERE id='$id'");
$row = mysql_fetch_array($resultat);
$body = $row['content'];
$title = $row['title'];
?>
<div id="content">
<?php echo $title; ?>
<?php if($action==succes) { ?> <h1>done</h1> <?php } ?>
<?php if($action==error) { ?> <h1>error</h1> <?php } ?>
<form action="./index.php?id=1&action=update" method="post">
<textarea id="elm1" name="content" rows="20" cols="80" style="width: 425px"><?php echo $body; ?></textarea>
<input type="submit" value="Opdatere" />
</form>
</div>
<?php
}
hovedpunkt 2 med undermenu ($id=1&idx=1) hvor idx variablen er sat til undermenupunkterne.
Kode:
function about($id,$idx,$action) {
if(!isset($idx)) {
$resultat = mysql_query("SELECT title,content FROM content WHERE idx='$id'");
} else {
$resultat = mysql_query("SELECT title,content FROM about WHERE id='$id'");
}
?>
<ul id="navilist">
<li><a href="./?id=3&idx=1">Børnehaven i hverdagen<img src="../images/gren.png" class="branch" width="187" height="30" alt="gren" /></a></li>
<li><a href="./?id=3&idx=2">Madpolitik<img src="../images/gren.png" class="branch" width="187" height="30" alt="gren" /></a></li>
<li><a href="./?id=3&idx=3">Årsplan<img src="../images/gren.png" class="branch" width="187" height="30" alt="gren" /></a></li>
<li><a href="./?id=3&idx=4">Virksomhedsplan<img src="../images/gren.png" class="branch" width="187" height="30" alt="gren" /></a></li>
<li><a href="./?id=3&idx=5">Sprogstimulering<img src="../images/gren.png" class="branch" width="187" height="30" alt="gren" /></a></li>
<li><a href="./?id=3&idx=6">Traditioner<img src="../images/gren.png" class="branch" width="187" height="30" alt="gren" /></a></li>
<li><a href="./?id=3&idx=7">Inklusion (sårbare børn)<img src="../images/gren.png" class="branch" width="187" height="30" alt="gren" /></a></li>
</ul>
<div id="content">
<?php echo $header; ?>
<?php if($action==success) { ?> <h1>done</h1> <?php } ?>
<?php if($action==error) { ?> <h1>error</h1> <?php } ?>
<form action="./index.php?action=update" method="post">
<textarea id="elm1" name="content" rows="20" cols="80" style="width: 425px"><?php echo $body; ?></textarea>
<input type="submit" value="Opdatere" />
</form>
</div>
<?php
}
Det jeg så gerne ville var at høre én updatere funktion til disse. Jeg prøvede at lave et array som alt sammen ligger ovenfor dette, her er min kode:
$dbtables[1]="content";
$dbtables[2]="about";
$dbtables[3]="everyday";
$dbtables[4]="staff";
$dbtables[5]="parrents";
$dbtables[6]="official";
if($id==1&$action==update) {
$content = trim($_POST['content']);
if (empty($content)) {
header("location:index.php?id=1&action=error");
exit();
} else {
$insert="update '$dbtables_array[$id]' set content = '$content' where id='$id'" or die("Connection to table failed : " . mysql_error());
mysql_query($insert);
header("location:index.php?id=1&action=succes");
exit();
}
}
Mit spørgsmål er så hvordan får jeg min update hvor tabelnavnet er dynamisk og bestemmes afhængig af hvilken function den bliver sendt fra. Jeg prøvede at lave det i et array, men kan ikke rigtig få det til at virke. Kom frisk, kom frejdigt =)
