Please help me, I've been fiddling with this codes for hours now, I can't see what's wrong with it. It always give me an invalid foreach() argument crap.
<?php
$page_title = "Edit a User";
include('includes/header.html');
echo '<h1>Edit a Post</h1>';
if((isset($_GET['id'])) && (is_numeric($_GET['id']))){
$id = $_GET['id'];
}elseif ((isset($_POST['id'])) && (is_numeric($_POST['id']))){
$id = $_POST['id'];
}else{
echo '<p class="error">This page has been accessed in error.</p>';
include('includes/footer.html');
exit();
}
require_once('../mysqli_connect.php');
if(isset($_POST['submitted'])){
$errors = array();
if(empty($_POST['subject'])){
$errors = "You forgot to enter a subject.";
}else{
$s = mysqli_real_escape_string($dbc, trim($_POST['subject']));
}
if(empty($_POST['body'])){
$errors = "You forgot to enter a body.";
}else{
$b = mysqli_real_escape_string($dbc, trim($_POST['body']));
}
if(empty($errors)){
$q = "UPDATE news SET subject='$s', body='$b' WHERE news_id=$id LIMIT 1";
$r = @mysqli_query($dbc, $q);
if(mysqli_affected_rows($dbc)==1){
echo '<p>The post has been edited.</p>';
}else{
echo '<p class="error">The post could not be edited due to a system error. Please contact the webmaster.</p>';
echo '<p>'.mysqli_error($dbc).'<br />Query: '.$q.'</p>';
}
$errors = array();
}else{
echo '<p class="error">The following error(s) occurred:<br />';
foreach($errors as $msg){
echo " - $msg<br />\n";
}
echo '</p><p>Please try again.</p>';
}
}
$q = "SELECT subject, body FROM news WHERE news_id=$id";
$r = @mysqli_query($dbc, $q);
if(mysqli_num_rows($r)==1){
$row = mysqli_fetch_array($r, MYSQLI_NUM);
echo '
<form action="edit_post.php" method="POST">
<fieldset id="npost">
<p><input id="newpost_subject" type="text" name="subject" maxlength="60" value="'.$row[0].'" /></p>
<p><textarea name="body" rows="10" cols="30" id="newpost_body">'.$row[1].'</textarea></p>
<p><input type="submit" name="submit" value="Post" id="newpost_submit" /></p>
<input type="hidden" name="submitted" value="TRUE" />
<input type="hidden" name="id" value="'.$id.'" />
</fieldset>
</form>
';
}else{
echo '<p class="error">This page has been accessed in error.</p>';
}
mysqli_close($dbc);
include('includes/footer.html');
?>