-2

I get an error saying

Notice: Undefined variable: error in C:\xampp\htdocs\songdb\edit.php on line 158

I have already tried putting isset() in the $error in the but it still doesn't work.

edited: I already remove != in just use isset($error) but give me this

Parse error: syntax error, unexpected '{' in C:\xampp\htdocs\songdb\edit.php on line 158

edited2:

I have added $error = ''; and use isset($error) 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.

edited 3:

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

Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\songdb\edit.php on line 218

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.

<?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']) && is_numeric($_GET['id']) && $_GET['id'] > 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
    }
}
?>

 <html>
     <head>
         <title>Edit Record</title>
     </head>
     <body>
     <?php
     if (isset($error)) {
         echo '<div style="padding:4px; border:1px solid red;      color:red;">'.$error.'</div>';
     }
     ?>
         <form action="edit.php" method="post">
             <input type="hidden" name="id" value="<?php echo $id; ?>"/>
             <table style="margin-left:auto; margin-right:auto; width:400px;">
                 <tbody>
                     <tr style="text-align:center">
                         <td colspan="2"><h2 style="color:#00008b;">Edit song into Music   Database</h2><label style="color:#FF0000;"></label></td>
                     </tr>

                     <tr>
                         <td>Title<label style="color:#FF0000;"></label></td>
                         <td><input type="text" name="title"></td>
                     </tr>
                     <tr>
                         <td>Artist<label style="color:#FF0000;"></label></td>
                         <td><input type="text" name="artist"></td>
                     </tr>
                     <tr>
                         <td>Genre<label style="color:#FF0000;"></label></td>
                         <td><input type="text" name="genre"></td>
                     </tr>
                     <tr>
                         <td>Language<label style="#FF0000;"></label></td>
                         <td><input type="text" name="language"></td>
                     </tr>
                     <tr>
                          <td>Lyrics: <label style="#FF0000;"></label></td>
                          <td><textarea name="lyrics" rows="5" cols="50"></textarea></td>
                      </tr>
                      <tr>
                          <td>Updated by<label style="#FF0000;"></label></td>
                          <td><input type="text" name="update"></td>
                      </tr>
                      <tr style="text-align:center">
                          <td colspan="2"><input type="submit" name="submit" value="Submit"></td>
                      </tr>
                  </tbody>
              </table>
          </form>
    </body>
</html>
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Hazirah_Halim
  • 87
  • 2
  • 16

3 Answers3

4

just use isset($error) (remove != in line 158)

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
1

I think You need define $error = '' befor $id = $_POST['songid'];.

Something like this:-

if (is_numeric(isset($_POST['songid'])))

 {

    // get form data, making sure it is valid
    $error = '';
    $id = $_POST['songid'];

   ......
   ......

And Also Change.

if(!empty($error))
 {
    echo '<div style="padding:4px; border:1px solid red;      color:red;">'.$error.'</div>';

 }
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Pravin Vavadiya
  • 3,195
  • 1
  • 17
  • 34
1

you are getting error because PHP does not getting variable ($error), so it shows undefined variable.

so define at the starting of script.

$error='';

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Rakyesh Kadadas
  • 851
  • 9
  • 11