...

View Full Version : Keep Submitted Values In A Form



sonofjack
12-06-2010, 01:07 PM
I've looked in various forums for this and tried different things but I can't seem to get it working.

I have a form where you select a name from a drop down list. After pressing submit the page goes back to the form and I want the values to be retained instead of it saying 'Select One' again.

My form code is:

<php




<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p>Enter the new track:<br />
<textarea name="tracktitle" rows="1" cols="20">
</textarea></p>
<p>Composer:
<select name="composerfield" size="1">
<option selected value="">Select One</option>
<option value="">---------</option>
<?php
while ($addcomposer= mysql_fetch_array($composers)) {
$cid = $addcomposer['composerid'];
$cname = htmlspecialchars($addcomposer['composername']);
echo "<option value='$cid'>$cname</option>\n";
}
?>
</select></p>

<p>Album:
<select name="albumid" size="1">
<option selected value="">Select One</option>
<option value="">---------</option>
<?php
while ($addalbum = mysql_fetch_array($albums)) {
$aid = $addalbum['albumid'];
$aname = htmlspecialchars($addalbum['albumname']);
echo "<option value='$aid'>$aname</option>\n";
}
?>
</select></p>

<input type="submit" value="SUBMIT" />
</form>



Thank you in advance

mlseim
12-06-2010, 01:27 PM
Instead of going back to itself to process the form,

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

Go to another script that writes a couple of cookies ... saving the form data in cookies.
(you could also use SESSION).

Then, that script sends it back to the form page. The transaction of that will be
invisible to the user, but it will handle the header needed to save cookies.

The form page (at the top) will read any cookies that are saved ... which will be
your form values. Those can be read on any other script you have.

sonofjack
12-06-2010, 03:37 PM
Thanks for the reply.

Do you mean something along these lines?



<form action="formsubmit.php" method="post">


and then in formsubmit.php



<?php

session_start();
$_SESSION['composerfield'] = "Saving values";
session_write_close();
header('Location: addtracktest.php');

?>


Although that doesn't work.
Any clue as to what I've missed out/done wrong?
Thanks

Fumigator
12-06-2010, 06:00 PM
You have the right idea, just make sure you add session_start() to the top of your form script.

sonofjack
12-06-2010, 08:43 PM
Can't seem to work it out here.
Can anyone give me a few pointers? What am I missing from my code?
It takes me back to the form page but it hasn't submitted the original data from the form to the database.

mlseim
12-06-2010, 08:43 PM
More like this ...


<?php
session_start();
$_SESSION['composerfield'] = $_POST['composerfield'];
header('Location: addtracktest.php');
?>



You need this at the top of every script that uses SESSION ...

<?php
session_start();

Also important ... It's best to have it first, without any blank lines.
Not a big deal in your case, but it's a good habit.

On your form page, you'll then read those variables ....



<?php
session_start();

// default
$composer_field="---------";
// read the session if it exists ...
if(isset($_SESSION['composerfield'])){
$composer_field=$_SESSION['composerfield'];
}

// Notice that I'm using a variable name $composer_field (with an underscore).
// I don't want confusion with the form name you are using later on called "composerfield".
?>


<option value="<?=$composer_field?>"><?=$composer_field?></option>

sonofjack
12-07-2010, 12:43 AM
Many thanks for that.

Haven't got it quite working yet but want to check I'm doing it right.


My form is on addtracktest.php and I've got a file called formsubmit.php that the form posts to.

Am I right in that this goes in formsubmit.php


<?php
session_start();
$_SESSION['composerfield'] = $_POST['composerfield'];
header('Location: addtracktest.php');
?>



Does this go just before the form on addtracktest.php, or right at the top of the file?


<?php
session_start();

$composer_field="---------";
if(isset($_SESSION['composerfield'])){
$composer_field=$_SESSION['composerfield'];
}
?>


And this goes in the form?


<option value="<?=$composer_field?>"><?=$composer_field?></option>


Somewhere here?


<form action="formsubmit.php" method="post">
<p>Enter the new track:<br />
<textarea name="tracktitle" rows="1" cols="20">
</textarea></p>
<p>Composer:
<select name="composerfield" size="1">
<option selected value="">Select One</option>

