我有一个表单,它接受用户输入的多个参数。然后它们用于搜索和检索记录。查询需要连接多个表,但由于参数值来自用户,我的代码目前正在创建具有交叉产品的多条记录,即具有多个地址的同一个人。
这是我的代码:
if( $_POST["submit"] ) {
if (!($stmt =$mysqli->prepare(" SELECT DISTINCT Fname, Minit, Lname, EMPLOYEE.Phone, Address, Sname, Saddress, Dno, Hourly
FROM EMPLOYEE, DEPARTMENT, LOCATION
WHERE EMPLOYEE.Fname=? OR EMPLOYEE.Lname=? OR EMPLOYEE.Dno=? OR LOCATION.Store_num=?"))) {
print "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
if (!$stmt->bind_param("ssii",$_POST['Fname'], $_POST['Lname'], $_POST['Dno'], $_POST['Store_num'])) {
print "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
}
if (!$stmt->execute()) {
print "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
$stmt->store_result();
if (!$stmt->bind_result($Fname,$Minit,$Lname,$Phone,$Address,$Slocation,$Saddress,$Dno,$Hourly)) {
print "Binding output parameters failed: (" . $stmt->errno . ") " . $stmt->error;
}
if ($stmt->num_rows == 0){
print "No results were found for the following search <p>"
.$_POST['Fname'].$_POST['Lname'].$_POST['Store_num'].$_POST['Dno']."</p>";
}
else {
print "<table border=2 cellpadding=4>
<tr bgcolor=white>
<th>First Name</th>
<th>Middle Initial</th>
<th>Last Name</th>
<th>Phone</th>
<th>Address</th>
<th>Store</th>
<th>Store Location</th>
<th>Dept #</th>
<th>Hourly Rate</th>
</tr>";
while ($stmt->fetch()){
print "<tr><td>".$Fname."</td><td>".$Minit."</td><td>".$Lname.
"</td><td>".$Phone."</td><td>".$Address."</td><td>".$Slocation."</td>
<td>".$Saddress."</td><td>".$Dno."</td><td>".$Hourly."</td></tr>";
}
print "</table>";
}