2017-11-11 82 views
2

我试图从数据库中检索用户数据...值是常量(“t”),我有很多列来搜索因此我决定使用post方法发布列名并查找常量值(在我的情况下为“t”)。我已经创建了这个代码,但它不工作,请检查代码,我正在测试它使用邮递员,所以附上截图,请看看我得到什么错误。通过发布列名不工作来搜索恒定值的代码需要一些更正

我在DbOperations.php

<?php 

    class DbOperations{ 

    private $con; 

    function __construct(){ 

     require_once dirname(__FILE__).'/DbConnect.php'; 

     $db = new DbConnect(); 

     $this->con = $db->connect(); 

    } 

    //CRUD -> c -> CREATE 

    //Test Purpose 

    public function gettestuser($value, $pin){ 
     $valid_columns = array('a' => 1, 'b' => 1, 'ho' => 1, 'll' => 1, 'c' => 1, 'd' => 1); 
     if (!array_key_exists($value, $valid_columns)) { 
      throw new Exception("Error Processing Request", 1); 
     } 

     $stmt = $this->con->prepare("SELECT * FROM test_category WHERE $value = 't' pin = ?"); 
     $stmt->bind_param("ss", $value, $pin); 
     $stmt->execute(); 
     return $stmt->get_result()->fetch_assoc(); 
     } 
    } 
?> 

我gettestuser.php

<?php 
require_once '../include/DbOperations.php'; 

$response = array(); 

if($_SERVER['REQUEST_METHOD']=='POST'){ 
    if(isset($_POST['reg_value']) && isset($_POST['reg_pin'])){ 

    $db = new DbOperations(); 

    $test_category = $db->gettestuser($_POST['reg_value'], $_POST['reg_pin']); 

    var_dump($test_category); 

     $response['error'] = false; 
     $response['pid'] = $test_category['pid']; 
     $response['name'] = $test_category['name']; 
     $response['pin'] = $test_category['pin']; 
     $response['a'] = $test_category['a']; 
     $response['b'] = $test_category['b']; 
     $response['ho'] = $test_category['ho']; 
     $response['ll'] = $test_category['ll']; 
     $response['c'] = $test_category['c']; 
     $response['d'] = $test_category['d']; 



    }else{ 
     $response['error'] = true; 
     $response['message'] = "Required fields are missing"; 
     } 
    } 

echo json_encode($response); 
?> 

enter image description here

我的表结构功能

enter image description here

+0

在该行的错误是说我怀疑它的'其中$值=“T”脚='这是错的?你也不能绑定列。 –

+0

@LawrenceCherone谢谢......但你能告诉我我在那里做错了什么?我一点也不明白。 –

+0

它应该很可能是'WHERE the_reg_column =? AND pin =?',列名的所有字母是什么? –

回答

1

要添加动态字段,必须为字段名称绑定参数。同时您忘记and的组合条件,改变你的代码:

$stmt = $this->con->prepare("SELECT * FROM test_category WHERE $value = 't' and pin = ?"); 
    $stmt->bind_param("s", $pin); 
    $stmt->execute(); 
    return $stmt->get_result()->fetch_assoc(); 
+0

谢谢...这段代码很好,但是有两件事情做错了... 1.当没有找到匹配的结果时它仍然显示:C:\ wamp64 \ www \ Android \ v1 \ gettestuser.php:13:null { “错误”:假​​的, “PID”:空, “名”:空, “针”:空, “一”:空, “b”:空, “豪”:空, “LL”:空,“C “:null,”d“:null} 2.它不显示多个用户数据......它只返回一个用户的详细信息。 –

+0

您需要while循环来获取多个数据。请参阅https://stackoverflow.com/questions/35103594/get-result-fetch-assoc-always-returning-1-row –

相关问题