I have a form with some fields. Address is one of them. I used textarea tag for address.
Now the problem is, when i search something according to one address, it only works for the one line addresses. I mean if i entered a newline (Paragraph format) in my address it doesn't show the result.
All other search query working very well.Here is my codes,
Code for drop-down list of address :
<input type="hidden" name="category_address" value="address"/>
<select name='criteria_address' style="width:100px;">
<option selected="selected"></option>
<?php
$order = "SELECT DISTINCT address FROM lh_clients ORDER BY clientname" or die (mysql_error());
$result = mysql_query($order);
while ($data = mysql_fetch_array($result)) {
echo ("<option>$data[address]</option>");
}
?>
</select>
Code of search query :
if (isset($_POST['criteria_address']))
{
$category_address = $_POST['category_address'];
$criteria_address = $_POST['criteria_address'];
$query = "SELECT * FROM lh_clients WHERE $category_address = '$criteria_address' ORDER BY clientname";
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($result);
while ($data = mysql_fetch_array($result)) { ?>
<tr>
<td style="vertical-align:top;"><? echo $data['clientname']; ?></td>
<td style="vertical-align:top;"><? echo $data['clienttype']; ?></td>
<td style="vertical-align:top;"><? echo nl2br($data['address']); ?></td>
<td style="vertical-align:top;"><? echo $data['contacts']; ?></td>
<td style="vertical-align:top;"><? echo $data['sensitivity']; ?></td>
<td style="vertical-align:top;"><? echo $data['acountmanager']; ?></td>
<td style="vertical-align:top;"><? echo $data['responsibleexecutive']; ?></td>
<td></td>
</tr>
<?php
}
}
I think the search query doesn't work for the newline in addresses.
Well...
$order = "SELECT DISTINCT address FROM lh_clients ORDER BY clientname" or die (mysql_error());
$result = mysql_query($order);
while ($data = mysql_fetch_array($result)) {
foreach (explode("\n", $data[address]) as $line) {
echo ("<option value='".htmlentities($line)."'>{$line}</option>");
}
}
Try use LIKE for search in text columns
$address = trim(html_entity_decode($criteria_address));
$query = "SELECT * FROM lh_clients WHERE $category_address like '%{$address}%' or $category_address like '{$address}%' or $category_address like '%{$address}' ORDER BY clientname";
You could try using the LIKE operator like so:
WHERE $category_address LIKE '{$criteria_address}%'
This will find all records starting with $criteria address.
Related
I'll try to explain the problem straight away. I have one HTML form which takes input just like a comment form and it saves the xyz data into a MySQL database using PHP. Now, what I want is to create and display links for those comments on a page.
I mean the comments which have been saved including the user's email and name, should be opened by clicking a link.
I don't want to display all the details on a single page from the database for all the users. There should be a page on which links are shown, when a user click a link, the full post should be displayed in next page.
There is not something which I know about this process. Please help me out.
// $rows = set of result from your database query
foreach($rows as $row){
echo '<a'
. ' href="my_link_to_display_comment?id='.$row['id'].'">'
. 'Comment from '.$row['user_name']
. '</a>';
}
First a page to display all the links like the below example -
$result = mysql_query("SELECT * FROM calendar WHERE sort_month='11'");
while($row = mysql_fetch_array($result))
{echo
"".$row['event_name'].""
;}
and then in event.php(the next page after clicking link)
$id = $_GET['id'];
$sql = "select * from calendar where id = $id";
$result = mysql_query($sql, $con);
if ($result){
$row = mysql_fetch_row($result);
$title = $row[12];
$content = $row[7];} ?>
<?php echo $title ?>
<?php echo $content ?>
If you want to show details of a single user just do this.
You can make a search box by using a form.
eg. like if I want to display a details of a student, I will search him by using his roll number and run these queries.
<?php //to search student
require_once './secure.inc.php';
$status = 0;
if(isset($_POST['submit'])){
$roll_number = $_POST['roll_number'];
$query = "select * from students where roll_number=$role_number";
require_once '../includes/db.inc.php';
$result = mysql_query($query);
if(mysql_num_rows($result)==1){
$status = 1;
$row = mysql_fetch_assoc($result); //mysql_fetch_array - both numeric and key index
}else{
$status=2;
}
}
?>
//to display
<?php } else if($status==1) { ?>
<table>
<tbody>
<tr>
<td>Roll Number : </td>
<td><?php echo $row['roll_number']; ?></td>
</tr>
<tr>
<td>Name : </td>
<td><?php echo $row['name']; ?></td>
</tr>
<tr>
<td>Gender : </td>
<td><?php echo $row['gender']; ?></td>
</tr>
<tr>
<td>Email : </td>
<td><?php echo $row['email']; ?></td>
</tr>
<tr>
<td>Mobile Number : </td>
<td><?php echo $row['mobile_number']; ?></td>
</tr>
<tr>
<td>Course : </td>
<td><?php echo $row['course']; ?></td>
</tr>
</tbody>
</table>
<?php } ?>
My idea is to click 'Delete' link and it will pass the id to another PHP page (deleteSession.php), and then execute the query in deleteSession.php. but I couldn't seems to get the id from manageSession.php
In manageSession.php,
<table align='center' border='1' cellpadding='5' cellspacing='0'>
<tr>
<th>Session Id</th>
<th>Type</th>
<th>Date & Time</th>
<th>Venue</th>
<th>Pax</th>
<th>Delete</th>
<th>Edit</th>
</tr>
<?php
$sql = "SELECT booking_id, booking_types, dates_sessions, venue_available, room_count FROM bookings_available ORDER BY dates_sessions asc";
$result = mysqli_query($link, $sql) or die(mysqli_error($link));
//mysqli_close($link);
while ($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row['booking_id']; ?></td>
<td><?php echo $row['booking_types']; ?></td>
<td><?php echo $row['dates_sessions']; ?></td>
<td><?php echo $row['venue_available']; ?></td>
<td><?php echo $row['room_count']; ?></td>
<td><input type="button" value="Delete"/></td>
<td><input type="button" value="Edit"/></td>
</tr>
<?php } ?>
</table>
In deleteSession.php,
<?php
include "dbFunctions.php";
include "manageSession.php";
//$sql = "SELECT booking_id, booking_types, dates_sessions, venue_available, room_count FROM bookings_available";
//$result = mysqli_query($link, $sql) or die(mysqli_error($link));
$bookingId = filter_input(INPUT_GET, 'booking_id');
$deleteQuery = "DELETE FROM bookings_available WHERE booking_id = '$bookingId'";
?>
I think in deleteSession.php file code should be as follows.
$bookingId = filter_input(INPUT_GET, 'id');
OR
$bookingId = $_GET['id'];
Because you are passing get parameter as follows.
deleteSession.php?id=
And also keep anchor as follows.
Delete
In the deleteSession.php you can try and replace:
$bookingId = filter_input(INPUT_GET, 'booking_id');
with the below code:
$bookingId = $_REQUEST['id'];
Finally at the last line you have to execute the query which is stored in $deleteQuery variable, which is not executed yet by using below code:
$qry = mysql_query("DELETE FROM bookings_available WHERE booking_id = '$bookingId'");
//will show you error if not able to delete
if(!$qry)
die("Error: ".mysql_error());
Added this at line 3 and it works:
mysqli_select_db($link ,$DB);
Because in the code I have not selected the mysql database and also the query was not executing as the first parameter $link was missing.
Here is my code:
<table border="2px solid #FFF" width="100%">
<tr>
<td width="50%"><Center>Username:</Center><br /></td>
<td width="50%"><center>Numebr Of Warnings:</center><br /></td>
</tr>
</table>
<?
$query = mysql_query("SELECT * FROM warnings");
$numrows = mysql_num_rows($query);
if($numrows != "0"){
while($row = mysql_fetch_assoc($query)){
$warned = $row['username'];
$by = $row['by'];
$sql = mysql_query("SELECT * FROM warnings WHERE username='$warned'");
$warns = mysql_num_rows($sql);
?>
<table border="2px solid #FFF" width="100%">
<tr>
<td width="50%"><center><b><?php echo $warned; ?></b></center></td>
<td width="50%"><center><b><?php echo $warns; ?></center></b></td>
</tr>
</table>
<?php
}
}
else
echo "No Currently Warned Users";
?>
<hr />
And here is the result:
How can I make it so that instead of showing the 2 results for the user Mrg..... I just want it to show one result with the number or rows there are.
Help would be appreciated.
You can use DISTINCT in your query to avoid duplicate rows in your results:
SELECT DISTINCT username, `by` FROM warnings
Change the mysql_fetch_assoc with mysql_feth_array
You're getting the user with double record. Then for each user you are getting 2 lines
The best way to check it is to add pseudo-points to your code.
$query = mysql_query("SELECT * FROM warnings");
$arr=array();
while($row = mysql_fetch_assoc($query))
{
$arr[]=$row;
}
echo "<pre>";
print_r($arr);
echo "</pre>";
exit;
Here check if the $arr has double entries. Then change the mysql_fetch_assoc with mysql_fetch_array and try again
Here is the table
(by,id,username,date)
a1,1,u1,date
a2,2,u2,date
a3,3,u2,date
a4,4,u2,date
And here is the php code
<?php
$conn = mysqli_connect("localhost","username","password");
mysqli_select_db($conn,"dbname");
$warns = mysqli_query($conn,"select username, count(username) as warncount from dtest.warnings W group by username");
while($line = mysqli_fetch_array($warns))
{
echo $line['username']." has ".$line['warncount']. " warns <br/> ";
}
?>
I am trying to set up a PHP MySQL search script which will let me print the content of my membership MySQL database table and then filter the results using different criteria.
The following fields are used in my MySQL table:
committee_id
rank
last_name
first_name
sex
address
email
phone_number
active_status
I want 2 ways to filter the data:
1) using using a drop down with all the available rank i can filter the results by rank of member.
2) using a drop down with all the available active_status you can filter the results by active status only.
I have set up my search form successfully, and printed all MySQL Table contents, Only the search by position part is filtering my result, but not the active_status, the filter part is a challenge. here is my html table and search script:
<?php include("./includes/connnect.php");?>
<!DOCTYPE HTML>
<html>
<body>
<table>
<thead>
<tr>
<td>ID</td>
<td>position</td>
<td>Last Name</td>
<td>First Name</td>
<td>Sex</td>
<td>Address</td>
<td><strong>Email</td>
<td><strong>Phone Number</td>
<td>Status</td>
</tr>
</thead>
<tbody>
<?php
if ($_REQUEST["position"]<>'') {
$search_position = " AND position='".mysql_real_escape_string($_REQUEST["position"])."'";
}
if ($_REQUEST["status"]<>'') {
$search_status = " AND status='".mysql_real_escape_string($_REQUEST["status"])."'";
}
else {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE committee_id>0".$search_position.$search_status;
}
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
if (mysql_num_rows($sql_result)>0) {
while ($row = mysql_fetch_assoc($sql_result)) {
?>
<tr>
<td><?php echo $row["committee_id"]; ?></td>
<td><?php echo $row["rank"]; ?></td>
<td><?php echo $row["last_name"]; ?></td>
<td><?php echo $row["first_name"]; ?></td>
<td><?php echo $row["sex"]; ?></td>
<td><?php echo $row["address"]; ?></td>
<td><?php echo $row["email"]; ?></td>
<td><?php echo $row["phone_number"]; ?></td>
<td><?php echo $row["active_status"]; ?></td>
</tr>
<?php
}
} else {
?>
<tr><td colspan="5">No results found.</td>
<?php
}
?>
</tbody>
</table>
</body>
</html>
I would appreciate any suggestions to get this going.
Thanks.
That is because when there is $_REQUEST["status"] the variable SQL is not setted as you put it on the else statement
if ($_REQUEST["status"]<>'') {
$search_status = " AND status='" .
mysql_real_escape_string($_REQUEST["status"])."'";
}
else { /// this is your problem
$sql = "SELECT * FROM ".$SETTINGS["data_table"]."
WHERE committee_id>0".$search_position . $search_status;
}
Put the $sql out of the else and take the else out.
Thank you Jorge your solution worked. But I got the same result with these elseif statements:
if ($_REQUEST["position"]<>'') {
$search_position = " AND position='".mysql_real_escape_string($_REQUEST["position"])."'";
}
if ($_REQUEST["status"]<>'') {
$search_status = " AND status='".mysql_real_escape_string($_REQUEST["status"])."'";
}
if ($_REQUEST["position"]<>'' and $_REQUEST["status"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]."
WHERE position = '".mysql_real_escape_string($_REQUEST["position"])."'
AND status = '".mysql_real_escape_string($_REQUEST["status"])."'";
}else if ($_REQUEST["position"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE position = '".mysql_real_escape_string($_REQUEST["position"])."'".$search_city;
}else if ($_REQUEST["status"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE status = '".mysql_real_escape_string($_REQUEST["status"])."'".$search_position;
}else {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE committee_id>0".$search_position.$search_status;
}
I shall have to convert these to mysqli or pdo as advised.
My code is working right but i have a problem , i can search by the first select option only and the second select option didn't work, the first select option ($_REQUEST["area"]<>'')" and "
($_REQUEST["area"]<>'') is working good but i can fetch only by the
firs select option i write it and else give me error and when change
those i have the same problem, so i want to using two select option to
filter my search.
<?php
if ($_REQUEST["string"]<>'') {
$search_string = " AND (name LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%' OR
specialization LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%' OR
address LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%' OR
telephone LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%' OR
time LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%'
)";
}
if ($_REQUEST["area"]<>'') {
$search_area = " AND location_id='".mysql_real_escape_string($_REQUEST["area"])."'";
}
if ($_REQUEST["category"]<>'') {
$search_category = " AND cate_id='".mysql_real_escape_string($_REQUEST["category"])."'";
}
else {
$sql = "SELECT * FROM informations WHERE id>0".$search_string.$search_area.$search_category;
}
$sql_result = mysql_query ($sql) or die ('request "Could not execute SQL query" '.$sql);
if (mysql_num_rows($sql_result)>0) {
while ($row = mysql_fetch_assoc($sql_result)) {
?>
<tr>
<td><?php $sel_cate2 = "SELECT title FROM categories where id = ".$row['cate_id']." ";
$done_cate2 = mysql_query($sel_cate2);
$get2 = mysql_fetch_array($done_cate2);
echo $get2["title"]; ?></td>
<td><?php $sel_cate2 = "SELECT location FROM locations where id = ".$row['location_id']." ";
$done_cate2 = mysql_query($sel_cate2);
$get2 = mysql_fetch_array($done_cate2);
echo $get2["location"]; ?></td>
<td><?php echo $row["name"]; ?></td>
<td><?php echo $row["specialization"]; ?></td>
<td><?php echo $row["address"]; ?></td>
<td><?php echo $row["telephone"]; ?></td>
<td><?php echo $row["time"]; ?></td>
</tr>
<?php
}
} else {
?>
<tr><td colspan="5">No results found.</td>
<?php
}
?>
</table>