I am pretty new to PHP and MySQL. Anyway, I currently try to write an application for Karaoke events.
The visitors of the event should be able to search online in a database for a specific song they would like to sing and then should be able to add this song to a kind of queue. The queue can then be display on a screen so the visitors can see how many people are singing before them and what they are singing etc.
My msql database is pretty simple. The table is called "songs" and I have 4 columns
id, title, artist, language
So first thing I need is a search form in which the people can type in any kind of searchterm and they get back the matching songs. It should not matter weather they search for artist or song, so they could type in "Love" as well es "Beatles" ot get the matching results.
So if the searchterm is "love" for example, the visitor gets back a table of maybe 100 lines of songs with the term "love" in the title.
I finally made this work, here is my code so far
<form action="searchform.php" method="get" >
<input type="text" name="searchquery" dir="ltr">
<input type="submit" value="start search">
</form>
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require 'inc/db.php';
$daten = array();
if(isset($_GET ['searchquery']) && $_GET['searchquery'] !== ' ')
{
$searchquery= trim($_GET['searchquery']);
echo "<p>Your search was: <b>". $searchquery ."</b></p>";
$search_for = "%$searchquery%";
$songsearch=$db->prepare("SELECT title,artist FROM songs WHERE artist LIKE ? OR title LIKE ?");
$songsearch->bind_param('ss', $search_for, $search_for);
$songsearch->execute();
$songsearch->bind_result($showtitle, $showartist);
echo "<table><tr><th>Title</th><th>Artist</th></tr>";
while ($songsearch->fetch()) {
echo "<tr><td>$showtitle</td>";
echo "<td>$showartist</td></tr>";
}
echo "</table>";
}
?>
inc/db.php
<?php
error_reporting(E_ALL);
$db = new mysqli('localhost', 'root', 'root', 'songlist');
$db->set_charset('utf8');
?>
Now I want the visitor to be able to click on one songtitle of the returned list (table) and with this click I want the song to be added to a queue. My idea was to just add another table to the database called "queue" with the columns id,title,artist and to add the choosen song to this table using the "INSERT" command.
Something like this...
$addtoqueue= $db->prepare("INSERT INTO queue (title, artist) VALUES (?, ?)");
After adding a song to the queue, it should forward to a "queue.php" which displays the data from the queue-table and refreshes every 30 seconds or so.
So I would probably need some kind of "action" around the
echo "<tr><td>$showtitle</td>";
line...like
<tr><td><a href="function to add dataset to the queue">$showtitle</a></td>
But I have no idea how to "select" the specific dataset on which the visitor has clicked so that all columns of this dataset are inserted to the "queue" table.
p.s. I now tried the following, however I get errors on this and not the expected result of course
$queuedata = array();
if (isset($_GET['action']) and $_GET['action']=='queue') {
if ( isset($_GET['id']) ) {
$id_fetch = (INT) $_GET['id'];
if ($id_fetch > 0)
{
$dsfetch=$db->prepare("SELECT id, title, artist FROM songs WHERE id = ?");
$dsfetch->bind_param('iss', $id, $title, $artist);
$queuedata[] = $dsfetch;
}
}
$addqueue = $queuedata->prepare("INSERT INTO queue (id, title, artist) VALUES ('$id, $title, $artist')");
$addqueue->bind_param('sss', $id, $title, $artist);
if ($addqueue->execute()) {
header('Location: queue.php?action=feedback');
die();
}
}
if (isset($_POST['action']) and $_POST['action']=='feedback') {
echo '<p class="feedbacksuccess">Song successfully added to queue</p>';
}
and for the output
echo "<tr><td><a href=\"?action=queue&id=$queuedata->id;\">$showtitle</td>";
I get this error:
Error: Notice: Trying to get property of non-object in C:\xampp\htdocs\karaoke-files\suchtest2.php on line 55