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: -
$records = mysql_query("SELECT DiscID, DiscName FROM Discography");
-
-
while($options = mysql_fetch_array($records)){
-
$DiscID = $options['DiscID'];
-
$DiscName = htmlspecialchars($options['DiscName']);
-
echo "
-
<table align='center'>
-
<tr>
-
<td width='250'>
-
$DiscName
-
</td>
-
<td width='250'>
-
<input name='DiscID[]' type='checkbox'/>
-
</td>
-
</tr>
-
<td width='250'> Song Number:
-
</td>
-
<td width='250'>
-
<input name='Orden[]' type='text' size='3' />
-
</td>
-
</tr>
-
</table>
-
<br />";
-
}
-
?>
-
-
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: - INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '2', '')
-
-
Song 17 inserted into Disc 2 in order
-
INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '2', '2')
-
-
Error inserting Song into Disc 2: Duplicate entry '2-17' for key 1
-
INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '2', '10')
-
-
Error inserting Song into Disc 2: Duplicate entry '2-17' for key 1
-
INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '2', '')
-
-
Error inserting Song into Disc 2: Duplicate entry '2-17' for key 1
-
INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '2', '10')
-
-
Error inserting Song into Disc 2: Duplicate entry '2-17' for key 1
-
INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '2', '')
-
-
Error inserting Song into Disc 2: Duplicate entry '2-17' for key 1
-
INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '3', '')
-
-
Song 17 inserted into Disc 3 in order
-
INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '3', '2')
-
-
Error inserting Song into Disc 3: Duplicate entry '3-17' for key 1
-
INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '3', '10')
-
-
Error inserting Song into Disc 3: Duplicate entry '3-17' for key 1
-
INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '3', '')
-
-
Error inserting Song into Disc 3: Duplicate entry '3-17' for key 1
-
INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '3', '10')
-
-
Error inserting Song into Disc 3: Duplicate entry '3-17' for key 1
-
INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '3', '')
-
-
Error inserting Song into Disc 3: Duplicate entry '3-17' for key 1
-
INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '5', '')
-
-
Song 17 inserted into Disc 5 in order
-
INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '5', '2')
-
-
Error inserting Song into Disc 5: Duplicate entry '5-17' for key 1
-
INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '5', '10')
-
-
Error inserting Song into Disc 5: Duplicate entry '5-17' for key 1
-
INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '5', '')
-
-
Error inserting Song into Disc 5: Duplicate entry '5-17' for key 1
-
INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '5', '10')
-
-
Error inserting Song into Disc 5: Duplicate entry '5-17' for key 1
-
INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '5', '')
-
-
Error inserting Song into Disc 5: Duplicate entry '5-17' for key 1
-
And I want to get this -
INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '2', '2')
-
-
Song 17 inserted into Disc 2 in order 2
-
-
INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '3', '10')
-
-
Song 17 inserted into Disc 3 in order 10
-
-
INSERT INTO DiscoSong (SongID, DiscID, Orden) VALUES ('17', '5', '10')
-
-
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
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: -
$songID = $_POST['SongID']
-
$diskID = $_POST['DiskID']; // This would be an array from a DiskID[] input
-
$orden = $_POST['Orden'] // And this from a Orden[] input. (one for each DIskID)
-
-
foreach($DiskID as $_index => $_diskID) {
-
$sql = "
-
INSERT INTO tbl(DiskID, SongID, Orden)
-
VALUES($_diskID, $songID, {$order[$_index]}";
-
}
-
Thank you very much man, my problem is solved :D
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: 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
|
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...
|
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:
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...
|
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: 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...
|
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,...
|
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...
|
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...
| |