By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,570 Members | 999 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,570 IT Pros & Developers. It's quick & easy.

Problem inserting data using checkboxes and input type=text

P: 3
Hi!

I got a problem that's driving me crazy and I'm desperately in need of help.

I'll explain my scenario:
I'm doing a database driven site for a band, I got these tables for their discography section:

Discography
---------------------
DiscID
DiscName

Song
---------------
SongID
SongName

As this is a many to many relation I got the following table

DiscoSong
------------------
DiscID
SongID
Orden

I already filled the Discography table with all their records to date, so no problem there. Now what wanna do is that every time I insert a song I want to associate it with the records it appears on as well as its order of appearance on each record, that's why I got the attribute Orden on my DiscoSong table.

I'm selecting the records with checkboxes in a form and inserting the song number on input type=text fields.

This is my php code for it:

[PHP]
$SongID = $_POST['SongID'];

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


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


foreach ($discs as $DiscID) {

foreach($Orden as $ordena){

$sql = "INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('$SongID', '$DiscID', '$ordena')";
$ok = @mysql_query($sql);
if (!$ok) {
echo $sql;
echo "<p>Error inserting Song into Disc $DiscID: " .
mysql_error() . '</p>';
} else{
echo $sql;
echo "<p> Song $SongID inserted into Disc $DiscID in order $ordena</p>";
}

}

}

[/PHP]

My code for the checkboxes and input fields:

Expand|Select|Wrap|Line Numbers
  1. $records = mysql_query("SELECT DiscID, DiscName FROM Discography");
  2.  
  3. while($options = mysql_fetch_array($records)){
  4.         $DiscID = $options['DiscID'];
  5.         $DiscName = htmlspecialchars($options['DiscName']);
  6.         echo "
  7.         <table align='center'>
  8.             <tr>
  9.                 <td width='250'>
  10.                 $DiscName
  11.                 </td>
  12.                 <td width='250'>
  13.                  <input name='DiscID[]' type='checkbox'/>
  14.                  </td>
  15.              </tr>
  16.                  <td width='250'> Song Number: 
  17.                  </td>
  18.                  <td width='250'>
  19.                  <input name='Orden[]' type='text' size='3' />
  20.                  </td>
  21.              </tr>
  22.          </table>
  23.      <br />";
  24. }
  25. ?>
  26.  
  27.  
I got 6 records, so when I fill the form selecting 3 records and writing their song number on their respective input fields I get this:

Expand|Select|Wrap|Line Numbers
  1. INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '2', '')
  2.  
  3. Song 17 inserted into Disc 2 in order
  4. INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '2', '2')
  5.  
  6. Error inserting Song into Disc 2: Duplicate entry '2-17' for key 1
  7. INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '2', '10')
  8.  
  9. Error inserting Song into Disc 2: Duplicate entry '2-17' for key 1
  10. INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '2', '')
  11.  
  12. Error inserting Song into Disc 2: Duplicate entry '2-17' for key 1
  13. INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '2', '10')
  14.  
  15. Error inserting Song into Disc 2: Duplicate entry '2-17' for key 1
  16. INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '2', '')
  17.  
  18. Error inserting Song into Disc 2: Duplicate entry '2-17' for key 1
  19. INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '3', '')
  20.  
  21. Song 17 inserted into Disc 3 in order
  22. INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '3', '2')
  23.  
  24. Error inserting Song into Disc 3: Duplicate entry '3-17' for key 1
  25. INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '3', '10')
  26.  
  27. Error inserting Song into Disc 3: Duplicate entry '3-17' for key 1
  28. INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '3', '')
  29.  
  30. Error inserting Song into Disc 3: Duplicate entry '3-17' for key 1
  31. INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '3', '10')
  32.  
  33. Error inserting Song into Disc 3: Duplicate entry '3-17' for key 1
  34. INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '3', '')
  35.  
  36. Error inserting Song into Disc 3: Duplicate entry '3-17' for key 1
  37. INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '5', '')
  38.  
  39. Song 17 inserted into Disc 5 in order
  40. INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '5', '2')
  41.  
  42. Error inserting Song into Disc 5: Duplicate entry '5-17' for key 1
  43. INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '5', '10')
  44.  
  45. Error inserting Song into Disc 5: Duplicate entry '5-17' for key 1
  46. INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '5', '')
  47.  
  48. Error inserting Song into Disc 5: Duplicate entry '5-17' for key 1
  49. INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '5', '10')
  50.  
  51. Error inserting Song into Disc 5: Duplicate entry '5-17' for key 1
  52. INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '5', '')
  53.  
  54. Error inserting Song into Disc 5: Duplicate entry '5-17' for key 1
  55.  
And I want to get this

Expand|Select|Wrap|Line Numbers
  1. INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '2', '2')
  2.  
  3. Song 17 inserted into Disc 2 in order 2
  4.  
  5. INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '3', '10')
  6.  
  7. Song 17 inserted into Disc 3 in order 10
  8.  
  9. INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '5', '10')
  10.  
  11. Song 17 inserted into Disc 5 in order 10
I know my problem is on the second foreach, but I don't know what to do, I've been thinking for almost week and all my attempts have failed.

Hope someone can help me.

Thanks in advance.

PS. Sorry for making this thread so extensive, I be as much clear as possible with my problem
Aug 23 '08 #1
Share this Question
Share on Google+
2 Replies


Atli
Expert 5K+
P: 5,058
Hi.

Your second loop, the one looping through the Order, shouldn't be a loop.

You should be adding each Disk and the Orden that is meant for that Disk. That is; you should be looping through the Disks, and using the Orden at the same index as the Disk.

Consider this:
Expand|Select|Wrap|Line Numbers
  1. $songID = $_POST['SongID']
  2. $diskID = $_POST['DiskID']; // This would be an array from a DiskID[] input
  3. $orden = $_POST['Orden'] // And this from a Orden[] input. (one for each DIskID)
  4.  
  5. foreach($DiskID as $_index => $_diskID) {
  6.   $sql = "
  7.       INSERT INTO tbl(DiskID, SongID, Orden) 
  8.       VALUES($_diskID, $songID, {$order[$_index]}";
  9. }
  10.  
Aug 25 '08 #2

P: 3
Thank you very much man, my problem is solved :D
Aug 25 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.