title: Undefined variable of $error but already defined $error in php
body: <p>I get an error saying </p>
  
  <blockquote>
    <p>Notice: Undefined variable: error in C:\xampp\htdocs\songdb\edit.php
    on line 158</p>
  </blockquote>
  
  <p>I have already tried putting <code>isset()</code> in the <code>$error</code> in the  but it still doesn't work. </p>
  
  <p>edited:
  I already remove != in just use isset($error) but give me this </p>
  
  <p>Parse error: syntax error, unexpected '{' in C:\xampp\htdocs\songdb\edit.php on line 158</p>
  
  <p>edited2:</p>
  
  <p>I have added $error = ''; and use <code>isset($error)</code> but when i click the submit button without typing in all the fields, it works but it didn't display the error message like i wanted it to be.</p>
  
  <p>edited 3:</p>
  
  <p>I have added function renderForm($songid, $title, $artist, $genre, $language, $lyrics, $update) after $connect and $database, after $error = 'ERROR: Please fill in all required fields!'; and after if($row) but it gives me this error</p>
  
  <p>Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\songdb\edit.php on line 218</p>
  
  <p>If there is no solution to this code, can you give me an alternative/another solution of coding for the edit.php file? Thank you very much. To the previous answers from members, Thank you very much for helping me.</p>
  
  <pre><code>&lt;?php
  // connect to the database
  $connect = mysql_connect('localhost','root','');
  $database = mysql_select_db('songdb');
  
  function renderForm($songid, $title, $artist, $genre, $language, $lyrics, $update)
  // check if the form has been submitted. If it has, process the form and  save it to the database
  
  if (isset($_POST['submit'])) {
      // confirm that the 'id' value is a valid integer before getting the form data
      if (is_numeric(isset($_POST['songid']))) {
          // get form data, making sure it is valid
          $error = '';
          $id = $_POST['songid'];
          $title = isset($_POST['title']) ? $_POST['title'] : "";
          $artist = isset($_POST['artist']) ? $_POST['artist'] : "";
          $genre = isset($_POST['genre']) ? $_POST['genre'] : "";
          $language = isset($_POST['language']) ? $_POST['language'] : "";
          $lyrics = isset($_POST['lyrics']) ? $_POST['lyrics'] : "";
          $update = isset($_POST['update']) ? $_POST['update'] : "";
  
          $edit = "UPDATE songs SET    title='.$title.',artist='.$artist.',genre='.$genre.',language='.$language.',lyrics='.$lyrics.',update='.$update.' where songid=$songid";
  
          // check that fields are filled in
  
          if ($title == '' || $artist == '' || $genre == '' || $language == '' || $lyrics == '' || $update == '') {
              // generate error message
              $error = 'ERROR: Please fill in all required fields!';
              renderForm($songid, $title, $artist, $genre, $language, $lyrics, $update);
          } else {
              // save the data to the database
              mysql_query($edit) or die(mysql_error());
              // once saved, redirect back to the view page
              header("Location: view.php");
          }
      }
  } else {
      // if the form hasn't been submitted, get the data from the db and display the form
      // get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)
  
      if (isset($_GET['id']) &amp;&amp; is_numeric($_GET['id']) &amp;&amp; $_GET['id'] &gt; 0) {
          // query db
          $songid = $_GET['songid'];
          $result = mysql_query("SELECT * FROM songs WHERE songid=$songid")
          or die(mysql_error());
          $row = mysql_fetch_array($result);
          // check that the 'id' matches up with a row in the databse
  
          if($row) {
              // get data from db
              $title = $row['title'];
              $artist = $row['artist'];
              $genre = $row['genre'];
              $language = $row['language'];
              $lyrics = $row['lyrics'];
              $update = $row['update'];
  
              renderForm($songid, $title, $artist, $genre, $language, $lyrics, $update);
          } else {
              // if no match, display result
              echo "No results!";
          }
      } else {
          // if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error
      }
  }
  ?&gt;
  
   &lt;html&gt;
       &lt;head&gt;
           &lt;title&gt;Edit Record&lt;/title&gt;
       &lt;/head&gt;
       &lt;body&gt;
       &lt;?php
       if (isset($error)) {
           echo '&lt;div style="padding:4px; border:1px solid red;      color:red;"&gt;'.$error.'&lt;/div&gt;';
       }
       ?&gt;
           &lt;form action="edit.php" method="post"&gt;
               &lt;input type="hidden" name="id" value="&lt;?php echo $id; ?&gt;"/&gt;
               &lt;table style="margin-left:auto; margin-right:auto; width:400px;"&gt;
                   &lt;tbody&gt;
                       &lt;tr style="text-align:center"&gt;
                           &lt;td colspan="2"&gt;&lt;h2 style="color:#00008b;"&gt;Edit song into Music   Database&lt;/h2&gt;&lt;label style="color:#FF0000;"&gt;&lt;/label&gt;&lt;/td&gt;
                       &lt;/tr&gt;
  
                       &lt;tr&gt;
                           &lt;td&gt;Title&lt;label style="color:#FF0000;"&gt;&lt;/label&gt;&lt;/td&gt;
                           &lt;td&gt;&lt;input type="text" name="title"&gt;&lt;/td&gt;
                       &lt;/tr&gt;
                       &lt;tr&gt;
                           &lt;td&gt;Artist&lt;label style="color:#FF0000;"&gt;&lt;/label&gt;&lt;/td&gt;
                           &lt;td&gt;&lt;input type="text" name="artist"&gt;&lt;/td&gt;
                       &lt;/tr&gt;
                       &lt;tr&gt;
                           &lt;td&gt;Genre&lt;label style="color:#FF0000;"&gt;&lt;/label&gt;&lt;/td&gt;
                           &lt;td&gt;&lt;input type="text" name="genre"&gt;&lt;/td&gt;
                       &lt;/tr&gt;
                       &lt;tr&gt;
                           &lt;td&gt;Language&lt;label style="#FF0000;"&gt;&lt;/label&gt;&lt;/td&gt;
                           &lt;td&gt;&lt;input type="text" name="language"&gt;&lt;/td&gt;
                       &lt;/tr&gt;
                       &lt;tr&gt;
                            &lt;td&gt;Lyrics: &lt;label style="#FF0000;"&gt;&lt;/label&gt;&lt;/td&gt;
                            &lt;td&gt;&lt;textarea name="lyrics" rows="5" cols="50"&gt;&lt;/textarea&gt;&lt;/td&gt;
                        &lt;/tr&gt;
                        &lt;tr&gt;
                            &lt;td&gt;Updated by&lt;label style="#FF0000;"&gt;&lt;/label&gt;&lt;/td&gt;
                            &lt;td&gt;&lt;input type="text" name="update"&gt;&lt;/td&gt;
                        &lt;/tr&gt;
                        &lt;tr style="text-align:center"&gt;
                            &lt;td colspan="2"&gt;&lt;input type="submit" name="submit" value="Submit"&gt;&lt;/td&gt;
                        &lt;/tr&gt;
                    &lt;/tbody&gt;
                &lt;/table&gt;
            &lt;/form&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>