简体   繁体   English

如何通过在php mysql中发布列名来搜索常量值?

[英]How to search for a constant value by posting the column name in php mysql?

I have created this code to retrieve multiple user data where the pincode = the pin-code i am posting through post method and $value = 't'($value = a column name(i want to send the column name using post method(column name= a, b, ho, ll, c, d)) where i want it to look for 't').我创建了此代码来检索多个用户数据,其中 pincode = 我通过 post 方法发布的 pin 代码和 $value = 't'($value = 列名(我想使用 post 方法发送列名(列名= a, b, ho, ll, c, d)) 我想让它寻找't')。 my code is not complete and i don't know how to do it can anyone help me with this?我的代码不完整,我不知道该怎么做,有人可以帮我吗? Later i want it to connect with android app and list view the retrieved data.后来我希望它与android应用程序连接并列出查看检索到的数据。

My function in DbOperations.php我在 DbOperations.php 中的函数

<?php

class DbOperations{

    private $con;

    function __construct(){

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

        $db = new DbConnect();

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

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

My gettestuser.php我的 gettestuser.php

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

$response = array();

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

    $db = new DbOperations();

    $test_category = $db->gettestuser($_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);
?>

Something like this.像这样的东西。

DbOperations.php:数据库操作.php:

public function gettestuser($col_name, $pin) {
    $valid_columns = array('a' => 1, 'b' => 1, 'ho' => 1, 'll' => 1, 'c' => 1, 'd' => 1);
    if (!array_key_exists($col_name, $valid_columns)) {
        throw new Exception('Bad column name');
    }

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

gettestuser.php: gettestuser.php:

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

$response = array();

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset($_POST['reg_pin']) && isset($_POST['reg_value'])) {
        $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);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM