Note that there are some explanatory texts on larger screens.

plurals
  1. POInsert data into SQL tables
    text
    copied!<p>I'm having trouble inserting data into a table. I have the following tables: track (id, tracktitle), album (id, albumtitle), composer (id, composername), albumtrack (PRIMARY: trackid, albumid, composerid). </p> <p>My PHP page allows you to add a track and then select the album and composer connected with it. It adds the track to the tracktable okay but it won't add it to an album. </p> <p>I keep looking around for how to do it and I keep getting a bit lost. Can anyone tell me how I should be correctly doing this? Thanks</p> <pre><code>if (isset($_POST['tracktitle'])): // A new track has been entered // using the form. $cid= $_POST['cid']; $tracktitle = $_POST['tracktitle']; $albs = $_POST['albs']; if ($cid == '') { exit('&lt;p&gt;You must choose an composer for this track. Click "Back" and try again.&lt;/p&gt;'); } $sql = "INSERT INTO track, albumtrack SET track.tracktitle='$tracktitle', albumtrack.albumid='$albs', albumtrack.composerid='$cid' " ; if (@mysql_query($sql)) { echo '&lt;p&gt;New track added&lt;/p&gt;'; } else { exit('&lt;p&gt;Error adding new track' . mysql_error() . '&lt;/p&gt;'); } $trackid = mysql_insert_id(); if (isset($_POST['albs'])) { $albs = $_POST['albs']; } else { $albs = array(); } $numAlbs = 0; foreach ($albs as $albID) { $sql = "INSERT IGNORE INTO albumtrack SET albumtrack.trackid='$trackid', albumtrack.albumid='$albs', albumtrack.composerid='$cid'"; if ($ok) { $numAlbs = $numAlbs + 1; } else { echo "&lt;p&gt;Error inserting track into album $albID: " . mysql_error() . '&lt;/p&gt;'; } } ?&gt; &lt;p&gt;Track was added to &lt;?php echo $numAlbs; ?&gt; albums.&lt;/p&gt; &lt;p&gt;&lt;a href="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;"&gt;Add another track&lt;/a&gt;&lt;/p&gt; &lt;p&gt;&lt;a href="tracks.php"&gt;Return to track search&lt;/a&gt;&lt;/p&gt; &lt;?php else: // Allow the user to enter a new track $composers = @mysql_query('SELECT id, composername FROM composer'); if (!$composers) { exit('&lt;p&gt;Unable to obtain composer list from the database.&lt;/p&gt;'); } $albs = @mysql_query('SELECT id, albumtitle FROM album'); if (!$albs) { exit('&lt;p&gt;Unable to obtain album list from the database.&lt;/p&gt;'); } ?&gt; &lt;form action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;" method="post"&gt; &lt;p&gt;Enter the new track:&lt;br /&gt; &lt;textarea name="tracktitle" rows="1" cols="20"&gt; &lt;/textarea&gt;&lt;/p&gt; &lt;p&gt;Composer: &lt;select name="cid" size="1"&gt; &lt;option selected value=""&gt;Select One&lt;/option&gt; &lt;option value=""&gt;---------&lt;/option&gt; &lt;?php while ($composer= mysql_fetch_array($composers)) { $cid = $composer['id']; $cname = htmlspecialchars($composer['composername']); echo "&lt;option value='$cid'&gt;$cname&lt;/option&gt;\n"; } ?&gt; &lt;/select&gt;&lt;/p&gt; &lt;p&gt;Place in albums:&lt;br /&gt; &lt;?php while ($alb = mysql_fetch_array($albs)) { $aid = $alb['id']; $aname = htmlspecialchars($alb['albumtitle']); echo "&lt;label&gt;&lt;input type='checkbox' name='albs[]' value='$aid' /&gt;$aname&lt;/label&gt;&lt;br /&gt;\n"; } ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload