Kurashu
01-16-2005, 03:18 AM
I can't wrap my head around this. I am trying to come up with a way to search my music database. However, I am occuring multiple problems
One, my GET data wants to resolve to a, or some other alphanumeric characters, and two, the big one, is my SQL data wants to mess up (returns bands that shouldn't be in the search, returns multiple results, etc) because the the PHP variables used to search it. Those are the two biggest ones.
{
$for = $_POST['for'];
$query = $_POST['query'];
if ( (strlen($query) <= 0) || (strlen($for) <= 0) || !isset($search[$for]))
{
print "<form action=\"lyrics3.php?action=search\" method=\"post\">\n";
print "<table align=\"center\">\n";
print "<tr bgcolor=\"#666666\"><th colspan=\"3\">What For?</th</tr>\n";
print "<tr bgcolor=\"#666666\"><td>\n";
print "<select name=\"for\">\n";
foreach ($search as $k=>$v)
{
print '<option value="' . $k .'">' . $v . '</option>\n';
}
print "</select>\n";
echo "</td>\n";
echo "<td>\n";
print "<input type=/\text\" name=\"query\" />";
echo "</td>";
echo "<td>";
print "<input type=\"submit\" title=\"Change\" value=\"Change\" />";
print "</td>\n</tr>\n";
print '</table>' . "\n\r";
print "</form>\n";
echo '</div>';
echo '<br />';
}
elseif ( (strlen($query) > 0) && (strlen($for) > 0) && isset($search[$for]))
{
echo '<div class="content">' . "\n\r";
$sql = "SELECT s.id AS s_id, s.song AS song, s.hits AS hits, a.name AS a_name, a.id AS a_id FROM gb_songs s, gb_artists a WHERE " . $for . " ILIKE " . "'%". $query . "%' AND gb_songs.artist = gb_artists.id ORDER BY " . $for . " LIMIT " . $limit . " OFFSET " . $offset;
$sql = pg_query($sql);
if (!pg_num_rows($sql))
{
echo '<span>Error</span>' . "\n\r";
echo '<p>No results</p>' . "\n\r";
}
else
{
$x=0;
while($row = pg_fetch_array($sql,NULL,PGSQL_ASSOC))
{
$class = ($x % 2) ? 'con1' : 'con2';
$query[1] = array("action" => "band", "id" => $row['a_id']);
$query[2] = array("action" => "song", "id" => $row['s_id']);
echo '<p class="'. $class . '">' . make_link(append_query_string("lyrics3.php", $query), $row['a_name']) . ' :: ' . make_link(append_query_string("lyrics3.php", $query[2]), $row['song']). '( ' . $row['hits'] . ' )</p>' . "\n\r";
$x++;
}
}
echo '</div>' . "\n\r";
$limit, and $offset are handled else where in the script.
Here is what $search looks like.
$search = array('s.song' => 'Song Name', 'a.name' => 'Artist Name', 's.lyrics' => 'Lyrics');
http://www.greenbomber.com/lyrics3.php?action=search
Any help will be greatly apperciated.
One, my GET data wants to resolve to a, or some other alphanumeric characters, and two, the big one, is my SQL data wants to mess up (returns bands that shouldn't be in the search, returns multiple results, etc) because the the PHP variables used to search it. Those are the two biggest ones.
{
$for = $_POST['for'];
$query = $_POST['query'];
if ( (strlen($query) <= 0) || (strlen($for) <= 0) || !isset($search[$for]))
{
print "<form action=\"lyrics3.php?action=search\" method=\"post\">\n";
print "<table align=\"center\">\n";
print "<tr bgcolor=\"#666666\"><th colspan=\"3\">What For?</th</tr>\n";
print "<tr bgcolor=\"#666666\"><td>\n";
print "<select name=\"for\">\n";
foreach ($search as $k=>$v)
{
print '<option value="' . $k .'">' . $v . '</option>\n';
}
print "</select>\n";
echo "</td>\n";
echo "<td>\n";
print "<input type=/\text\" name=\"query\" />";
echo "</td>";
echo "<td>";
print "<input type=\"submit\" title=\"Change\" value=\"Change\" />";
print "</td>\n</tr>\n";
print '</table>' . "\n\r";
print "</form>\n";
echo '</div>';
echo '<br />';
}
elseif ( (strlen($query) > 0) && (strlen($for) > 0) && isset($search[$for]))
{
echo '<div class="content">' . "\n\r";
$sql = "SELECT s.id AS s_id, s.song AS song, s.hits AS hits, a.name AS a_name, a.id AS a_id FROM gb_songs s, gb_artists a WHERE " . $for . " ILIKE " . "'%". $query . "%' AND gb_songs.artist = gb_artists.id ORDER BY " . $for . " LIMIT " . $limit . " OFFSET " . $offset;
$sql = pg_query($sql);
if (!pg_num_rows($sql))
{
echo '<span>Error</span>' . "\n\r";
echo '<p>No results</p>' . "\n\r";
}
else
{
$x=0;
while($row = pg_fetch_array($sql,NULL,PGSQL_ASSOC))
{
$class = ($x % 2) ? 'con1' : 'con2';
$query[1] = array("action" => "band", "id" => $row['a_id']);
$query[2] = array("action" => "song", "id" => $row['s_id']);
echo '<p class="'. $class . '">' . make_link(append_query_string("lyrics3.php", $query), $row['a_name']) . ' :: ' . make_link(append_query_string("lyrics3.php", $query[2]), $row['song']). '( ' . $row['hits'] . ' )</p>' . "\n\r";
$x++;
}
}
echo '</div>' . "\n\r";
$limit, and $offset are handled else where in the script.
Here is what $search looks like.
$search = array('s.song' => 'Song Name', 'a.name' => 'Artist Name', 's.lyrics' => 'Lyrics');
http://www.greenbomber.com/lyrics3.php?action=search
Any help will be greatly apperciated.