...

View Full Version : Problem with checkboxes and drop down menus



sonofjack
12-02-2010, 09:33 AM
I've got a page where I can add a new track(songtitle) to a table and assign a composer and an album to it.
The composer is selected from a drop down menu and the album is selected from checkboxes.
After much messing about, it works fine.
But now I want the albums to be in a drop down menu too which I thought would be quite straightforward.
But it isn't working. The tracks aren't being added to the database.
I must have made an error with values or names or something but I can't work it out.
This is the form that works:



<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="cid" size="1">
<option selected value="">Select One</option>
<option value="">---------</option>
<?php
while ($composer= mysql_fetch_array($composers)) {
$cid = $composer['composerid'];
$cname = htmlspecialchars($composer['composername']);
echo "<option value='$cid'>$cname</option>\n";
}
?>
</select></p>
<p>Place in albums:<br />
<?php
while ($alb = mysql_fetch_array($albs)) {
$aid = $alb['albumid'];
$aname = htmlspecialchars($alb['albumname']);
echo "<label><input type='checkbox' name='albs[]' value='$aid' />$aname</label><br />\n";
}
?>
</p>
<input type="submit" value="SUBMIT" />
</form>


And this is the form with the drop down menu for albums that doesn't work.



<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="cid" size="1">
<option selected value="">Select One</option>
<option value="">---------</option>

<?php
while ($composer= mysql_fetch_array($composers)) {
$cid = $composer['composerid'];
$cname = htmlspecialchars($composer['composername']);
echo "<option value='$cid'>$cname</option>\n";
}
?>
</select></p>
<p>Album:
<select name='aid' size="1">
<option selected value="">Select One</option>
<option value="">---------</option>

<?php
while ($alb = mysql_fetch_array($albs)) {
$aid = $alb['albumid'];
$aname = htmlspecialchars($alb['albumname']);
echo "<option value='$aid'>$aname</option>\n";
}
?>
</p>
<input type="submit" value="SUBMIT" />
</form>



Can anyone help me out with what I did wrong?

Thanks

Lamped
12-02-2010, 10:44 AM
You didn't close the <select> for the albums, though that probably won't make a difference.

Where's the code that processes that form and inserts it into the database?

sonofjack
12-02-2010, 10:47 AM
Thanks for pointing that bit out. I've added it in now but, as you predicted, it hasn't made a difference.
The rest of the code is



<?php

include ('connect.php');

if (isset($_POST['tracktitle'])):
// A new track has been entered
// using the form.

$tracktitle = mysql_real_escape_string($tracktitle);
$cid= $_POST['cid'];
$tracktitle = $_POST['tracktitle'];
$albs = $_POST['albs'];

if ($cid == '') {
exit('<p>You must choose an composer for this track. Click "Back" and try again.</p>');
}

$trackid = mysql_insert_id();

if (isset($_POST['albs'])) {
$albs = $_POST['albs'];
} else {
$albs = array();
}

$numAlbs = 0;
foreach ($albs as $albID) {

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

if (mysql_query($sql)) {
$numAlbs = $numAlbs + 1;
} else {
echo "DEBUG SQL: " . $sql ."<p>Error inserting track into album $albID: " .
mysql_error() . '</p>';
}
}
?>


<p>Track was added to <?php echo $numAlbs; ?> albums.</p>

<p><a href="<?php echo $_SERVER['PHP_SELF']; ?>">Add another track</a></p>
<p><a href="tracks.php">Return to track search</a></p>


<?php
else:// Allow the user to enter a new track

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

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



I tried to remove the $numAlbs bit as it wasn't really necessary for me to know how many albums a track had been added to. But it stopped working when I tried editing so it's back in there for now.

Lamped
12-02-2010, 10:58 AM
That appears to be a single entry drop-down box, but you're treating it like an array.

http://www.onlinetools.org/tricks/using_multiple_select.php gives an example of using a listbox style select. You need to add multiple="multiple" to your select and name it aid[] - the brackets tell PHP to treat every value as part of an array.

I *think*, based on my brief look at your code, this should fix it.

sonofjack
12-02-2010, 11:02 AM
Thanks. In this instance I don't need the track to be added to more than one album. I just want it to be the like composer drop down menu. Add to one composer and to one album.
Same as this one but for albums...


<select name="cid" size="1">
<option selected value="">Select One</option>
<option value="">---------</option>
<?php
while ($composer= mysql_fetch_array($composers)) {
$cid = $composer['composerid'];
$cname = htmlspecialchars($composer['composername']);
echo "<option value='$cid'>$cname</option>\n";
}
?>


Though I will need to do some multiple adds for other fields when I expand it.

Thanks for the link, I'll check it out.

Lamped
12-02-2010, 11:26 AM
if (@$_POST['albs']) {
$albID = $_POST['albs'];
// Now do the SQL, without the foreach loop as part of this if statement
}

That's for single album choice, without multiple specified in the select, and <select name="albs">

sonofjack
12-02-2010, 11:47 AM
Thanks. I've amended it but it hasn't work.
That could be to do with what I've amended though.

Please could you check?



if (isset($_POST['tracktitle'])):
// A new track has been entered
// using the form.

$tracktitle = mysql_real_escape_string($tracktitle);
$cid= $_POST['cid'];
$tracktitle = $_POST['tracktitle'];
$albs = $_POST['albs'];

if ($cid == '') {
exit('<p>You must choose an composer for this track. Click "Back" and try again.</p>');
}

$trackid = mysql_insert_id();

$numAlbs = 0;

if (@$_POST['albs']) {
$albID=$_POST['albs'];

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

if (mysql_query($sql)) {
$numAlbs = $numAlbs + 1;
} else {
echo "DEBUG SQL: " . $sql ."<p>Error inserting track into album $albID: " .
mysql_error() . '</p>';
}
}

sonofjack
12-02-2010, 12:03 PM
Scrap what I said... it works fine.
Thanks a lot.

I had <select name='albs'>
instead of
<select name="albs"> as you had said.
Helps if I read things properly.

I think that's what the problem was. I also had to refresh the page a few times.

Anyway it works fine.
Thanks for the help

Lamped
12-02-2010, 12:04 PM
I'll try this post again without the site going down when I click reply... ;)

1. echo($sql) so we can see what's actually being passed to mysql_query.

2. Please use php tags instead of code tags so I can read the code better.

3. I assume there's an INSERT somewhere above the code you've provided, otherwise the $trackid = mysql_insert_id(); doesn't make any sense?

Lamped
12-02-2010, 12:06 PM
Scrap what I said... it works fine.
Thanks a lot.

Good stuff. Do try and remember point 2 above, though ;)

sonofjack
12-02-2010, 01:39 PM
php tags! Sorry, will do. I thought the code tag was the one. Will know for future.

regarding: $trackid = mysql_insert_id();

Yes there was another Insert code. As the whole thing wasn't working yesterday, I took it out and had just the one 'Insert Ignore Into' code for the id, tracktitles, composerid an albumid and it worked fine.
I guess I can remove the $trackid = mysql_insert_id(); code now then.

I've been adapting code from a tutorial to suit my own purposes which I thought might be a good way for me to learn. I have got myself stuck a few times though. Hopefully will be able to write it all from scratch soon. That's the plan.

Thanks for the help

Lamped
12-02-2010, 01:58 PM
Thanks for the help

No problem, and for what it's worth: It's a good way to learn and you seem to be doing well :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum