Problem with foreach loop

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8415
  • Loc: USA

Post 3+ Months Ago

I have the following piece of code to create a database...
PHP Code: [ Select ]
$dname = $_POST['dname'];
$duser = $_POST['duser'];
$dhost = $_POST['dhost'];
$dpass = $_POST['dpass'];
 
$db = new mysql();
 
if(!isset($dname) || !isset($duser) || !isset($dhost) || !isset($dpass))
{
    $this->error['database'][] = 'One of the fields was not filled in. All of them are required';
}
 
if(strlen($dname) > 10)
{
    $this->error['database'][] = 'The database name is too long. We don\'t recommend it to be greater then 10';
}
 
$errors = count($this->error['database']);
 
if($errors > 0)
{
    $db->select_db($dname, $duser, $dhost, $dpass);
    $sql = 'CREATE DATABASE '. $dname;
    $process = mysql_query($sql) or die(mysql_error());
    mysql_close();
    if($process)
    {
        echo <<<EOD
<p>The database was created successfully.<br />
<a href="install.php?page=config">Continue on to the next stage of the installation</a></p>
EOD;
    }
}
else
{
    echo "<ol>";
    foreach($this->error['database'] as $value) // Line 44
    {
        echo "<li>{$value}</li>\n";
    }
    echo "</ol>";
}
  1. $dname = $_POST['dname'];
  2. $duser = $_POST['duser'];
  3. $dhost = $_POST['dhost'];
  4. $dpass = $_POST['dpass'];
  5.  
  6. $db = new mysql();
  7.  
  8. if(!isset($dname) || !isset($duser) || !isset($dhost) || !isset($dpass))
  9. {
  10.     $this->error['database'][] = 'One of the fields was not filled in. All of them are required';
  11. }
  12.  
  13. if(strlen($dname) > 10)
  14. {
  15.     $this->error['database'][] = 'The database name is too long. We don\'t recommend it to be greater then 10';
  16. }
  17.  
  18. $errors = count($this->error['database']);
  19.  
  20. if($errors > 0)
  21. {
  22.     $db->select_db($dname, $duser, $dhost, $dpass);
  23.     $sql = 'CREATE DATABASE '. $dname;
  24.     $process = mysql_query($sql) or die(mysql_error());
  25.     mysql_close();
  26.     if($process)
  27.     {
  28.         echo <<<EOD
  29. <p>The database was created successfully.<br />
  30. <a href="install.php?page=config">Continue on to the next stage of the installation</a></p>
  31. EOD;
  32.     }
  33. }
  34. else
  35. {
  36.     echo "<ol>";
  37.     foreach($this->error['database'] as $value) // Line 44
  38.     {
  39.         echo "<li>{$value}</li>\n";
  40.     }
  41.     echo "</ol>";
  42. }

(It's included into a class so I know about the $this->error; thing... it works), the problem with this is that apparently, I'm using the foreach loop wrong. Here is the error that I'm getting.
Quote:
Warning: Invalid argument supplied for foreach() in C:\wamp\www\boxed\Boxed\install\stages\p_database.php on line 44

I've tried removing the $this->error['database'] and simply using it as a simple array in the process, but it still gives me that error.

I'm out of ideas at the moment. Any help towards the solution to this problem would be greatly appreciated. Thanks in advance :)

[EDIT:] It actually could be the $this->error['database'][]. Because I tried to print it, and the array are not set...
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8415
  • Loc: USA

Post 3+ Months Ago

Nevermind... fixed it... I was using wrong functions for checking if the user filled in the blanks :roll:

Geez, it took me too long to find the damn problem. Hate it.
  • alex89
  • Bronze Member
  • Bronze Member
  • User avatar
  • Posts: 239
  • Loc: Western Australia

Post 3+ Months Ago

Only 1 hour :)
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8415
  • Loc: USA

Post 3+ Months Ago

That's too long if all you did was trying to find this problem and the problem turned out to be that I'm using a wrong function

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 98 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.