0

On the members page of my website, I need to have an option so that the members can look up the info of the other members. So far what I have done is create a search form...

<form id="searchform" name="searchform" action="searchresults.php" method="post">
              Search Membership
              <input type="text" name="search" id="textfield" placeholder="Search Members" />
              <input type="submit" name="button" id="button" value="Search" />
</form>

I've also created a table in my database with all of the members info. Now I just need help with getting the form to communicate with the database table and take what is inputted into the search form and display the results on my results page.

I am not familiar with this and any help would be appreciated. Thanks.

UPDATE: So now I have the following PHP code on my searchresults.php page...

<?php

error_reporting(-1);

$host=""; 
$username="";
$password="";
$db_name="";
$tbl_name="MOAMemberSearch";

mysql_connect($host, $username, $password) or die("cannot connect"); 
mysql_select_db($db_name) or die("cannot select DB");

$sql = "SELECT * FROM MOAMemberSearch";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);

if (false === $result) {
    echo mysql_error();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Member Search - MOA</title>

<link href="css/style.css" rel="stylesheet" type="text/css" />

<link href="css/jquery.ennui.contentslider.css" rel="stylesheet" type="text/css" media="screen,projection" />

<style type="text/css">
#content_wrapper #content table {
    color: #000;
}
</style>
</head>

<body>
<div id="menu_wrapper">

    <div id="menu">

        <ul>
            <li><a href="index.html">Home</a></li>
            <li><a href="boardofdirectors.html">Board</a></li>
            <li><a href="members.php" class="current">Members</a></li>
            <li><a href="ratingcard.html">Rating Card</a></li>
            <li><a href="join.html">Join MOA</a></li>
            <li><a href="contact.html">Contact Us</a></li>
        </ul>       

    </div> <!-- end of menu -->
</div> <!-- end of menu wrapper -->

<div id="header_wrapper">
  <div id="header"><!-- end of slider -->
  </div>
  <!-- header -->

</div> 
<!-- end header wrapper -->

<div id="content_wrapper">
    <div id="content">

      <h1>Search Results</h1>
      <table width="930" border="0" style="font-size:12px">
    <tr>
        <th>Sports</th>
        <th>Last Name</th>
        <th>First Name</th>
        <th>Address</th>
        <th>City</th>
        <th>State</th>
        <th>Zip</th>
        <th>Phone 1</th>
        <th>Phone 2</th>
        <th>Email</th>
    </tr>
<tr>
    <td><?php echo $rows['Sports']; ?></td>
    <td><?php echo $rows['LastName']; ?></td>
    <td><?php echo $rows['FirstName']; ?></td>
    <td><?php echo $rows['Address']; ?></td>
    <td><?php echo $rows['City']; ?></td>
    <td><?php echo $rows['State']; ?></td>
    <td><?php echo $rows['Zip']; ?></td>
    <td><?php echo $rows['Phone1']; ?></td>
    <td><?php echo $rows['Phone2']; ?></td>
    <td><?php echo $rows['Email']; ?></td>
</tr>
      </table>

Currently getting an error where the search results should be appearing

