Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding multiple items to a single MySql table with a PHP Loop
    primarykey
    data
    text
    <p>I'm working on a site whereby users can search the Spotify Library and click on results which in turn then add themselves to a 'playlist'. This playlist is then going to be added to a MySql database via PHP. All this is added is the spotify href, but obviously users are going to want to add more than one song to their playlist.</p> <p>Thanks to Brad's answer below, I now have an improved database structure, but I am still unsure as to how to loop through each song which has been added to the playlist, post all these songs and add them as separate rows in my 'songs' table.</p> <p>UPDATED This bit of code from 'spotify.js' adds a clicked song to a playlist div, and then adds a hidden input to be submitted. Each time a new song is clicked it creates a new input but the value of each input changes.</p> <pre><code>$('.spotify-result').click(function() { $(this).clone().appendTo($('.selected-list')); $('.submit-spotify').before('&lt;input type="hidden" id="track_href" class="track" value="" name="track_href" /&gt;'); $('.track').val($('.track-href', this).text()); }); </code></pre> <p>This is the hardcoded bit of form from 'spotify.php'...</p> <pre><code>&lt;form action="submit_spotify.php" method="post"&gt; &lt;input type="hidden" id="track_href" value="" name="track_href" /&gt; &lt;input type="submit" value="Submit Journix" /&gt; &lt;/form&gt; </code></pre> <p>This is a snippet of 'submit-spotify.php' which currently works but of course only adds the the first song to the database.</p> <pre><code>if($db_server) { mysql_select_db($db_database) or die ("&lt;p&gt;Couldn't find database.&lt;/p&gt;"); $playlist_query = "INSERT INTO playlists (user_id, route_id) VALUES ('$route_id', '$user_id')"; if (mysql_query($playlist_query)) { $song_query = "INSERT INTO songs (href) VALUES ('$href')"; if (mysql_query($song_query)) { $message = "&lt;p&gt;Congratulations, your playlist has been added.&lt;/p&gt;"; } else { $message = ("Insert failed. " . mysql_error() . "&lt;br/&gt;" . $song_query); } } else { $message = ("Insert failed. " . mysql_error() . "&lt;br/&gt;" . $playlist_query); } } else { $message = "Error: could not connect to the database."; } mysql_close($db_server); </code></pre> <p>I'm guessing I'll need a loop in there to run through each '.spotify-result' and get the .val() of each one, adding an new to the form for each one? I'm not at all where to go from there to input each one into a new row, or if this is even the correct approach.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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