PHP/MySql Code problem

Associate
Joined
10 Dec 2003
Posts
122
Location
Im here, where are you?
I have spent ages going over and over this piece of code and I cannot find where the problem is and was hoping someone could help.

The page is: http://ccgi.vajudd.plus.com/search.php
Its for a university project which will be hosted on uni servers but Im using my Plus.net cgi space for testing.
The problem is the drop down list, it dynamically creates the list correctly but when one is selected and the "submit query" is hit I get the following error:

Fatal error: Call to undefined function: showerror() in /files/home1/vajudd/search.php on line 173

Googling the error merely took me to other pages with same errors-not helpful!
The source code for the drop down box is with the connection file name starred out:

<?php
include '********';
//Do connection stuff
if (!($connection = @ mysql_connect($hostname, $username, $password)))
die("Could not connect");
if (!mysql_select_db($databaseName, $connection))
showerror();

//If the region is empty then display the selection box
if (empty($_GET["typeBrand"]))
{ displaySelection($query, $connection);
}
else
{
displayResults($query, $connection);
}
mysql_close($connection);
function displaySelection($query, $connection)
{
//Create the Query - notice the use of 'distinct'
$query = " SELECT distinct Brand FROM Product";
// Run the query on the DBMS
if (!($result = @ mysql_query ($query, $connection)))
showerror();
// Find out how many rows are available
$rowsFound = @ mysql_num_rows($result);
// If the query has results ...
if ($rowsFound > 0)
{
echo "<p>Please select a brand name from the list below</p>";
//FORM ACTIONS ITSELF
echo "<form action='search.php' method='GET'>";
echo "<p><select name='typeBrand'>";
echo "<option> All</option>";
while ($row = @ mysql_fetch_array($result))
{
echo "<option/>".$row["Brand"];
}
echo "</select>";
}//Rows Found
echo "<p><input type='submit'></p></form>";
}//End of empty region Name

function displayResults($query, $connection)
{
$typeBrand = $_GET["typeBrand"];
$query = "SELECT Name, Description, Type, Interface, UnitPrice, Image FROM Product";

if ($typeBrand != "All")
$query .= " AND Brand = '$typeBrand' ";
$query .= " ORDER BY Name";
// echo $query;
// Run the query on the DBMS
if (!($result = @ mysql_query ($query, $connection)))
showerror();

// Find out how many rows are available
$rowsFound = @ mysql_num_rows($result);

// If the query has results ...
if ($rowsFound > 0)
{
// and start a <table>.
echo "\n<table border=1 cellpadding =10 class=paragraph>\n<tr>" .
"\n\t<th>Name</th>" .
"\n\t<th>Description</th>" .
"\n\t<th>Type</th>" .
"\n\t<th>Interface</th>" .
"\n\t<th>Price (£)</th>" .
"\n\t<th>Image</th>" .
"\n</tr>";

// Fetch each of the query rows
while ($row = @ mysql_fetch_array($result))
{
// Print one row of results
echo "\n<tr>" .
"\n\t<td>" . $row["Name"] . "</td>" .
"\n\t<td>" . $row["Description"] . "</td>" .
"\n\t<td>" . $row["Type"] . "</td>" .
"\n\t<td>" . $row["Interface"] . "</td>" .
"\n\t<td>" . $row["UnitPrice"] . "</td>" .
"\n\t<td><img src=" . $row["Image"] . " /></td>" .
"\n</tr>";
} // end while loop body

// Finish the <table>
echo "\n</table>";
} // end if $rowsFound body

// Report how many rows were found
echo "$rowsFound records found matching your
criteria<br>";
}//End else for region name
?>

Any help would be very much appreciated.
 
I had thought that but it doesnt have a problem with the first showerror() function, only the second one. If I do comment out the showerror() bit I get:
" records found matching your criteria"
Im stumped. :(
 
I have never defined a showerror() function, I guessed it was something predefined in php (Im still learning so sorry if what Im saying doesnt make sense). I also guessed that it showed the error because something went wrong with the query and that the problem was not the showerror() function, but it was displaying that as something else was undefined.
 
I am officially a numpty. Im looking all over the php for an error....and its the query thats at fault. I had:

SELECT Name, Description, Type, Interface, UnitPrice, Image FROM Product
AND Brand
ORDER BY Name

Which has a glaring error I cannot believe I missed, where is my WHERE and why have I got an AND?!

New code:

$query = "SELECT Name, Description, Type, Interface, UnitPrice, Image FROM Product WHERE";

if ($typeBrand != "All")
$query .= " Brand = '$typeBrand' ";
$query .= " ORDER BY Name";

And it works! Thanks for making me look at the query again, I knew it should be returning some value as the Brand names are read directly from database so non should have 0 values.
*Goes to find her basic SQL code book from first semester* :D
 
Thank you very much! Another section for my assignment is a report on security in websites...so that thread will prove most useful. Also have added the striptags() function as follows:

$typeBrand = $_GET["typeBrand"];
strip_tags($typeBrand);

Still works so I think I did it right. :D
 
Back
Top Bottom