Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Regular Coder
    Join Date
    Sep 2005
    Posts
    153
    Thanks
    34
    Thanked 0 Times in 0 Posts

    Problem with checkboxes and drop down menus

    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:

    Code:
    <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.

    Code:
    <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

  • #2
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    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?
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #3
    Regular Coder
    Join Date
    Sep 2005
    Posts
    153
    Thanks
    34
    Thanked 0 Times in 0 Posts
    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

    Code:
    <?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.
    Last edited by sonofjack; 12-02-2010 at 10:55 AM.

  • #4
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    That appears to be a single entry drop-down box, but you're treating it like an array.

    http://www.onlinetools.org/tricks/us...ple_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.
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #5
    Regular Coder
    Join Date
    Sep 2005
    Posts
    153
    Thanks
    34
    Thanked 0 Times in 0 Posts
    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...
    Code:
    <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.

  • #6
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    PHP Code:
      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">
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • Users who have thanked Lamped for this post:

    sonofjack (12-02-2010)

  • #7
    Regular Coder
    Join Date
    Sep 2005
    Posts
    153
    Thanks
    34
    Thanked 0 Times in 0 Posts
    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?

    Code:
    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>';
        }
      }

  • #8
    Regular Coder
    Join Date
    Sep 2005
    Posts
    153
    Thanks
    34
    Thanked 0 Times in 0 Posts
    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

  • #9
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    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.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #10
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    Quote Originally Posted by sonofjack View Post
    Scrap what I said... it works fine.
    Thanks a lot.
    Good stuff. Do try and remember point 2 above, though
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #11
    Regular Coder
    Join Date
    Sep 2005
    Posts
    153
    Thanks
    34
    Thanked 0 Times in 0 Posts
    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

  • #12
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    Quote Originally Posted by sonofjack View Post
    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
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •