Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 5 of 5
  1. #1
    Junior Member
    Join Date
    Aug 2006
    Location
    Sheffield, UK
    Posts
    24
    Member #
    13792
    Hi, I'm helping a friend out with his tab site and I've come a bit unstuck. I have 2 problems really..

    1. There are 2 tables, album and song, that need data being added to when a new album is submitted. The problem is when the song data is added I need a value for 'album_id' which relates to the album the song is from, but the album data is added at the same time as the song data so I don't know the album_id when it is submitted. Is there any way around this? I know I could perfom a small query straight after the album data is added but I was wondering if there is an easier way around it?

    2. In the form for adding an album, there are a number of text fields generated depending on the number of songs on the album for the song names. I've named each of these fields so that when the data is sent to the processing page all the song names are added as an array, but I can't seem to get the sql query to run in a loop to add all the songs as seperate values in the database. The code I have is (ignore the 'album_id' part, I just placed it there until I can figure out what to do for my other question):
    Code:
    for($i=1; $i <= $_POST['song_no']; $i++)
        {
    $sql = "INSERT INTO
                song
                (song_name,
                band_id,
                required)
            VALUES
                ('".$_POST[song_name[$i]]."',
                '".$_POST['album_id']."',
                '".$_POST[required[$i]]."')";
        }
    I'm quite sure there's just a problem with the syntax but I can't figure out how to get it working.

    Any help would be great, thanks!

  2.  

  3. #2
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,733
    Member #
    5580
    Liked
    718 times
    When you create the form, I'm assuming you use a PHP script to create the form,
    so you know how many text boxes to make and you end up with a line like this
    in your form:
    <input type='hidden' name='song_no' value='14'>

    That way, when the form is submitted, you have a value for "song_no",
    which tells you how many songs (or text boxes) you have on the form.

    Why not do the same thing with the album_id?

    Have the PHP script add a hidden input for the ID. Like this:
    <input type='hidden' name='album_id' value='467234'>

    When you process the form, it will take the album_id with it, and associate
    each song with the id.

    EDIT:
    Also, know that an array starts with the index of [0] ZERO .
    So, in your "for" loop ..... for($i=0; .... )


  4. #3
    Junior Member
    Join Date
    Aug 2006
    Location
    Sheffield, UK
    Posts
    24
    Member #
    13792
    Quote Originally Posted by mlseim
    When you create the form, I'm assuming you use a PHP script to create the form,
    so you know how many text boxes to make and you end up with a line like this
    in your form:
    <input type='hidden' name='song_no' value='14'>

    That way, when the form is submitted, you have a value for "song_no",
    which tells you how many songs (or text boxes) you have on the form.
    Yeah, I've done this. I've used this to tell how many songs the process form needs to add to the database too. I just can't get it to work.

    Quote Originally Posted by mlseim
    Why not do the same thing with the album_id?

    Have the PHP script add a hidden input for the ID. Like this:
    <input type='hidden' name='album_id' value='467234'>

    When you process the form, it will take the album_id with it, and associate
    each song with the id.
    Because the album_id value is created at the same time the songs are supposed to be added to the database.

    When I want to add an album, the following values need to be added to the database...
    'album' table:
    band_id, album_name, album_year
    'song' table:
    song_name, album_id, required

    All those values are added at the same time and the album_id value is added by the database as the unique id. So when it comes to added it to the song table, I don't know it yet. Like I said before, I could perform a simple query to find it out but I'm pretty sure there's a simpler way.

    Quote Originally Posted by mlseim
    EDIT:
    Also, know that an array starts with the index of [0] ZERO .
    So, in your "for" loop ..... for($i=0; .... )
    Yeah, someone just pointed that out to me earlier! I've changed it now.

    My code for the process form is now..
    Code:
    $sql = "INSERT INTO
                song
                (song_name,
                album_id,
                required)
            VALUES
                ('".$_POST['song_name'][$i]."',
                '".$_POST['album_id']."',
                '".$_POST['required'][$i]."')";
    But now this is just adding one empty field to the song table

  5. #4
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    [phpfunction]mysql_insert_id[/phpfunction] for MySQL :-)

  6. #5
    Junior Member
    Join Date
    Aug 2006
    Location
    Sheffield, UK
    Posts
    24
    Member #
    13792
    Thanks. I've actually got it working now, finally!


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 10:39 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com