473,416 Members | 1,698 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,416 software developers and data experts.

Problem inserting data using checkboxes and input type=text

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
2 3050
Atli
5,058 Expert 4TB
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
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

2
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...
6
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"...
1
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...
4
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...
12
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...
3
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...
11
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; }
1
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...
4
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.