All pastes #729319 Raw Edit

asles

public php v1 · immutable
·published 2007-10-07 21:16 UTC
rendered paste body
<h1>Oppdater artikkel</h1><?phpinclude('db.php');// Vi sjekker om artikkel id er blitt postet, samt om det er ett nummer..if (isset($_POST['articleId']) && is_numeric($_POST['articleId'])){	// Setter data fra input til lokale variabler	$title = isset($_POST['title']) ? $_POST['title'] : null;	$article = isset($_POST['article']) ? $_POST['article'] : null;	$categoryId = isset($_POST['categoryId']) ? $_POST['categoryId'] : null;	$articleId = isset($_POST['articleId']) ? $_POST['articleId'] : null;	// Sjekker at vi fikk inn variabelene vi trengte	if ($title == null || $article == null) {		die("Fikk ikke data på tittel og article!");	}		// Skal du sjekke om du får noe data?	if (trim($title) == "" || trim($article) == "") {		die("Tittel eller article var blanke.. Det kan ikke være riktig?");	}	// Sjekker at vi fikk categoryid, samt at det er ett tall	if ($categoryId == null || !is_numeric($categoryId)) {		die("category id er ikke satt eller er ikke ett tall..");	}	// OBS OBS : Her kan det ekstremt enkelt gøres en SQL injection!!	//$mysqli->query("UPDATE articles SET title = '$title' article ='$article' categoryId ='$categoryId' WHERE articleId ='$articleId'");	// Vi legger til addslashes for å kommentere ut evt, SQL injection...	$updateSQL = 'UPDATE articles SET title = "' . addslashes($title) . '" article = "' . addslashes($article) . '" categoryId = ' . $categoryId . ' WHERE articleId = ' . $articleId . ';';	echo "DEBUG TIL GLENN: " . $updateSQL;			// Så "kjører" vi SQL koden...	if (!$mysqli->query($updateSQL)) {		echo "Noe gikk galt...";		echo $mysqli->error;	} else {		echo 'Artikkelen er oppdatert i databasen';	}}else {	?><form method="POST" action="index.php?side=rediger_artikkel"><table border="1"><?php$selectQuery = 'SELECT articles.title as atitle, articles.article as barticle, categories.title as ctitle, articles.categoryId as categoryId FROM articles, categories WHERE articles.articleId='.$_GET['articleId']. ' AND articles.categoryId = categories.categoryId';$result = $mysqli->query($selectQuery);while ($row = $result->fetch_array(MYSQLI_ASSOC)) {	$categoryId = $row["categoryId"];	?>	<tr>		<td>Tittel:</td>		<td><input type="hidden" name="articleId"			value="<?= $_GET['articleId'] ?>" /> <input type="text"			value="<?= $row['atitle'] ?>" name="title" size="30" /></td>	</tr>	<tr>		<td>Tekst:</td>		<td><textarea rows="20" cols="30" name="article" cols="20"><?= $row['barticle'] ?></textarea></td>	</tr>	<tr>		<td>Kategori:</td>		<td><select name="categoryId">		<?php		$selectQuery = 'SELECT categoryId,title FROM categories';		$result = $mysqli->query($selectQuery);		while ($row = $result->fetch_array(MYSQLI_ASSOC)) {			echo '<option value="'.$row['categoryId'].'" ' . ($row['categoryId'] == $categoryId ? 'selected="selected"' : '') . '>'.$row['title'].'</option>';		}		?>		</select></td>	</tr>	<tr>		<td><input type="submit" value="oppdater artikkel"></td>		<td></td>	</tr></table></form>		<?php}}?>