Php search help
Hey guys
I am needing a bit of help if you can please.
For some reason my search code is displaying all the records even tho I have not typed anything in the text box how can I get it so it dosent show them until the button has been pressed and something has been typed.
<h1>Search Previous Orders</h1>
<form action="search.php" method="get">
<input type="text" name="k" size="50" value="<?php echo $_GET['k']; ?>"/>
<input type="submit" name="submit" value="Search Orders"/>
</form>
<hr />
<?php
$k = $_GET['k'];
$terms = explode(" ", $k);
$query = "SELECT * FROM orders WHERE ";
foreach ($terms as $each){
$i++;
if ($i == 1)
$query .= "FullName LIKE '%$each%' ";
else
$query .= "OR FullName LIKE '%$each%' ";
}
require('connect/connect.php');
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0){
while ($row = mysql_fetch_assoc($query)){
$id = $row['id'];
$fullname = $row['FullName'];
$gotdate = $row['GotDate'];
$paydate = $row['PayDate'];
$money = $row['Money'];
$phone = $row['Phone'];
$info = $row['Info'];
$status = $row['Status'];
$orderby = $row['OrderBy'];
echo "$fullname<br/>";
}
}
else
echo "No Results Found For \"<b>$each</b>\"";
?>
I am needing a bit of help if you can please.
For some reason my search code is displaying all the records even tho I have not typed anything in the text box how can I get it so it dosent show them until the button has been pressed and something has been typed.
<h1>Search Previous Orders</h1>
<form action="search.php" method="get">
<input type="text" name="k" size="50" value="<?php echo $_GET['k']; ?>"/>
<input type="submit" name="submit" value="Search Orders"/>
</form>
<hr />
<?php
$k = $_GET['k'];
$terms = explode(" ", $k);
$query = "SELECT * FROM orders WHERE ";
foreach ($terms as $each){
$i++;
if ($i == 1)
$query .= "FullName LIKE '%$each%' ";
else
$query .= "OR FullName LIKE '%$each%' ";
}
require('connect/connect.php');
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0){
while ($row = mysql_fetch_assoc($query)){
$id = $row['id'];
$fullname = $row['FullName'];
$gotdate = $row['GotDate'];
$paydate = $row['PayDate'];
$money = $row['Money'];
$phone = $row['Phone'];
$info = $row['Info'];
$status = $row['Status'];
$orderby = $row['OrderBy'];
echo "$fullname<br/>";
}
}
else
echo "No Results Found For \"<b>$each</b>\"";
?>
Your $i variable is never set to 0. You also never check if get request has been made.
KR,
- Filip
KR,
- Filip
CodenStuff wrote:Nope, it's just your sick and dirty mind. You sick twisted warped little pervo![]()
Code: Select all
Again, if you aren't sure why your queries don't work, you can always echo them and test them in phpMyAdmin. <?php
if(isset($_GET['k'])) { // check if get request has been made and k has been set
$k = $_GET['k'];
$terms = explode(" ", $k);
$query = "SELECT * FROM orders WHERE ";
$i = 0; // setting counter to 0
foreach ($terms as $each){
$i++;
if ($i == 1)
$query .= "FullName LIKE '%$each%' ";
else
$query .= "OR FullName LIKE '%$each%' ";
}
require('connect/connect.php');
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0){
while ($row = mysql_fetch_assoc($query)){
$id = $row['id'];
$fullname = $row['FullName'];
$gotdate = $row['GotDate'];
$paydate = $row['PayDate'];
$money = $row['Money'];
$phone = $row['Phone'];
$info = $row['Info'];
$status = $row['Status'];
$orderby = $row['OrderBy'];
echo "$fullname<br/>";
}
}
else
echo "No Results Found For \"<b>$each</b>\"";
} // Closing initial if
?>
KR,
- Filip
CodenStuff wrote:Nope, it's just your sick and dirty mind. You sick twisted warped little pervo![]()
Hello thanks for all your help just one more question lol my table seems to be messing up what have i done wrong in it this is the code?
Code: Select all
<?php
session_start();
if(!isset($_SESSION["sess_user"])){
header("location:index.php");
}
else {
?>
<html>
<head>
<title>MBDUES.co.uk</title>
<link rel="stylesheet" type="text/css" href="css/style.css" media="screen" />
</head>
<body>
<div id="wrapper">
<div id="header">
<img src="img/Banner.png"/>
</div>
<div id="nav">
<a href='./members.php'>Home</a>
<a href='./add_order.php'>Add New Order</a>
<a href='./search.php'>Search Orders</a>
<a href='./logout.php'>Logout</a>
</div>
<div id="content">
<div id="full">
Welcome, <?=$_SESSION['sess_user'];?> <hr />
<h1>Search Previous Orders</h1>
<form action="search.php" method="get">
<input type="text" name="k" size="50" value="<?php echo $_GET['k']; ?>"/>
<input type="submit" name="submit" value="Search Orders"/>
</form>
<hr />
<?php
if(isset($_GET['k'])) { // check if get request has been made and k has been set
$k = $_GET['k'];
$terms = explode(" ", $k);
$query = "SELECT * FROM orders WHERE ";
$i = 0; // setting counter to 0
foreach ($terms as $each){
$i++;
if ($i == 1)
$query .= "FullName LIKE '%$each%' ";
else
$query .= "OR FullName LIKE '%$each%' ";
}
require('connect/connect.php');
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0){
while ($row = mysql_fetch_assoc($query)){
$id = $row['id'];
$fullname = $row['FullName'];
$gotdate = $row['GotDate'];
$paydate = $row['PayDate'];
$money = $row['Money'];
$phone = $row['Phone'];
$info = $row['Info'];
$status = $row['Status'];
$orderby = $row['OrderBy'];
echo "
<table border='1' style='width:100%'>
<tr>
<th>Full Name</th>
<th>Date Got</th>
<th>Pay Date</th>
<th>Money Due</th>
<th>Status</th>
<th>Order By</th>
</tr>
<tr>
<td>$fullname</td>
<td>$gotdate</td>
<td>$paydate</td>
<td>$money</td>
<td>$status</td>
<td>$orderby</td>
</tr>
</table>
";
}
}
else
echo "No Results Found For \"<b>$each</b>\"";
} // Closing initial if
?>
</div>
</div>
<div id="footer">
Footer
</div>
</div>
</body>
</html>
<?php
}
?>
You do not have the required permissions to view the files attached to this post.
Hello,
here's the fixed code:
-Filip
here's the fixed code:
Code: Select all
KR,<?php
session_start();
if(!isset($_SESSION["sess_user"])){
header("location:index.php");
}
?>
<html>
<head>
<title>MBDUES.co.uk</title>
<link rel="stylesheet" type="text/css" href="css/style.css" media="screen" />
</head>
<body>
<div id="wrapper">
<div id="header">
<img src="img/Banner.png"/>
</div>
<div id="nav">
<a href='./members.php'>Home</a>
<a href='./add_order.php'>Add New Order</a>
<a href='./search.php'>Search Orders</a>
<a href='./logout.php'>Logout</a>
</div>
<div id="content">
<div id="full">
Welcome, <? echo $_SESSION['sess_user']; ?> <hr />
<h1>Search Previous Orders</h1>
<form action="search.php" method="get">
<input type="text" name="k" size="50" value="<?php echo $_GET['k']; ?>"/>
<input type="submit" name="submit" value="Search Orders"/>
</form>
<hr />
<?php
if(isset($_GET['k'])) { // check if get request has been made and k has been set
$k = $_GET['k'];
$terms = explode(" ", $k);
$query = "SELECT * FROM orders WHERE ";
$i = 0; // setting counter to 0
foreach ($terms as $each){
$i++;
if ($i == 1)
$query .= "FullName LIKE '%$each%' ";
else
$query .= "OR FullName LIKE '%$each%' ";
}
require('connect/connect.php');
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0){
echo "
<table border='1' style='width:100%'>
<tr>
<th>Full Name</th>
<th>Date Got</th>
<th>Pay Date</th>
<th>Money Due</th>
<th>Status</th>
<th>Order By</th>
</tr>";
// Echo table header
while ($row = mysql_fetch_assoc($query)){
$id = $row['id'];
$fullname = $row['FullName'];
$gotdate = $row['GotDate'];
$paydate = $row['PayDate'];
$money = $row['Money'];
$phone = $row['Phone'];
$info = $row['Info'];
$status = $row['Status'];
$orderby = $row['OrderBy'];
echo "
<tr>
<td>$fullname</td>
<td>$gotdate</td>
<td>$paydate</td>
<td>$money</td>
<td>$status</td>
<td>$orderby</td>
</tr>
";
}
echo "</table>";
}
else
echo "No Results Found For \"<b>$each</b>\"";
} // Closing initial if
?>
</div>
</div>
<div id="footer">
Footer
</div>
</div>
</body>
</html>
-Filip
CodenStuff wrote:Nope, it's just your sick and dirty mind. You sick twisted warped little pervo![]()
Hello,
Sorry was ment to ask this is the last post if i wanted to just type John Doe in the text box how do i get it so it just searches for that name or any name put in like John Doe because at the moment if i type John Doe its searching for the name John and Doe and Displaying the records when i inly want it to show the John Doe records?
You can see what i mean by the screenshots its only ment to show the records for Mark Brodie but its searching for Brodie and its showing Paul Brodie Aswell.
Sorry was ment to ask this is the last post if i wanted to just type John Doe in the text box how do i get it so it just searches for that name or any name put in like John Doe because at the moment if i type John Doe its searching for the name John and Doe and Displaying the records when i inly want it to show the John Doe records?
You can see what i mean by the screenshots its only ment to show the records for Mark Brodie but its searching for Brodie and its showing Paul Brodie Aswell.
You do not have the required permissions to view the files attached to this post.
You would want to change OR in query to AND:
-Filip
Code: Select all
KR, foreach ($terms as $each){
$i++;
if ($i == 1)
$query .= "FullName LIKE '%$each%' ";
else
$query .= "AND FullName LIKE '%$each%' "; //here
}
-Filip
CodenStuff wrote:Nope, it's just your sick and dirty mind. You sick twisted warped little pervo![]()
I just noticed if i press the button without anything in the text box it shows all the records lol?
Copyright Information
Copyright © Codenstuff.com 2020 - 2023