<option value="<?=$composer_field?>"><?=$composer_field?></option>


<?php
while ($addcomposer= mysql_fetch_array($composers)) {
$cid = $addcomposer['composerid'];
$cname = htmlspecialchars($addcomposer['composername']);
echo "<option value='$cid'>$cname</option>\n";
}
?>
</select></p>

<p>Album:
<select name="albumid" size="1">
<option selected value="">Select One</option>
<option value="">---------</option>
<?php
while ($addalbum = mysql_fetch_array($albums)) {
$aid = $addalbum['albumid'];
$aname = htmlspecialchars($addalbum['albumname']);
echo "<option value='$aid'>$aname</option>\n";
}
?>
</select></p>

<input type="submit" value="SUBMIT" />
</form>




That's what I've done and all it has done is add a "1" to my list of composers and it doesn't send anything to the database.
I've obviously put something in the wrong place. Please could you let me know?
Thanks

mlseim
12-07-2010, 02:21 AM
I'm not sure whether or not you are going to re-display the form after they submit.
You don't have to ... but if you do, what they entered will be retained.


This is the script with your form ...


<?php
session_start();

$composer_field="---------";
if(isset($_SESSION['composerfield'])){
$composer_field=$_SESSION['composerfield'];
}
$album_name="---------";
if(isset($_SESSION['albumid'])){
$album_name=$_SESSION['albumid'];
}
?>


<form action="formsubmit.php" method="post">
<p>Enter the new track:<br />
<textarea name="tracktitle" rows="1" cols="20">
</textarea></p>
<p>Composer:
<select name="composerfield" size="1">
<?php
if(isset($_SESSION['composerfield'])){
echo"<option value=\"$composer_field\">$composer_field</option>";
}
else{
echo"<option selected value=\"\">Select One</option>";
}

while ($addcomposer= mysql_fetch_array($composers)) {
$cid = $addcomposer['composerid'];
$cname = htmlspecialchars($addcomposer['composername']);
echo "<option value='$cid'>$cname</option>\n";
}
?>
</select></p>

<p>Album:
<select name="albumid" size="1">
<?php
if(isset($_SESSION['albumid'])){
echo"<option value=\"$album_name\">$album_name</option>";
}
else{
echo"<option selected value=\"\">Select One</option>";
}
while ($addalbum = mysql_fetch_array($albums)) {
$aid = $addalbum['albumid'];
$aname = htmlspecialchars($addalbum['albumname']);
echo "<option value='$aid'>$aname</option>\n";
}
?>
</select></p>

<input type="submit" value="SUBMIT" />
</form>




At the top of "formsubmit.php":


<?php
session_start();
$_SESSION['composerfield'] = $_POST['composerfield'];
$_SESSION['albumid'] = $_POST['albumid'];

// You are going to have more script below here ? ...
// So you don't need to redirect anywhere.
//header('Location: addtracktest.php');

// The rest of your form submit script here if you have more ...
// perhaps you are writing their data into a database?
// If there is a problem, or you need to go back to the form,
// the user will retain the information they already put in.

// return back if you need to:
//header('Location: addtracktest.php');


?>

sonofjack
12-07-2010, 01:07 PM
Many thanks again.

Yes I can see what it's doing. However I've been trying to tweak to work exactly how I want. Can't seem to get it right.

It isn't submitting the data to the database so do I need to have this bit that comes after the form on my addtracktest page..



if (isset($_POST['tracktitle'])) :
$tracktitle = $_POST['tracktitle'];
$addcomposer = $_POST ['composerfield'];
$addalbum = $_POST ['albumid'];

