Hi, everyone.I am currently studying PHP on my own.
Currently, I want to search for information in the database by multiple criteria, but the search results are not displayed.
Specifically, html is displayed, but the search results (information in the database) are not displayed.
If anyone knows why it is not displayed, please let me know.
Or, could you please let me know if there are multiple searchable codes based on the current code?
Current Code
<form method="post" action="searchtest.php">
<div class="studentinfo">
<p>Customer Number:<input type="text" name="id"></p>
<p>Contact: <input type="text" name="Staff_Name">/p>
<p>Status: <input type="text" name="Status">/p>
</div>
<div class="Schoolinfo">
<p>Name:<input type="text" name="Name">/p>
<p>Campus: <input type="text" name="Campus">/p>
<p> Course Start Date: <input type="date" name="Start_Date">/p>
</div>
<div>
<input type="submit" value="Search">
</form>
searchtest.php
<?php
$search=filter_input(INPUT_POST, 'search');
$id = filter_input(INPUT_POST, 'id');
$Staff_Name=filter_input(INPUT_POST, 'Staff_Name');
$Status=filter_input(INPUT_POST, 'Status');
$Name=filter_input(INPUT_POST, 'Name');
$Campus=filter_input(INPUT_POST, 'Campus');
$Start_Date=filter_input(INPUT_POST, 'Start_Date');
if(!is_null($search)){
$sql="SELECT* FROM studentinfo WHERE1";
$data=[];
if(!is_null($id)){
$sql.="ANDid=? ";
$data[] = $id;
}
if(!is_null($Staff_Name)){
$sql.="AND Staff_Name LIKE?";
$data[]="%".$Staff_Name."%";
}
if(!is_null($Status)){
$sql.="AND Status=?";
$data[] = $Status;
}
if(!is_null($Campus)){
$sql.="AND Campus LIKE?";
$data[]="%".$Campus."%";
}
if(!is_null($Name)){
$sql.="AND Name LIKE?";
$data[]="%".$Name."%";
}
if(!is_null($Start_Date)){
$sql.="AND Start_Date=?";
$data[] = $Start_Date;
}
if(count($data)==0)$sql.="AND0";
$stmt = $pdo->prepare($sql);
$stmt->execute($data);
}
?>
Thank you for your cooperation.
php mysql
You must connect to the DB before throwing the SQL query but there is no information about the DB in the code provided, so please check the correct procedure first.
Example PDO Instance Creation
//$dns='mysqld:dbname=database name; host=hostname';
// dns (Database Source Name)
$dns='mysqld:dbname=myapp;host=localhost';
$usre='root';
$password=';
$con = new PDO($dns, $user, $password);
// Query after connecting to DB
$con->prepare('SQL statement');
Note:
[PHP·PDO] How to connect to the database
$stmt->execute($data);
You haven't done anything since you executed it in
while($result=$stmt->fetch(PDO::FETCH_ASSOC)){
// Now you can get a column of data in the result.
}
Receive the data as shown in .
Recently, however, I use a special module in the framework around RDB. I don't think I use PDOS statement very often.
Also, I have doubts that such questions will go up to stackoverflow.
Do you use proper teaching materials?I think we should review it.
It's more efficient to use a proper program without thinking that you can study on the web.
If it's a proper teaching material,
$stmt=$pdo->prepare($selectQuery);
$stmt->execute($data);
while($result=$stmt->fetch(PDO::FETCH_ASSOC)){
}
should come out as a set.
© 2024 OneMinuteCode. All rights reserved.