473,390 Members | 942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,390 software developers and data experts.

Problem inserting data using checkboxes and input type=text


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:



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


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:

$SongID = $_POST['SongID'];

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

$Orden = $_POST['Orden'];
$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>";




My code for the checkboxes and input fields:

Expand|Select|Wrap|Line Numbers
  1. $records = mysql_query("SELECT DiscID, DiscName FROM Discography");
  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. ?>
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', '')
  3. Song 17 inserted into Disc 2 in order
  4. INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '2', '2')
  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')
  9. Error inserting Song into Disc 2: Duplicate entry '2-17' for key 1
  10. INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '2', '')
  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')
  15. Error inserting Song into Disc 2: Duplicate entry '2-17' for key 1
  16. INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '2', '')
  18. Error inserting Song into Disc 2: Duplicate entry '2-17' for key 1
  19. INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '3', '')
  21. Song 17 inserted into Disc 3 in order
  22. INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '3', '2')
  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')
  27. Error inserting Song into Disc 3: Duplicate entry '3-17' for key 1
  28. INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '3', '')
  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')
  33. Error inserting Song into Disc 3: Duplicate entry '3-17' for key 1
  34. INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '3', '')
  36. Error inserting Song into Disc 3: Duplicate entry '3-17' for key 1
  37. INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '5', '')
  39. Song 17 inserted into Disc 5 in order
  40. INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '5', '2')
  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')
  45. Error inserting Song into Disc 5: Duplicate entry '5-17' for key 1
  46. INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '5', '')
  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')
  51. Error inserting Song into Disc 5: Duplicate entry '5-17' for key 1
  52. INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '5', '')
  54. Error inserting Song into Disc 5: Duplicate entry '5-17' for key 1
And I want to 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 2
  5. INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '3', '10')
  7. Song 17 inserted into Disc 3 in order 10
  9. INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '5', '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
2 3047
5,058 Expert 4TB

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)
  5. foreach($DiskID as $_index => $_diskID) {
  6.   $sql = "
  7.       INSERT INTO tbl(DiskID, SongID, Orden) 
  8.       VALUES($_diskID, $songID, {$order[$_index]}";
  9. }
Aug 25 '08 #2
Thank you very much man, my problem is solved :D
Aug 25 '08 #3

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

Similar topics

by: Bill | last post by:
I'm trying to create a form that allows me to modify the contents of list. When the user clicks on the modify button, it takes them to a page which displays the quantity of items they purchase, and...
by: Walter | last post by:
Hi, The user has to type his name into input a input field (type = text). This name must be passed to another ASP file. I did this but nothing happened: <form id=ins method="post"...
by: Ben | last post by:
Hi all, I'm trying to write inside a table cell from external javascript but am not successful. When I insert inside a form within <td...>, it works but does not work for normal table cell. My...
by: Dean A. Hoover | last post by:
Is there an example somewhere of a function that will take 2 parameters: text and a text object (input type=text or textarea) and insert that text using the current selection point(s) for the...
by: Randell D. | last post by:
Folks, I have a form called "ourTestForm". Its a test form - nothing special - it contains five input tags - they are named one, two, three, four and five. The input tags are of type...
by: acecraig100 | last post by:
I am fairly new to Javascript. I have a form that users fill out to enter an animal to exhibit at a fair. Because we have no way of knowing, how many animals a user may enter, I created a table...
by: C.W.Holeman II | last post by:
I what to hide an input element and the following text. I have the selector for the input working and just need to grab the text following it. CSS: form{ display:table; text-align:center; }
by: plumba | last post by:
Hi all. I have a form which has a table and within the rows of the table are simple text input boxes. The ONLY problems with this is that when the form is posted (in text format) if the number of...
by: backups2007 | last post by:
I want to be able to pass rows of queried data to rows of input text boxes. As the example below shows, I have come up with this incomplete solution. But this code only passes the data to the first...
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.