I am trying to display the results of my database search on the web page. I do not get any results when I press the submit button. However, when I put a specific value in the query code instead of the $valueToSearch variable, it finds and displays the results successfully. I believe the problem is in the submit button. Here is the code:
<?php
if(isset($_POST['search']))
{
$valueToSearch = $_POST['valueToSearch'];
$query = "SELECT * FROM volunteers WHERE fname='.$valueToSearch.'";
$search_result = filterTable($query);
}
else{
$query = "SELECT * FROM `volunteers` WHERE 1";
$search_result = filterTable($query);
}
function filterTable($query)
{
$connect = mysqli_connect("localhost", "root", "", "volunteedbzlfqf");
$filter_Result = mysqli_query($connect, $query);
return $filter_Result;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP HTML TABLE DATA SEARCH</title>
<style>
table,tr,th,td
{
border: 1px solid black;
}
</style>
</head>
<body>
<form action="" method="post">
<input type="text" name="valueToSearch" placeholder="Value To Search"><br><br>
<input type="submit" name="search" value="Filter"><br><br>
<table>
<tr>
<th>الهوية</th>
<th>كلمة المرور</th>
<th>الاسم</th>
<th>العائلة</th>
<th>الجنس</th>
<th>الجوال</th>
<th>الإيميل</th>
<th>الميلاد</th>
<th>المدينة</th>
<th>المؤهل</th>
<th>التخصص</th>
<th>التطوع</th>
<th>الساعات</th>
<th>العضوية</th>
<th>النقاط</th>
<th>التقييم</th>
<th>الإنذارات</th>
</tr>
<?php while($row = mysqli_fetch_array($search_result)):?>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['password'];?></td>
<td><?php echo $row['fname'];?></td>
<td><?php echo $row['lname'];?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['phone'];?></td>
<td><?php echo $row['email'];?></td>
<td><?php echo $row['DOB'];?></td>
<td><?php echo $row['city'];?></td>
<td><?php echo $row['degree'];?></td>
<td><?php echo $row['major'];?></td>
<td><?php echo $row['Vtype'];?></td>
<td><?php echo $row['hours'];?></td>
<td><?php echo $row['membership'];?></td>
<td><?php echo $row['points'];?></td>
<td><?php echo $row['rating'];?></td>
<td><?php echo $row['warnings'];?></td>
</tr>
<?php endwhile;?>
</table>
</form>
</body>
</html>
As of today, the action
attribute, if present, must be non-empty. So you have two choices:
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<form method="post">
I think there's also a cleaner way to check for the POST done, and that would be based on this excellent example at W3Schools
All in all, the code fixes you need are:
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST['valueToSearch']))
{
$valueToSearch = $_POST['valueToSearch'];
$query = "SELECT * FROM volunteers WHERE fname='.$valueToSearch.'";
$search_result = filterTable($query);
} else {
$query = "SELECT * FROM `volunteers`";
$search_result = filterTable($query);
}
}
and don't forget the form
action
parameter.
You might need to just change:
$query = "SELECT * FROM volunteers WHERE fname='.$valueToSearch.'";
to:
$query = 'SELECT * FROM volunteers WHERE fname="' . $valueToSearch . '"';
or better yet, use something like:
$query = 'SELECT * FROM volunteers WHERE fname= ?';
and parameterize that.
SELECT * FROM volunteers WHERE fname="scotti"mysqli_result Object ( [current_field] => 0 [field_count] => 15 [lengths] => [num_rows] => 1 [type] => 0 )
الهوية كلمة المرور الاسم العائلة الجنس الجوال الإيميل الميلاد المدينة المؤهل التخصص التطوع الساعات العضوية النقاط التقييم الإنذارات 1 scotti
CREATE TABLE `volunteers` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`password` varchar(16) DEFAULT NULL,
`fname` varchar(16) DEFAULT NULL,
`lname` varchar(16) DEFAULT NULL,
`gender` varchar(16) DEFAULT NULL,
`phone` varchar(16) DEFAULT NULL,
`email` varchar(16) DEFAULT NULL,
`DOB` date DEFAULT NULL,
`city` varchar(16) DEFAULT NULL,
`degree` varchar(16) DEFAULT NULL,
`major` varchar(16) DEFAULT NULL,
`Vtype` varchar(16) DEFAULT NULL,
`hours` varchar(16) DEFAULT NULL,
`membership` varchar(16) DEFAULT NULL,
`points` varchar(16) DEFAULT NULL,
`rating` varchar(16) DEFAULT NULL,
`warnings` varchar(16) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.