Jump to content

[SOLVED] Select Statement in php file


seriousdamage

Recommended Posts

Hi,

can someone help me out with my select statement?

I have 2 files:

First is an html search form.

the second is the result php file.

The statement should ask to search in the entire databese for what ever is in the html form and then bring back to full row.

 

But I only get the headers back.

 

Please help me.

 

<html>
<body>
<form name="search" method="POST" action="search.php"> 
<p>Search: <input name="search" type="text" id="search"> 

<p><input type="submit" name="submit" value="submit"> 
</form>
</body>
</html>

 

$host = "localhost"; 
$user = "*****"; 
$pass = "*****"; 
$dbname = "contacts"; 

$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>"); 
mysql_select_db($dbname); 


$result = mysql_query("SELECT * FROM contacts where '*' ='" . $_POST['search'] ."'") or die (mysql_errno().": ".mysql_error()."<BR>");


echo "<table border=0 width=1400>
<tr bgcolor=#87ceeb>
<td width=100><b>First Name</td><td width=100><b>Last Name</td>
<td width=100><b>Company Name</td><td width=100><b>Website</td>
<td width=100><b>Street + Number</td><td width=100><b>City</td>
<td width=100><b>Post Code</td><td width=100><b>Country</td>
<td width=100><b>E-Mail 1</td><td width=100><b>E-Mail 2</td>
<td width=100><b>Phone</td><td width=100><b>Mobile</td>
<td width=100><b>Christmas Card</td><td width=100><b>Birthday</b></td>


</tr>";


while($row = mysql_fetch_array($result)) {  

echo "<tr bgcolor=#4682b4>";
  echo "<td><font color=#FFFFFF>" . $row['first_name'] . "</td>";
  echo "<td><font color=#FFFFFF>" . $row['last_name'] . "</td>";
  echo "<td><font color=#FFFFFF>" . $row['company_name'] . "</td>";
  echo "<td><font color=#FFFFFF>" . $row['website'] . "</td>";
  echo "<td><font color=#FFFFFF>" . $row['street'] . "</td>";
  echo "<td><font color=#FFFFFF>" . $row['city'] . "</td>";
  echo "<td><font color=#FFFFFF>" . $row['zip'] . "</td>";
  echo "<td><font color=#FFFFFF>" . $row['country'] . "</td>";
  echo "<td><font color=#FFFFFF>" . $row['mail1'] . "</td>";
  echo "<td><font color=#FFFFFF>" . $row['mail2'] . "</td>";
  echo "<td><font color=#FFFFFF>" . $row['phone'] . "</td>";
  echo "<td><font color=#FFFFFF>" . $row['mobile'] . "</td>";
  echo "<td><font color=#FFFFFF>" . $row['card'] . "</td>";
  echo "<td><font color=#FFFFFF>" . $row['birthday'] . "</font></td>";

  echo "</tr>";
  }
echo "</table>";

Link to comment
Share on other sites

Your field name is '*' which is not a valid field name for a mysql database as far as I know. I think the problem is also that you are trying to use the wildcard character under the WHERE clause and as part of the string.  This will not work what you need to do is use the LIKE clause,

 

"SELECT * FROM contacts WHERE myfield LIKE('mysearchstring')";

 

Link to comment
Share on other sites

Hi, problem solved,

I have written the code as follows, maybe this was the long way around, but for a biginner like me, it just does the job.

 

$result = mysql_query("SELECT * FROM contacts where first_name LIKE '" . $_POST['search'] ."' OR last_name LIKE '" . $_POST['search'] ."' OR company_name LIKE '" . $_POST['search'] ."' OR website LIKE '" . $_POST['search'] ."' OR street LIKE '" . $_POST['search'] ."' OR city LIKE '" . $_POST['search'] ."' OR zip LIKE '" . $_POST['search'] ."' OR country LIKE '" . $_POST['search'] ."' OR mail1 LIKE '" . $_POST['search'] ."' OR mail2 LIKE '" . $_POST['search'] ."' OR phone LIKE '" . $_POST['search'] ."' OR mobile LIKE '" . $_POST['search'] ."' OR card LIKE '" . $_POST['search'] ."' OR birthday LIKE '" . $_POST['search'] ."'") or die (mysql_errno().": ".mysql_error()."<BR>");

Link to comment
Share on other sites

Yes that is how you would extend the search to other fields. I would like to add though that allowing someone to search you entire table with wildcards can be quite taxing on your database. You may want to add some sort of flood control to prevent a user performing a search more then once in a five minute interval.

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.