提交搜索时,我正在尝试回显页面正文中的某些HTML,但我得到的是没有html回声的页面,我无法看到问题。
function search($lname,$postcode){
$stmt->prepare("SELECT * FROM clients WHERE lname=:lname AND postcode=:postcode");
$stmt->execute(array(':lname'=>$lname, ':postcode'=>$postcode));
$userRow=$stmt->fetch(PDO::FETCH_ASSOC);
}
if(isset($_POST['search'])){
$lname = $_POST['lname'];
$postcode = $_POST['postcode'];
$result = search($lname, $postcode);
if ($result['rating'] == 'good') {
$user->redirect('./results.php?good');
}
}
由于
答案 0 :(得分:1)
我很难说你的search
函数用于什么,但错误是:
您没有在提供的代码中调用search
函数。
我想它应该被称为
if(isset($_POST['search'])){
$lname = $_POST['lname'];
$postcode = $_POST['postcode'];
$result = search($lname, $postcode); // or maybe this->search
接下来你要检查某事物的rating
键。我无法理解什么。什么是$_GET['rating'];
?当然它是一些字符串变量,如果是 - 它没有键rating
。那么我想你要查看rating
函数返回的结果的search
键?
可以这样做:
$lname = $_POST['lname'];
$postcode = $_POST['postcode'];
$result = search($lname, $postcode); // or maybe this->search
if ($result['rating'] == 'good') { /* do something */ }
但并非全部。正如您当前的实现search
返回bool
一样,rating
中的$result
键显然没有。所以,我想你应该返回$userRow
并检查其中的rating
键:
function search($lname,$postcode){
$stmt = $this->db->prepare("SELECT * FROM clients WHERE lname=:lname AND postcode=:postcode");
$stmt->execute(array(':lname'=>$lname, ':postcode'=>$postcode));
$userRow=$stmt->fetch(PDO::FETCH_ASSOC);
var_dump($userRow); // for test purposes
return $userRow;
}
答案 1 :(得分:0)
if(isset($_POST['search'])){
$lname = trim($_POST['lname']);
$postcode = trim($_POST['postcode']);
if($lname==""){
$error[] = "Provide Last Name.";
}else if($postcode==""){
$error[] = "Provide Postcode.";
}else{
try{
$stmt = $conn->prepare("SELECT * FROM clients WHERE lname=:lname AND postcode=:postcode");
$stmt->execute(array(":lname"=>$lname, ":postcode"=>$postcode));
$userRow=$stmt->fetch(PDO::FETCH_ASSOC);
if($userRow['rating'] == ''){
$user->redirect('./results.php?notfound');
}else{
if($userRow['rating'] == 'good'){
$user->redirect('./results.php?good');
}
if($userRow['rating'] == 'caution'){
$user->redirect('./results.php?caution');
}
if($userRow['rating'] == 'badnopay'){
$user->redirect('./results.php?badnopay');
}
if($userRow['rating'] == 'badabusive'){
$user->redirect('./results.php?badabusive');
}
if($userRow['rating'] == 'badlate'){
$user->redirect('./results.php?badlate');
}
}
}
catch(PDOException $e){
echo $e->getMessage();
}
}
}