Patrick
  • 7
  • 1
  • 8
  • Yes, it does. This error is very common and easy to troubleshoot following basic steps. Your query is failing. You need to figure out why. At least one answer explains that and how to do it. – John Conde Oct 19 '14 at 16:50
  • I've already implemented the code from the "new" answer page you provided. Same error shows. Obviously it does not solve the problem. So mind explaining? – Patrick Oct 19 '14 at 16:52
  • Follow the steps in [this answer](http://stackoverflow.com/a/11674313/250259) to troubleshoot this. For starters, you haven't even checked to see what error MySQL is reporting. That is always step one. – John Conde Oct 19 '14 at 16:54
  • @John Conde OR John let me ask you this as an alternative, would it be easier to do this by not using a server database and rather a different type of file? – Patrick Oct 19 '14 at 16:55
  • You *definitely* want to use a database for this. For what you're doing it is the right solution. – John Conde Oct 19 '14 at 16:57
  • @John Conde Okay, I used the code from your answer on that page. I am not longer getting that error, but now in the search results area of my page I'm getting a new error that reads " Undefined variable: rows in D:\Hosting\....\searchresults.php on Line 81, 82, etc.." in each of the search result spots. I will update code above to show what it looks like now. – Patrick Oct 19 '14 at 17:10
  • $make sure `$rows` is the name of the variable you are storing your results in. PHP claims it does not exist. – John Conde Oct 19 '14 at 17:19
  • @John Conde And how do I do that? Please forgive me as I'm rather new to PHP and really only need it for a couple items on my website. Your help is appreciated. – Patrick Oct 19 '14 at 17:22
  • Look where you call `mysql_fetch_assoc()` or `mysql_fetch_array()`. That's the variable you are assigning those results to. – John Conde Oct 19 '14 at 17:24
  • @John Conde or I should really say, what am I missing from my code above to do that? – Patrick Oct 19 '14 at 17:53
  • The code above is incomplete so I can't say for sure. But it would look like `while ($row = mysql_fetch_assoc($result)) {` – John Conde Oct 19 '14 at 18:08
  • @John Conde I've updated the question with all of the page code from the top of the page all the way through the results table. Does that help you? – Patrick Oct 19 '14 at 18:24
  • Change everywhere that saws `$rows` too just be `$row` – John Conde Oct 19 '14 at 18:25
  • @John Conde Thank you. I did that and I now have a search result come back. However, it is not what I searched for. It is only showing the first entry of the database. – Patrick Oct 19 '14 at 18:30
  • You need to loop through the results if you want all of the rows returned to be displayed. A quick google search will show you how. – John Conde Oct 19 '14 at 18:41
  • @John Conde So I googled that and nothing is particularity clear about how to implement this. What I have tried from doing that has done nothing. – Patrick Oct 19 '14 at 19:00
  • [The manual](http://php.net/manual/en/function.mysql-fetch-assoc.php) has a clear example of how to do it. – John Conde Oct 19 '14 at 19:14
  • @John Conde Okay, So I read that and it makes absolutely no sense to me. It says"you either need to access the result with numeric indices by using mysql_fetch_row() or add alias names." But that has nothing shown in the examples about that. So should I re-code so that my code looks like what is on that example page or what? – Patrick Oct 19 '14 at 22:48

1 Answers1

1

First, use mysqli. This will save you headaches and improve security. Then, you are looking at something like this as a form recieve page.

<?php
$con = mysqli_connect(address,user,pass,database);
if(isset($_POST['search'])) {
$result =  mysqli_query($con,"SELECT * FROM members WHERE Name='".mysqli_real_escape_string($con,$_POST['search'])."'");
If(mysqli_num_rows($result)!=0) {
$row = mysqli_fetch_array($result);
//$row is associated array of member data, echo what you want here
}
Else {
//no results
}
}
?>
user3105700
  • 365
  • 5
  • 16
  • So this PHP code would go on my results page? – Patrick Oct 19 '14 at 02:02
  • Yes. But don't copy and paste, you have to put in database auth and have the code do something with the data. Good luck! – user3105700 Oct 19 '14 at 02:03
  • Okay. So I linked the search form to the results page. I put the above code into the results and inputted my database info. And it doesn't do anything. What am I supposed to put in the FROM and WHERE in $result? Also, what am I supposed to put in $row? Is that what takes what the user puts in the form and creates the search? – Patrick Oct 19 '14 at 03:44
  • FROM is your table name. Don't replace FROM, just the name after it. WHERE should be the Column you want to search on. Then $row is an associative array where $row['ID'] would be the ID column from the row you've selected. So echo it out onto an HTML table maybe? – user3105700 Oct 19 '14 at 03:48
  • Okay, so call me stupid, but I get everything now all the way up to $row. I have 3 searchable fields and in the results it needs to show all 10 fields of the database entry. PHP is really not my thing and I'm having a tough time writing an array to show all that is needed. Does any of this make sense? Any suggestions? Thanks – Patrick Oct 19 '14 at 06:24
  • When you query, you get a mysqli_result object, which represents all the rows you have received. When you fetch an array, you are getting one row of the results in an array. Arrays can be accessed like array[key] so the keys of the array are the column names and the values are the specific values for that row. Echo each piece of info for the member into an HTML table, and you should be fine. – user3105700 Oct 19 '14 at 06:40
  • I've updated the question with the code I currently have. I'm still having a problem. I get the error... " Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in D:\Hosting\..... on line 75 ". Line 75 is . Thoughts? – Patrick Oct 19 '14 at 08:11
  • Whoops. I forgot to add no results safety. Before you fetch the array, you should check that there is results. Use if(mysqli_num_rows($result) != 0) before $row =. That ensures that you actually pulled rows from your query before you try to get one. – user3105700 Oct 19 '14 at 14:42
  • 1
    Other than that, this should be immune to SQL injection. Personally I don't like prepared statements, but someone can show you how to do it that way if they want. – user3105700 Oct 19 '14 at 14:44
  • I added that and I'm still getting the same error ... " Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in D:\Hosting\..... on line 12 ". Line 12 reads as ... if(mysqli_num_rows($result) != 0) $row = mysqli_fetch_array($result); . So what is this error telling me? I mis-typed in a previous comment when I said line 75, the error is associated with line 12. – Patrick Oct 19 '14 at 16:20
  • See question for updated code – Patrick Oct 19 '14 at 16:28