if ($composers ==") { exit ('<p>Choose a composer</p>'); }
if ($albums ==") { exit ('<p>Choose an album</p>'); }

$sql = "INSERT IGNORE INTO tracks (trackid, tracktitle, albumid, composerid) VALUES ('$trackid', '$tracktitle', '$addalbum', '$addcomposer')";

if (mysql_query($sql)) {echo "Track added";}
else {echo "DEBUG SQL: " . $sql . "<p>Error adding track " . mysql_error() . "</p>";}

endif;



....on my formsubmit.php page? Followed by a return the form?

Also after I submit it seems to be displaying the ID number in the composer field rather than the name which is also in the dropdown list. Similarly with the album id.
It needs to retain the album and composer that was submitted and display the name ready for another track submission with those values. I want the user to keep adding tracks by the same composer and album until they want to change. And the other fields which I will add later once I get this all working well.

It's nearly there...

mlseim
12-07-2010, 05:21 PM
Post all of the scripts you have ...
It's too hard to figure this out by scrolling back and forth between posts.

sonofjack
12-07-2010, 05:44 PM
Good idea

addtracktest.php



<?php
session_start();

$composer_field="---------";
if(isset($_SESSION['composerfield'])){
$composer_field=$_SESSION['composerfield'];
}
$album_name="---------";
if(isset($_SESSION['albumid'])){
$album_name=$_SESSION['albumid'];
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>New Tracks</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
<style>
body {font-family: arial; font-size: 12px;}
</style>
</head>
<body>

<?php include('connect.php'); ?>

<?php

$composers = @mysql_query('SELECT composerid, composername FROM composers');
if (!$composers) {
exit('<p>Unable to obtain composer list from the database.</p>'); }

$albums = @mysql_query('SELECT albumid, albumname FROM albums');
if (!$albums) {
exit('<p>Unable to obtain album list from the database.</p>'); }
?>


<?php



//If track has been deleted remove from database.


if (isset($_GET['deletetrack'])) {

$id = $_GET['deletetrack'];

$sql = "DELETE FROM tracks
WHERE trackid='$id'";

if (@mysql_query($sql)) {
echo '<p>This listings has been deleted.</p>';
}
else {
echo '<p>Error deleting listings: ' .
mysql_error() . '</p>';
}
}

?>

// Form
<form action="formsubmit.php" method="post">
<p>Enter the new track:<br />
<textarea name="tracktitle" rows="1" cols="20">
</textarea></p>
<p>Composer:
<select name="composerfield" size="1">

<?php
if(isset($_SESSION['composerfield'])){
echo"<option value=\"$composer_field\">$composer_field</option>";
}
else{
echo"<option selected value=\"\">Select One</option>";
}

while ($addcomposer= mysql_fetch_array($composers)) {
$cid = $addcomposer['composerid'];
$cname = htmlspecialchars($addcomposer['composername']);
echo "<option value='$cid'>$cname</option>\n";
}
?>
</select></p>

<p>Album:
<select name="albumid" size="1">
<?php
if(isset($_SESSION['albumid'])){
echo"<option value=\"$album_name\">$album_name</option>";
}
else{
echo"<option selected value=\"\">Select One</option>";
}
while ($addalbum = mysql_fetch_array($albums)) {
$aid = $addalbum['albumid'];
$aname = htmlspecialchars($addalbum['albumname']);
echo "<option value='$aid'>$aname</option>\n";
}
?>
</select></p>

<input type="submit" value="SUBMIT" />
</form>

<?php
if (isset($_POST['tracktitle'])) :

$tracktitle = $_POST['tracktitle'];
$addcomposer = $_POST ['composerfield'];
$addalbum = $_POST ['albumid'];

if ($composers ==") { exit ('<p>Choose a composer</p>'); }
if ($albums ==") { exit ('<p>Choose an album</p>'); }

$sql = "INSERT IGNORE INTO tracks (trackid, tracktitle, albumid, composerid) VALUES ('$trackid', '$tracktitle', '$addalbum', '$addcomposer')";

if (mysql_query($sql)) {echo "Track added";}
else {echo "DEBUG SQL: " . $sql . "<p>Error adding track " . mysql_error() . "</p>";}


endif;

?>



<hr>
<p>Tracks</p>

<?php
$sql = 'SELECT tracks.trackid, tracks.tracktitle, composers.composername, albums.albumname FROM tracks, composers, albums WHERE

albums.albumid = tracks.albumid AND composers.composerid = tracks.composerid ORDER BY tracks.tracktitle ASC';
?>

<table>
<tr><th align=left>Track Title</th>
<th align=left>Composer</th>
<th align=left>Album</th></tr>

<?php

$tracks = @mysql_query( $sql );

if (!$tracks) {
echo '</table>';
exit('<p>Error retrieving tracks from database!<br />'.
'Error: ' . mysql_error() . '</p>');
}

while ($track = mysql_fetch_array($tracks)) {
echo "<tr valign='top'>\n";
$id = $track['trackid'];
$tracktitle = htmlspecialchars($track['tracktitle']);
$composername = htmlspecialchars($track['composername']);
$albumtitle = htmlspecialchars($track['albumname']);

echo "<td width=150>$tracktitle</td>";
echo "<td width=150>$composername</td>";
echo "<td width=300>$albumtitle</td>";
echo "<td><a href='edittrack.php?id=$id'>Edit</a> | ";
echo "<a href=" . $_SERVER['PHP_SELF'] . "?deletetrack=$id>Delete</a></td>\n";
echo "</tr>\n";

}

?>

</table>

</body>
</html>


formsubmit.php



<?php
session_start();
$_SESSION['composerfield'] = $_POST['composerfield'];
$_SESSION['albumid'] = $_POST['albumid'];

// You are going to have more script below here ? ...
// So you don't need to redirect anywhere.
//header('Location: addtracktest.php');


// The rest of your form submit script here if you have more ...
// perhaps you are writing their data into a database?
// If there is a problem, or you need to go back to the form,
// the user will retain the information they already put in.

// return back if you need to:
header('Location: addtracktest.php');

?>



Many thanks.

Fumigator
12-07-2010, 05:57 PM
Also after I submit it seems to be displaying the ID number in the composer field rather than the name which is also in the dropdown list

Yes, that's how select and option elements work. The value of the "value" attribute is what gets sent through the form submit. Whatever is between <option> and </option> is merely for display purposes, it does not go anywhere. If you need that string value, you'll have to run a query and select it again from the database using the selected ID.

mlseim
12-07-2010, 06:10 PM
Try these two scripts ....

addtracktest.php


<?php
session_start();

$composer_field="---------";
if(isset($_SESSION['composerfield'])){
$composer_field=$_SESSION['composerfield'];
}
$album_id="---------";
if(isset($_SESSION['albumid'])){
$album_id=$_SESSION['albumid'];
}

// messages from formsubmit
if(isset($_GET['mess']){
$mess=$_GET['mess'];
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>New Tracks</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
<style>
body {font-family: arial; font-size: 12px;}
</style>
</head>
<body>

<?php include('connect.php'); ?>

<?php

$composers = @mysql_query('SELECT composerid, composername FROM composers');
if (!$composers) {
exit('<p>Unable to obtain composer list from the database.</p>'); }

$albums = @mysql_query('SELECT albumid, albumname FROM albums');
if (!$albums) {
exit('<p>Unable to obtain album list from the database.</p>'); }
?>

<!-- Display any messages from formsubmit -->
<b><?=$mess?></b><br />

<form action="formsubmit.php" method="post">
<p>Enter the new track:<br />
<textarea name="tracktitle" rows="1" cols="20">
</textarea></p>
<p>Composer:
<select name="composerfield" size="1">

<?php
if(isset($_SESSION['composerfield'])){
while ($addcomposer= mysql_fetch_array($composers)) {
$cid = $addcomposer['composerid'];
if($cid == $composer_field){
$cname = htmlspecialchars($addcomposer['composername']);
}
echo"<option value=\"$composer_field\">$cname</option>";
}
}
else{
echo"<option selected value=\"\">Select One</option>";
}
while ($addcomposer= mysql_fetch_array($composers)) {
$cid = $addcomposer['composerid'];
$cname = htmlspecialchars($addcomposer['composername']);
echo "<option value='$cid'>$cname</option>\n";
}
?>
</select></p>

<p>Album:
<select name="albumid" size="1">
<?php
if(isset($_SESSION['albumid'])){
while ($addalbum= mysql_fetch_array($albums)) {
$aid = $addalbum['albumid'];
if($aid == $album_id){
$aname = htmlspecialchars($addcomposer['albumname']);
}
echo"<option value=\"$album_name\">$aname</option>";
}
}
else{
echo"<option selected value=\"\">Select One</option>";
}
while ($addalbum = mysql_fetch_array($albums)) {
$aid = $addalbum['albumid'];
$aname = htmlspecialchars($addalbum['albumname']);
echo "<option value='$aid'>$aname</option>\n";
}
?>
</select></p>

<input type="submit" value="SUBMIT" />
</form>
<hr>
<p>Tracks</p>

<?php
$sql = 'SELECT tracks.trackid, tracks.tracktitle, composers.composername, albums.albumname FROM tracks, composers, albums WHERE

albums.albumid = tracks.albumid AND composers.composerid = tracks.composerid ORDER BY tracks.tracktitle ASC';
?>

<table>
<tr><th align=left>Track Title</th>
<th align=left>Composer</th>
<th align=left>Album</th></tr>

<?php

$tracks = @mysql_query( $sql );

if (!$tracks) {
echo '</table>';
exit('<p>Error retrieving tracks from database!<br />'.
'Error: ' . mysql_error() . '</p>');
}

while ($track = mysql_fetch_array($tracks)) {
echo "<tr valign='top'>\n";
$id = $track['trackid'];
$tracktitle = htmlspecialchars($track['tracktitle']);
$composername = htmlspecialchars($track['composername']);
$albumtitle = htmlspecialchars($track['albumname']);

echo "<td width=150>$tracktitle</td>";
echo "<td width=150>$composername</td>";
echo "<td width=300>$albumtitle</td>";
echo "<td><a href='edittrack.php?id=$id'>Edit</a> | ";
echo "<a href='formsubmit.php?deletetrack=$id'>Delete</a></td>\n";
echo "</tr>\n";

}

?>

</table>

</body>
</html>



formsubmit.php


<?php
session_start();
if(isset($_POST['composerfield'])){
$_SESSION['composerfield'] = $_POST['composerfield'];
}
if(isset($_POST['albumid'])){
$_SESSION['albumid'] = $_POST['albumid'];
}

if (isset($_POST['tracktitle'])){

$tracktitle = $_POST['tracktitle'];
$addcomposer = $_POST ['composerfield'];
$addalbum = $_POST ['albumid'];

if ($composers ==") { $mess="<p>Choose a composer</p>"; }
if ($albums ==") { $mess="<p>Choose an album</p>"; }

$sql = "INSERT IGNORE INTO tracks (trackid, tracktitle, albumid, composerid) VALUES ('$trackid', '$tracktitle', '$addalbum', '$addcomposer')";

if (mysql_query($sql)) {$mess= "Track added";}
else {echo "DEBUG SQL: " . $sql . "<p>Error adding track " . mysql_error() . "</p>";}

}

//If track has been deleted remove from database.
if (isset($_GET['deletetrack'])) {

$id = $_GET['deletetrack'];

$sql = "DELETE FROM tracks
WHERE trackid='$id'";

if (@mysql_query($sql)) {
$mess="This listings has been deleted.</p>";
}
else {
$mess="Error deleting listings: " .mysql_error(). "</p>";
}
}

// return back:
header('Location: addtracktest.php?mess=$mess');

?>

mlseim
12-07-2010, 06:16 PM
EDIT:

I just made a couple of changes to the two scripts above.
Re-copy them please ...


.

sonofjack
12-07-2010, 08:44 PM
Thanks again for that.
I'm just studying the code to work out what is going where.

At present the composer field is just showing the one, and the album field isn't showing any data. And when I submit, I get the message:

Parse error: syntax error, unexpected '>' in /home/dancesounds/dancesounds.co.uk/phpsearch/formsubmit.php on line 16

Which seems to point to:



if ($composers ==") { $mess="<p>Choose a composer</p>"; }
if ($albums ==") { $mess="<p>Choose an album</p>"; }


I tried changing it to....



if ($composers ==") { $mess='<p>Choose a composer</p>'; }
if ($albums ==") { $mess='<p>Choose an album</p>'; }


...to see if the " and ' was causing any confusion.
Then after submitting the form it takes me back to the addtracktest.php page with

$mess

displayed at the top of the page.

I also noticed that



echo"<option value=\"$album_name\">$aname</option>";


should probably have been



echo"<option value=\"$album_id\">$aname</option>";


Though on changing it it hasn't made a difference to displaying the album titles.

Thought it was there but I can't work out which bit isn't working.
Thanks again for looking into it.

mlseim
12-07-2010, 09:33 PM
Make them two double-quotes side by side ""

Like this:
if ($composers =="") { $mess="<p>Choose a composer</p>"; }
if ($albums =="") { $mess="<p>Choose an album</p>"; }

That will fix the "$mess" appearing problem too.

mlseim
12-07-2010, 09:41 PM
Try replacing this section of your code ...





<p>Composer:
<select name="composerfield" size="1">

<?php
$flag1=0;
if(isset($_SESSION['composerfield'])){
while ($addcomposer= mysql_fetch_array($composers)) {
$cid = $addcomposer['composerid'];
if($cid == $composer_field && flag1==0){
$flag1=1;
$xcid=$cid;
$cname = htmlspecialchars($addcomposer['composername']);
}
echo"<option value=\"$xcid\">$cname</option>";
}
}
else{
echo"<option selected value=\"\">Select One</option>";
}
while ($addcomposer= mysql_fetch_array($composers)) {
$cid = $addcomposer['composerid'];
$cname = htmlspecialchars($addcomposer['composername']);
echo "<option value='$cid'>$cname</option>\n";
}
?>
</select></p>

<p>Album:
<select name="albumid" size="1">
<?php
$flag2=0;
if(isset($_SESSION['albumid'])){
while ($addalbum= mysql_fetch_array($albums)) {
$aid = $addalbum['albumid'];
if($aid == $album_id && $flag2==0){
$flag2=1;
$xaid=$aid;
$aname = htmlspecialchars($addalbum['albumname']);
}
echo"<option value=\"$xaid\">$aname</option>";
}
}
else{
echo"<option selected value=\"\">Select One</option>";
}
while ($addalbum = mysql_fetch_array($albums)) {
$aid = $addalbum['albumid'];
$aname = htmlspecialchars($addalbum['albumname']);
echo "<option value='$aid'>$aname</option>\n";
}
?>
</select></p>

sonofjack
12-07-2010, 10:19 PM
I keep wanting to come back and say it works great.
It doesn't seem to want to play ball though.
When I first loaded it it displayed the composers and albums as normal. When I submitted it, it didn't save to the database and it returned to the addtracktest page with $mess displaying again.
Also the composer field now lists the same composer lots of times and the same album lots of times preceded by blank entries in the drop down list.
Even after refreshing the page and deleting cookies it still shows it the same way.
Trying to work out the code you sent so I can get my head around it :)

I notice you put



if($cid == $composer_field && flag1==0){

and also


if($aid == $album_id && $flag2==0){


One with and one without a $. I tried inserting/removing the $ but I got the same result.

Also the delete function seems to have stopped working.

Must be close.
And continued thanks for the help....

addtracktest.php


<?php
session_start();

$composer_field="---------";
if(isset($_SESSION['composerfield'])){
$composer_field=$_SESSION['composerfield'];
}
$album_id="---------";
if(isset($_SESSION['albumid'])){
$album_id=$_SESSION['albumid'];
}

// messages from formsubmit
if(isset($_GET['mess'])){
$mess=$_GET['mess'];
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>New Tracks</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
<style>
body {font-family: arial; font-size: 12px;}
</style>
</head>
<body>

<?php include('connect.php'); ?>

<?php

$composers = @mysql_query('SELECT composerid, composername FROM composers');
if (!$composers) {
exit('<p>Unable to obtain composer list from the database.</p>'); }

$albums = @mysql_query('SELECT albumid, albumname FROM albums');
if (!$albums) {
exit('<p>Unable to obtain album list from the database.</p>'); }
?>

<!-- Display any messages from formsubmit -->
<b><?=$mess?></b><br />

<form action="formsubmit.php" method="post">
<p>Enter the new track:<br />
<textarea name="tracktitle" rows="1" cols="20">
</textarea></p>

<p>Composer:
<select name="composerfield" size="1">

<?php
$flag1=0;
if(isset($_SESSION['composerfield'])){
while ($addcomposer= mysql_fetch_array($composers)) {
$cid = $addcomposer['composerid'];
if($cid == $composer_field && $flag1==0){
$flag1=1;
$xcid=$cid;
$cname = htmlspecialchars($addcomposer['composername']);
}
echo"<option value=\"$xcid\">$cname</option>";
}
}
else{
echo"<option selected value=\"\">Select One</option>";
}
while ($addcomposer= mysql_fetch_array($composers)) {
$cid = $addcomposer['composerid'];
$cname = htmlspecialchars($addcomposer['composername']);
echo "<option value='$cid'>$cname</option>\n";
}
?>
</select></p>

<p>Album:
<select name="albumid" size="1">
<?php
$flag2=0;
if(isset($_SESSION['albumid'])){
while ($addalbum= mysql_fetch_array($albums)) {
$aid = $addalbum['albumid'];
if($aid == $album_id && $flag2==0){
$flag2=1;
$xaid=$aid;
$aname = htmlspecialchars($addalbum['albumname']);
}
echo"<option value=\"$xaid\">$aname</option>";
}
}
else{
echo"<option selected value=\"\">Select One</option>";
}
while ($addalbum = mysql_fetch_array($albums)) {
$aid = $addalbum['albumid'];
$aname = htmlspecialchars($addalbum['albumname']);
echo "<option value='$aid'>$aname</option>\n";
}
?>
</select></p>



<input type="submit" value="SUBMIT" />
</form>
<hr>
<p>Tracks</p>

<?php
$sql = 'SELECT tracks.trackid, tracks.tracktitle, composers.composername, albums.albumname FROM tracks, composers, albums WHERE

albums.albumid = tracks.albumid AND composers.composerid = tracks.composerid ORDER BY tracks.tracktitle ASC';
?>

<table>
<tr><th align=left>Track Title</th>
<th align=left>Composer</th>
<th align=left>Album</th></tr>

<?php

$tracks = @mysql_query( $sql );

if (!$tracks) {
echo '</table>';
exit('<p>Error retrieving tracks from database!<br />'.
'Error: ' . mysql_error() . '</p>');
}

while ($track = mysql_fetch_array($tracks)) {
echo "<tr valign='top'>\n";
$id = $track['trackid'];
$tracktitle = htmlspecialchars($track['tracktitle']);
$composername = htmlspecialchars($track['composername']);
$albumtitle = htmlspecialchars($track['albumname']);

echo "<td width=150>$tracktitle</td>";
echo "<td width=150>$composername</td>";
echo "<td width=300>$albumtitle</td>";
echo "<td><a href='edittrack.php?id=$id'>Edit</a> | ";
echo "<a href='formsubmit.php?deletetrack=$id'>Delete</a></td>\n";
echo "</tr>\n";

}

?>

</table>

</body>
</html>


formsubmit.php



<?php
session_start();
if(isset($_POST['composerfield'])){
$_SESSION['composerfield'] = $_POST['composerfield'];
}
if(isset($_POST['albumid'])){
$_SESSION['albumid'] = $_POST['albumid'];
}

if (isset($_POST['tracktitle'])){

$tracktitle = $_POST['tracktitle'];
$addcomposer = $_POST ['composerfield'];
$addalbum = $_POST ['albumid'];

if ($composers =="") { $mess="<p>Choose a composer</p>"; }
if ($albums =="") { $mess="<p>Choose an album</p>"; }

$sql = "INSERT IGNORE INTO tracks (trackid, tracktitle, albumid, composerid) VALUES ('$trackid', '$tracktitle', '$addalbum', '$addcomposer')";

if (mysql_query($sql)) {$mess= "Track added";}
else {echo "DEBUG SQL: " . $sql . "<p>Error adding track " . mysql_error() . "</p>";}

}

//If track has been deleted remove from database.
if (isset($_GET['deletetrack'])) {

$id = $_GET['deletetrack'];

$sql = "DELETE FROM tracks
WHERE trackid='$id'";

if (@mysql_query($sql)) {
$mess="This listings has been deleted.</p>";
}
else {
$mess="Error deleting listings: " .mysql_error(). "</p>";
}
}

// return back:
header('Location: addtracktest.php?mess=$mess');

?>

mlseim
12-07-2010, 10:41 PM
I keep making typos ... and I can't troubleshoot the scripting that I'm doing.
I PM'd you about this.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum