Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to create dynamic form and populated it with data from mysql?

Status
Not open for further replies.

keith23

Technical User
May 26, 2005
97
NL
Hi all . i wonder how i can creating a dynamic form by pulling its data such as songid,songname,album from mysql db and creating a form like below. For each record in db one row in the form.I wrote part of the code but i do not know how to print the form with right information on it.The bold parts are the dynamic values and assume all data are in one table.Could any expert help me wth this part.Thanks
formdynamic.jpg



dynamic form code:
Code:
<?
$username="username";
$password="password";
$database="your_database";



mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "select songno,songname,songalbum from musictable)";
mysql_query($query);

mysql_close();
?>


part of the code that for form .The bold part are pulled from db.
Code:
<tr>
                        <td bgcolor="#C0C0C0"><p align="center"><b>1</b></p>
                        </td>
                        <td bgcolor="#C0C0C0"><input
                        type="checkbox" name="Id" value="[B]1[/B]"></td>
                        <td bgcolor="#C0C0C0"><a
                        href="javascript:newWindow('./player/ram4.php?sid=[B]1[/B]')"><img
                        src="images/Speaker.gif"
                        alt="Listen to this Song" border="0"
                        width="16" height="16"
                        longdesc="Listen to this Song"></a>&nbsp;[B]Atash[/B]
                        </td>
                        <td bgcolor="#C0C0C0">[B]Atish[/B]</td>
                        <td bgcolor="#C0C0C0"><a
                        href="javascript:newWindow('RateSong.asp?SongID=[B]1[/B]')"><img
                        src="images/RatIt.gif"
                        alt="Rate This Song" border="0"
                        width="100" height="22"
                        longdesc="Rate This Song"></a></td>
                        <td align="center" bgcolor="#C0C0C0">0&nbsp;</td>
                        <td bgcolor="#C0C0C0">coming Soon <!-- <a href="../site.php?songID=[B]1[/B]&Song=[B]Atash[/B]">Read </a>| <a href="../Members/WriteLyrics.php?SongId=[B]1[/B]&Song=[B]Atash[/B]">Write</a> --> </td>
 
look at the php manual. look up using mysql_fetch_row or mysql_fetch_array or mysql_fetch_assoc

each of those items has exactly what you are looking for in the manual.
 
guelphdad many thanks to u i wrote the script but do not know how to place the html code for the form control(checkbox) i get errors!! could u tell me how to place the html inside the while loop?thanks
Code:
<?
.....
......
$query  = "SELECT id,filename, artist, album,title FROM wimpy";
$result = mysql_query($query);

while($row = mysql_fetch_assoc($result))
{
    echo "Id : {$row['id']} <br>" .
         "filename :{$row['filename']} <br>" .
         "artist : {$row['artist']} <br>" . 
         "album : {$row['album']} <br>" .
         "title : {$row['title']} <br><br>";
 
//error below here==> error
echo "
<input
                        type="checkbox" name="Id" value=[b]"{$row['id']}[/b]"></td>
                        <td bgcolor="#C0C0C0"><a
                        href="javascript:newWindow('./player/ram4.php?sid=[b]{$row['id']}[/b]')"><img
                        src="images/Speaker.gif"
                        alt="Listen to this Song" border="0"
                        width="16" height="16"
                        longdesc="Listen to this Song"></a>&nbsp;[b]{$row['title'][/b]}                        </td>
                        <td bgcolor="#C0C0C0">[b]{$row['title']}[/b]</td>
                        <td bgcolor="#C0C0C0"><a
                        href="javascript:newWindow('RateSong.asp?SongID=[b]{$row['id']}[/b]')"><img
                        src="images/RatIt.gif"
                        alt="Rate This Song" border="0"
                        width="100" height="22"
                        longdesc="Rate This Song"></a></td>
                        <td align="center" bgcolor="#C0C0C0">0&nbsp;</td>
                        <td bgcolor="#C0C0C0">coming Soon <!-- <a href="../site.php?songID=1&Song=Atash">Read </a>| <a href="../Members/WriteLyrics.php?SongId=1&Song=Atash">Write</a> --> </td> [b]";[/b]

} 


?>
 
One thing I notice is that you use double quotes to begin and end your string, but then you also have them inside the string. When you start a string with a double quote, PHP looks for the next double quote to end the string. You either need to use single quotes within the string, or you need to escape the double quotes within the string by using the forward slash before the double quotes; such as:
Code:
echo "<input type=\"checkbox\" name=\"Id\" value=\"{$row['id']}\">";
This will not cause a parsing error.
 

Thanks for u reply . I get in to problem puting the checkbox value. It shows impty when i run it . I use the same <?=strval( $row['Id'] );?> eles where in same below code and it worked. Could u help me fix this ?Thanks

problem in bold part no value!!!
Code:
while($row = mysql_fetch_assoc($result))
{
    echo "<br>Id : {$row['id']} <br>" .
         "filename :{$row['filename']} <br>" .
         "artist : {$row['artist']} <br>" . 
         "album : {$row['album']} <br>" .
         "title : {$row['title']} <br><br>";
 

?>
<tr>
                        <td bgcolor="#C0C0C0"><p align="center"><b><?=strval( $row['album'] );?></b></p>
                        </td>
                        <td bgcolor="#C0C0C0"><input
                        type="checkbox" name="Id" [b]value=" <?=strval( $row['Id'] );?> ">[/b]</td>
                        <td bgcolor="#C0C0C0"><a
                        href="javascript:newWindow('./mp3s/myWimpy.php?queryWhere=Id&queryValue=<?=strval( $row['id'] );?>')"><img
                        src="images/Speaker.gif"
                        alt="Listen to this Song" border="0"
                        width="16" height="16"
                        longdesc="Listen to this Song"></a>&nbsp;<?=strval( $row['album'] );?>
                        </td>
                        <td bgcolor="#C0C0C0"><?=strval( $row['album'] );?></td>
                        <td bgcolor="#C0C0C0"><a
                        href="javascript:newWindow('RateSong.asp?SongID=1')"><img
                        src="images/RatIt.gif"
                        alt="Rate This Song" border="0"
                        width="100" height="22"
                        longdesc="Rate This Song"></a></td>
                        <td align="center" bgcolor="#C0C0C0">0&nbsp;</td>
                        <td bgcolor="#C0C0C0">coming Soon <!-- <a href="../site.php?songID=1&Song=Atash">Read </a>| <a href="../Members/WriteLyrics.php?SongId=1&Song=Atash">Write</a> --> </td>

<?


}
 
In the checkbox code you have: $row['Id']; everywhere else you use $row['id'].

The 'i' in id is capitalized in the checkbox code and not in others. Also there is no reason to use strval(). Everything but the id is already a string, and the id I assume is an integer, which is perfectly fine to print.
 
As a general rule you should always develop with error reporting set to E_ALL, and display_errors set to 1 (or true). This will cause PHP to display all errors, warnings, and notices that occur while a script runs. This is extremely beneficial in tracking down bugs, and problems. In the last situation PHP would have notified you that a $row['Id'], was not defined, by issuing a notice.

Both error_reporting, and display_errors can be found in the php.ini file and set there if you are working on a development machine. If you are working on a machine that has live code running on it, then you can set these values just for the scripts your developing, by using the function set_ini(), in you scripts.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top