Author Topic: Issue with ForEach on Post Add Page for Blog  (Read 151 times)

Steven C

  • Guest
Issue with ForEach on Post Add Page for Blog
« on: September 06, 2010, 10:32:21 AM »
Hey everyone,
I am beginning with PHP and slowly trying to fumble my way through this.  I am working on building just a basic blog function, everything I have seems to be fine except for the page used to add posts.  There seems to be an issue with my foreach on line 38 but for the life of me I can figure out.  Can any one help?
 
PHP Code: [Select]
 
<?php
if(empty($_POST)) {
 
$status 'To add a new post, fill out the form below.  Click the Add Post button once.';
 }
 else {
  
$title $_POST['title'] ;
  
$summary $_POST['summary'] ;
  
$content $_POST['content'] ;
  
  
$error_list = array();
  
  if(empty(
$title)) {
   
$error_list[] = 'You did not supply a Title';
  }
  if(empty(
$summary)) {
   
$error_list[] = 'You did not supply a Summary';
  }
  if(empty(
$content)) {
   
$error_list[] = 'You did not supply any Content';
  }
  
  if(empty(
$error_list)) {
   
mysql_connect('test','test','test') or die ("could not connect to mysql");
   
mysql_select_db('test') or die ("no database");
   
   
$sql "INSERT INTO blog_posts ";
   
$sql "SET title='$title', date_posted=NOW(), summary= '$summary', content='$content' ";
   
   if(
mysql_query($sql)) {
    
header('Location: index.php');
   }
  }
  else {
   
$status '<ul>'
   
   
foreach($error_list as $error_message) {
    
$status .= "<li>$error_list</li>" ;
   
   
$status .= '</ul>' 
   
  }
 }
?>

Smokey PHP

  • Web Developer
  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 506
  • Karma: +4/-0
    • View Profile
Re: Issue with ForEach on Post Add Page for Blog
« Reply #1 on: September 08, 2010, 05:19:52 PM »
Hi there, if you haven't figured it out already, I believe I've sorted you out below (you'll kick yourself!)

End of your script:
Quote
if(mysql_query($sql)) {
    header('Location: index.php');
   }
  }
  else {
   $status = '<ul>'
   
   foreach($error_list as $error_message) {
    $status .= "<li>$error_list</li>" ;
   
   $status .= '</ul>' ;
   
  }
 }
?>

*new* end of your script:
PHP Code: [Select]

if(mysql_query($sql)) {
    
header('Location: index.php');
   }
  }
  else {
   
$status '<ul>';
   
   foreach(
$error_list as $error_message) {
    
$status .= "<li>$error_list</li>" ;
   
   
$status .= '</ul>' 
   
  }
 }
?>


You were missing a semi-colon on the line above your foreach! :-/.