Hello;

Here is the setup:

I am trying to populate a form with default field sets loaded with variables from a previous page. I have placed echo statements into my code to verify that the variables are coming accross correctly. The variable are being loaded from a MySql table read. The result is being parsed into the individual variables, echoed out to verify that they are working, then, I am creating a form inside of a table for formatting, and trying to populate the value fields with the variables. Here is the sample of what the results are, followed by the actual code:

Make your CHANGES in the form below:

itsnotmine.mp3

song is about not owning up

yeah

*the above text is the echo out of the variables to prove they are coming thru

This is what is displayed on the form******

itsnotmine.mp3     Song Name
song                      Song Description
                              Song Lyrics

Notice there is nothing displayed in the Song Lyrics Text Area, and only
One word displayed in the Song Description Text Box

Here is the code from the page:

<?php
echo 'Make your <B>CHANGES</b> in the form below:<br><br>';
$change = $_SESSION['sedit'];
$query = "SELECT `name` , `description`, `lyrics` FROM `songs` WHERE `name`LIKE  '$change' " ;
$result = mysql_query($query);
$songarray = mysql_fetch_array($result, MYSQL_ASSOC);
$changename = $songarray['name'];
$changedesc = $songarray['description'];
$changelyric = $songarray['lyrics'];
echo $changename . '<br>';
echo $changedesc . '<br>';
echo $changelyric . '<br>';
echo '<table width="100%">';
echo '<tr><td><form method="post" action="song_change_update.php"></td></tr>';
echo '<tr><td><input type="text" name="songname" value=' . $changename . '>Song Name</td></tr>';
echo '<tr><td><input type="text" name="songdesc" value='.$changedesc.'>Song Description</td></tr>';
echo '<tr><td><textarea name="songlyrics" rows="20" cols="40" value=' . $changelyric . '></textarea>Song Lyrics</td></tr>';
//echo '<textarea name="" rows="" cols=""></textarea> ';
echo '<tr><td></form></td></tr>';
echo '</table>';
?>

So, I do not understand why the full content of the variables are not being filled into the form fields. Can anyone please help me?

Thank you
Ice

    $change = $_SESSION['sedit'];
    
    $result = mysql_query("SELECT name , description, lyrics FROM songs WHERE name LIKE '$change'") or exit(mysql_error());
    $songarray = mysql_fetch_assoc($result);
    
    foreach ($songarray as $key => $value)
    {
    	$$key = htmlspecialchars($value);
    }
    
    echo 'Make your <B>CHANGES</b> in the form below:<br><br>' . $name . '<br>' . $description . '<br>' . $lyrics . '<br>
    <form method="post" action="song_change_update.php">
    <table width="100%">
    <tr><td>Song Name: <input type="text" name="songname" value="' . $name . '"></td></tr>
    <tr><td>Song Description: <input type="text" name="songdesc" value="' . $description . '"></td></tr>
    <tr><td>Song Lyrics: <textarea name="songlyrics" rows="20" cols="40">' . $lyrics . '</textarea></td></tr>
    </table>
    </form>
    ';
    

      By golly, That worked! Thank you very much. Now, I can study what you changed, and come to an understanding of what I can do differently from now on for things. I didnt realize also, that I could just do the single echo, and it wouldnt close out until the semicolon was reached. Now I do understand that, but also the assigning the table fields to the variable using the 'for each' statement. I am still learning to code. Thank you, I will change my coding in the other places to make it more efficient as well.

      Ice

        Ok, now that I have the form working correctly, I cannt seem to get my update script working right. Here is the script to update the database, that is not working:

        <?php
        include 'config.php';
        echo 'Song Change Update script';
        $field1=$_POST['songname'];
        $field2=$_POST['songdesc'];
        $field3=$_POST['songlyrics'];
        
        //Now we need to update the song table with the new info
        $sql = "UPDATE songs SET (sid, name, description, lyrics) VALUES (NULL, $field1, $field2, $field3) WHERE name == $field1";
        mysql_query($sql);
        ?>

        The result is that nothing happens with the database.

        Am I using a wrong syntax, or?

        Thank you;
        Ice

          include('config.php');
          
          foreach ($_POST as $key => $value)
          {
          	$value = trim($value);
          	if ($value != '') {$value = "'" . mysql_real_escape_string($value) . "'";} else {$value = 'NULL';}
          	$$key = $value;
          }
          
          $sql = "UPDATE songs SET (name, description, lyrics) VALUES ($songname, $songdesc, $songlyrics) WHERE name = $songname";
          $result = mysql_query($sql) or exit(mysql_error());
          echo 'UPDATE OK';
          

            Ok, I observed what you changed it to, tried to understand why, I guess you can use the same kinda format for this as for the last, however, when I changed the old script to this, I got this error message:

            You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(name, description, lyrics) VALUES ('molly.mp3', 'hatchet wings', 'too many trie' at line 1

            I am going to look at it, as I copied and pasted from what you wrote, I will look for the error.

            Ice

              Still getting the same message, and I have tried all different ways of working it.

              Ice

                OK, I had to make some 'changes' before it would work correctly. First, I had to add in the 'sid' autonumber identifier field, as, obviously, the name of the song might change, and, thusly, never equal out to the old name. Secondly, there was some differences in syntax required to make the update query. Here is the working script:

                <?php
                include('config.php');
                
                foreach ($_POST as $key => $value)
                {
                    $value = trim($value);
                    if ($value != '') {$value = "'" . mysql_real_escape_string($value) . "'";}
                     else {$value = 'NULL';}
                    $$key = $value;
                }
                echo $songid . '<br>';
                echo $songname . '<br>';
                echo $songdesc . '<br>';
                echo $songlyrics . '<br>';
                $sql = "UPDATE songs SET name=$songname, description=$songdesc, lyrics=$songlyrics WHERE sid=$songid";
                $result = mysql_query($sql) or exit(mysql_error());
                echo 'UPDATE OK'; 
                ?>
                  9 days later

                  NOTE: For those that might happen upon this thread, find out in this thread why you should NOT use the foreach() code above to handle incoming variables.

                    Write a Reply...