Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I correct my PHP code for this SQL query?
    primarykey
    data
    text
    <p>I've been messing with this for a while and I'm nearly there. Just need to get past this wall I've hit. </p> <p>I have the following tables:</p> <pre><code>tracks (trackid, tracktitle, albumid, composerid) albums (albumid, albumname) composers (composerid, composername) </code></pre> <p>I can insert a new record via PhpMyAdmin SQL tab with</p> <pre><code>INSERT INTO tracks (tracktitle, albumid, composerid) VALUES ('New Song', 1, 1); </code></pre> <p>and it works fine. </p> <p>My PHP form though isn't doing the same thing and I must have overlooked something. Please can someone check out the code for my addtrack page and tell me what is wrong? </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 tracks SET tracks.tracktitle='$tracktitle'" ; 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 tracks (trackid, albumid, composerid) VALUES " . "($trackid, $albs, $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 composerid, composername FROM composers'); if (!$composers) { exit('&lt;p&gt;Unable to obtain composer list from the database.&lt;/p&gt;'); } $albs = @mysql_query('SELECT albumid, albumname FROM albums'); 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['composerid']; $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['albumid']; $aname = htmlspecialchars($alb['albumname']); echo "&lt;label&gt;&lt;input type='checkbox' name='albs[]' value='$aid' /&gt;$aname&lt;/label&gt;&lt;br /&gt;\n"; } ?&gt; </code></pre> <p>Once I have this sorted, I can move on to expanding it and also sorting out the security issues. Someone on here recommended I look into PDO's which are a new thing to me. But one hurdle at a time....</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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