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($s ql);
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:
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:
And I want to get this
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
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($s ql);
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:
Code:
$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 />";
}
?>
Code:
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
Code:
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
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
Comment