I don't what's wrong with my coding.

Search Result

<?php $db_host ='localhost'; $db_user = 'root'; $database = 'lib'; $table = 'book'; $link=mysql_connect ("localhost", "root", "") or die(mysql_error()); mysql_select_db('lib')or die("Couldn't open $database: ".mysql_error());

$author=$_POST[‘author’];
$title=$_POST[‘title’];
$isbn=$_POST[‘isbn’];
$publication=$_POST[‘publication’];
$edition=$_POST[‘edition’];
$noofcopies=$_POST[‘noofcopies’];
$category=$_POST[‘category’];
$datepurchase=$_POST[‘datepurchase’];
$price=$_POST[‘price’];

if ($author == “”)
{
$author = ‘%’;
}
if ($title == “”)
{
$title = ‘%’;
}
if ($isbn == “”)
{
$isbn = ‘%’;
}
if ($publication == “”)
{
$publication = ‘%’;
}
if ($edition == “”)
{
$edition = ‘%’;
}
if ($noofcopies == “”)
{
$noofcopies = ‘%’;
}
if ($category == “”)
{
$category = ‘%’;
}
if ($datepurchase == “”)
{
$datepurchase = ‘%’;
}
if ($price == “”)
{
$price = ‘%’;
}
$result = mysql_query (“SELECT * FROM book WHERE author LIKE ‘$author%’ , title LIKE ‘$title%’ , isbn LIKE ‘$isbn%’ , publication LIKE ‘$publication%’ , edition LIKE ‘$edition%’ , noofcopies LIKE ‘$noofcopies%’ , category LIKE ‘$category%’ , datepurchase LIKE ‘$datepurchase%’ AND price LIKE ‘$price%’”);
$num_rows = mysql_num_rows ($result);

echo “There are $num_rows records.

”;

if ($row = mysql_fetch_row($result))
{
do {
echo “

\n”;
echo “\n”;
foreach ($row as $field)
echo “\t\n”;
echo “\n”;
echo “
$field
\n”;
}
while($row = mysql_fetch_row($result));
}
else
{
echo “Sorry, no records were found!”;
}
mysql_close($link);
?>

Everything…

  1. It’s not indented
  2. No doctype
  3. No meta tags
  4. center? What is this 1999???
  5. db connection in the page… so many things wrong here
  6. Old style mysql_query which have been deprecated for AGES
  7. Creating useless variables
  8. If all values are empty they become % why do this for each individual when you can put them into an array, do all at the same time and save on resources?
  9. Unnecessary DO loop when you could have just used a single WHILE loop
  10. Not checking if the form was submitted – this will cause headaches at some point in the future
  11. Using a form & button to go to the homepage? Why oh why

Other than that, if you could give us the following, we may be able to help!

  1. Description of the problem
  2. Steps you took to replicate the problem
  3. Expected outcome
  4. Actual outcome

Twice… :-X

Sponsor our Newsletter | Privacy Policy | Terms of Service