Jump to content

Getting results from SQL


blackdogupya

Recommended Posts

Okay, after successfully getting a form to input data to a MySQL database, I'm now trying to get the data back out by searching.

 

Search form code:

 

 <form name="search" method="post" action="process_search.php">
Seach for: <input type="text" name="find" /> in 
<select name="columns">
<option value="">Please choose one:</option>
<option value="status">Status</option>
<option value="date">Date</option>
<option value="species">Species</option>
<option value="breed">Breed</option>
<option value="sex">Sex</option>
<option value="primary_colour">Primary Colour</option>
<option value="colour">Colour</option>
<option value="distinctive_traits">Distinctive Traits</option>
<option value="fur_length">Fur Length</option>
<option value="age">Age</option>
<option value="desexed">Desexed</option>
<option value="microchipped">Microchipped</option>
<option value="suburb">Suburb</option>
<option value="pound_area">Pound Area</option>
<option value="contact">Contact</option>
<option value="link">Link</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>

 

All good there!  Form displays how I want it to, and submits correctly.

 

The bit I'm struggling with is the process_search.php side of things.  This is what I have (note that i'm trying to get the results into a formatted table and I'd like more results if the match is in more than one column):

 

<? 
//Here we display stuff if they have submitted the form
if ($searching =="yes") 
{ 
echo "<h2>Results</h2><p>"; 

//If they stuffed up and didn't search for anything, we show them this 
if ($find == "") 
{ 
echo "<p>Oh, Bianca, you need to enter SOMETHING to search!"; 
exit; 
} 

//If everything is all good, we connect to the database
mysql_connect("localhost", "******", "******") or die(mysql_error()); 
mysql_select_db("******") or die(mysql_error()); 

//Let's not forget the register globals off crap
$status = $_POST['status'];
$date = $_POST['date'];
$species = $_POST['species'];
$breed = $_POST['breed'];
$sex = $_POST['sex'];
$primary_colour = $_POST['primary_colour'];
$colour = $_POST['colour'];
$distinctive_traits = $_POST['distinctive_traits'];
$fur_length = $_POST['fur_length'];
$age = $_POST['age'];
$desexed = $_POST['desexed'];
$microchipped = $_POST['microchipped'];
$suburb = $_POST['suburb'];
$pound_area = $_POST['pound_area'];
$contact = $_POST['contact'];
$link = $_POST['link'];
$columns = $_POST['columns'];

// We perform a bit of filtering 
$find = strtoupper($find); 
$find = strip_tags($find); 
$find = trim ($find); 

//Now we search for our search term, in the field the user specified 
$data = mysql_query("SELECT * FROM animal_info WHERE upper($columns) LIKE'%$find%'"); 

//And we display the results 
where($result = mysql_fetch_array( $data )) 
{ 
    echo "<tr>";
    echo "<td>".$row['status']."</td>";
    echo "<td>".$row['date']."</td>";
    echo "<td>".$row['species']."</td>";
    echo "<td>".$row['breed']."</td>";
echo "<td>".$row['sex']."</td>";
echo "<td>".$row['primary_colour']."</td>";
echo "<td>".$row['colour']."</td>";
echo "<td>".$row['distinctive_traits']."</td>";
echo "<td>".$row['fur_length']."</td>";
echo "<td>".$row['age']."</td>";
echo "<td>".$row['desexed']."</td>";
echo "<td>".$row['microchipped']."</td>";
echo "<td>".$row['suburb']."</td>";
echo "<td>".$row['pound_area']."</td>";
echo "<td>".$row['contact']."</td>";
echo "<td>".$row['link']."</td>";
    echo "</tr>";
} 
else
{
echo "ERROR: ".mysql_error();
}
//This counts the number or results - and if there wasn't any it gives them a little message explaining that 
$anymatches=mysql_num_rows($data); 
if ($anymatches == 0) 
{ 
echo "Nope, couldn't find anything here!  Maybe refine your search criteria?<br><br>"; 
} 

//And we remind them what they searched for 
echo "<b>Searched For:</b> " .$find; 
} 
?> 

 

Trouble with this is that I'm not getting any errors OR results! :(

 

ANY help would be appreciated HUGELY!

 

Cheers,

 

Dave

Link to comment
Share on other sites

You also have to change $find to $_POST['find'].

Plus i think your error reporting is off. switch it to on.

ini_set('display_errors',1); 
error_reporting(E_ALL);

 

I've actually declared the $_POST['find'] in the list after posting this, so that's now there.

 

I've added the error reporting, but again, nothing is showing up.  No errors. :(

 

This is starting to give me the s*its! :)

 

Thanks for your help so far!

Link to comment
Share on other sites

it's still just returning a blank page.

 

What does a 'view source' in your browser of the blank page show?

 

