Jump to content


Photo

Cant recive data from database when adding search function


  • Please log in to reply
4 replies to this topic

#1 tokkille

tokkille
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 09 March 2006 - 03:40 PM

Hi,

I have maneged to recive information from my database. but when i put in a search function on the homepage to search the database it dosent retrive the result in a table?!

This is what it looks like:

<html>
<head>
<title>G&ouml;tt mos</title>
<link href="../skolan.css" rel="stylesheet" type="text/css">
</head>

<body>
<h3 align="center">Goa Grejer </h3>
<p align="center">S&ouml;k vem som &auml;r g&ouml;ttigast </p>

<hr>

<?php
if ($searchstring)
{
$sql="SELECT * FROM maraton1 WHERE $searchtype LIKE '%$searchstring%' ORDER BY Plats ASC";
include("XXX.php");
$result = mysql_query($sql,$db);
echo "<table border=1 align=center cellspacing=2 cellpadding=3>\n";
echo "<TR><TH>Plats<TH>Person<TH>Poäng</TR>\n";
while ($rad = mysql_fetch_array($result))
{
echo "<TR><TD>$rad[Plats]<TD>$rad[Person]<TD>$rad[Poäng]\n";
}
echo "</TABLE>";
}
else
{
?>
<form method="POST" action="<?php $PHP_SELF ?>">

<table border="1" align="center" cellspacing=2 cellpadding=3>
<tr><td>Sök här</td>
<td>Söktyp</td></tr>
<tr>
<td><input type="text" name="searchstring" size="40"></td>
<td><select size="1" name="searchtype">
<option selected value="Person">Person</option>
<option value="Plats">Plats</option>

</select></td>
</tr>
</table>

<p align="center">
<input type="submit" value="Sök" name="B1">
<input type="reset" value="Töm sökraden ovan" name="B2">
</p>
</form>

<?php
}
?>



<hr>
<p align="center"><a href="../../../../Application Data/Macromedia/Dreamweaver 8/Configuration/ServerConnections/www.vahlne.se//index.php">Tillbaks</a>
<p>
</body>
</html>


Thanks for any help!

/Tokkille

#2 lessthanthree

lessthanthree
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts
  • LocationUK

Posted 09 March 2006 - 03:43 PM

There's a few errors.

But for a start:

<?php
if ($searchstring)
{
$sql="SELECT * FROM maraton1 WHERE $searchtype LIKE '%$searchstring%' ORDER BY Plats ASC";
include("XXX.php");
$result = mysql_query($sql,$db);

should be

<?php
if (isset($_POST["searchstring"]))
{
$searchstring = $_POST["searchstring"] //for security make sure you clean this value!!!!
$searchtype = $_POST["searchtype"] //and this one!!!

$sql="SELECT * FROM maraton1 WHERE $searchtype LIKE '%".$searchstring%' ORDER BY Plats ASC";
include("XXX.php");
$result = mysql_query($sql,$db);


call me a safe bet, i'm betting i'm not

#3 tokkille

tokkille
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 09 March 2006 - 04:04 PM

Thanks less then three!!

My new code looks like this:

<html>
<head>
<title>G&ouml;tt mos</title>
<link href="file:///C|/Documents and Settings/vahlne/Desktop/PHP/Kap48/skolan.css" rel="stylesheet" type="text/css">
</head>

<body>
<h3 align="center">Goa Grejer </h3>
<p align="center">S&ouml;k vem som &auml;r g&ouml;ttigast </p>

<hr>

<?php
if (isset($_POST["searchstring"]))
{
$searchstring = $_POST["searchstring"]
$searchtype = $_POST["searchtype"]

$sql="SELECT * FROM maraton1 WHERE $searchtype LIKE '%$searchstring%' ORDER BY Plats ASC";
include("XXX.php");
$result = mysql_query($sql,$db);

echo "<table border=1 align=center cellspacing=2 cellpadding=3>\n";
echo "<TR><TH>Plats<TH>Person<TH>Poäng</TR>\n";
while ($rad = mysql_fetch_array($result))
{
echo "<TR><TD>$rad[Plats]<TD>$rad[Person]<TD>$rad[Poäng]\n";
}
echo "</TABLE>";
}
else
{
?>
<form method="POST" action="<?php $PHP_SELF ?>">

<table border="1" align="center" cellspacing=2 cellpadding=3>
<tr><td>Sök här</td>
<td>Söktyp</td></tr>
<tr>
<td><input type="text" name="searchstring" size="40"></td>
<td><select size="1" name="searchtype">
<option selected value="Person">Person</option>
<option value="Plats">Plats</option>

</select></td>
</tr>
</table>

<p align="center">
<input type="submit" value="Sök" name="B1">
<input type="reset" value="Töm sökraden ovan" name="B2">
</p>
</form>

<?php
}
?>



<hr>
<p align="center"><a href="index.php">Tillbaks</a>
<p>
</body>
</html>

But know it complains about:
Parse error: parse error, unexpected T_VARIABLE in /customers/vahlne.se/vahlne.se/httpd.www/sok funktion.php on line 17


#4 lessthanthree

lessthanthree
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts
  • LocationUK

Posted 09 March 2006 - 04:20 PM

add semicolons to the end of the strings...my fault.

$searchstring = $_POST["searchstring"];
$searchtype = $_POST["searchtype"];
call me a safe bet, i'm betting i'm not

#5 tokkille

tokkille
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 09 March 2006 - 04:52 PM

Stupid of me could almost have figured that one out my self....


Thanks alot it works fin now!

/Tokkille




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users