Edit: You actually have a fatal parse error on line 47, because there is no such thing as a where loop. You meant to use a while loop.

 

You should have php's error_reporting set to E_ALL (or even better a -1) and display_errors set to ON in your master php.ini on your development system so that all the php detected errors will be reported and displayed. You will save a TON of time.

Link to comment
Share on other sites

You also have the end of an else {} statement, with some mysql error reporting logic in it, tacked onto the end of that loop construct. I suspect you intended to test if the value that mysql_query returned was a result resource or not.

Link to comment
Share on other sites

You also have the end of an else {} statement, with some mysql error reporting logic in it, tacked onto the end of that loop construct. I suspect you intended to test if the value that mysql_query returned was a result resource or not.

 

Okay, I've changed everything you have all suggested, here's the code as it stands now:

 

<? 
//Here we display stuff if they have submitted the form
if ( $_POST['searching'] == 'yes' )
{ 
echo "<h2>Results</h2><p>"; 

//If they stuffed up and didn't search for anything, we show them this 
if ($find == "") 
{ 
echo "<p>Oh, Bianca, you need to enter SOMETHING to search!"; 
exit; 
} 

//If everything is all good, we connect to the database
mysql_connect("localhost", "******", "******") or die(mysql_error()); 
mysql_select_db("******") or die(mysql_error()); 

//Let's not forget the register globals off crap
$status = $_POST['status'];
$date = $_POST['date'];
$species = $_POST['species'];
$breed = $_POST['breed'];
$sex = $_POST['sex'];
$primary_colour = $_POST['primary_colour'];
$colour = $_POST['colour'];
$distinctive_traits = $_POST['distinctive_traits'];
$fur_length = $_POST['fur_length'];
$age = $_POST['age'];
$desexed = $_POST['desexed'];
$microchipped = $_POST['microchipped'];
$suburb = $_POST['suburb'];
$pound_area = $_POST['pound_area'];
$contact = $_POST['contact'];
$link = $_POST['link'];
$columns = $_POST['columns'];
$find = $_POST['find'];

// We perform a bit of filtering 
$find = strtoupper($find); 
$find = strip_tags($find); 
$find = trim ($find); 

//Now we search for our search term, in the field the user specified 
$data = mysql_query("SELECT * FROM animal_info WHERE upper($columns) LIKE'%$find%'"); 

//And we display the results 
while($row = mysql_fetch_array( $data )) 
{ 
    echo "<table>";
echo "<tr>";
    echo "<td>".$row['status']."</td>";
    echo "<td>".$row['date']."</td>";
    echo "<td>".$row['species']."</td>";
    echo "<td>".$row['breed']."</td>";
echo "<td>".$row['sex']."</td>";
echo "<td>".$row['primary_colour']."</td>";
echo "<td>".$row['colour']."</td>";
echo "<td>".$row['distinctive_traits']."</td>";
echo "<td>".$row['fur_length']."</td>";
echo "<td>".$row['age']."</td>";
echo "<td>".$row['desexed']."</td>";
echo "<td>".$row['microchipped']."</td>";
echo "<td>".$row['suburb']."</td>";
echo "<td>".$row['pound_area']."</td>";
echo "<td>".$row['contact']."</td>";
echo "<td>".$row['link']."</td>";
    echo "</tr>";
echo "</table>";
} 

//This counts the number or results - and if there wasn't any it gives them a little message explaining that 
$anymatches=mysql_num_rows($data); 
if ($anymatches == 0) 
{ 
echo "Nope, couldn't find anything here!  Maybe refine your search criteria?<br><br>"; 
} 

//And we remind them what they searched for 
echo "<b>Searched For:</b> " .$find; 
} 
?> 

 

Still giving me a blank page.  When I view source, that's blank also!

 

My eyes are going blurry with looking at it so hard! :P

 

Any other ideas.  Thanks again for your help!

 

Cheers,

 

Dave

Link to comment
Share on other sites

$_POST['searching']

 

^^^ Does your form have a field named 'searching', with a value of 'yes'? Is your form method set to post? Have you set the error_reporting and display_errors settings to the suggested values (and confirmed that they actually changed to those values after you restarted your web server)?

Link to comment
Share on other sites

$_POST['searching']

 

^^^ Does your form have a field named 'searching', with a value of 'yes'? Is your form method set to post? Have you set the error_reporting and display_errors settings to the suggested values (and confirmed that they actually changed to those values after you restarted your web server)?

 

Well, I have a little egg on my face here! :S

 

I was uploading the modified file to the wrong directory!!!

 

It's working!  Needs a little refining for the results to display in a good way, but I can sort that!

 

Thank you all so much!  It's great to have a resource like this to help, and you guys are all amazing for putting in your free time to help idiots like me!

 

Thanks again!

 

Cheers,

 

Dave

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.