dousuie2222 2016-03-08 09:58
浏览 22
已采纳

数据库<> PHP通信脚本无法正常工作

So I got this code, it is a PHP script that communicates my database with my application. But, somehow the update function isn't working at all and I can't figure out what's wrong - though I'm 99% sure it isn't the application itself.

Here's the whole php scipt

    <?php

class DataBase
{
    private $server;
    private $user;
    private $password;
    private $database;

    function __construct($server, $user, $password, $database)
    {
        $this->server = $server;
        $this->user = $user;
        $this->password = $password;
        $this->database = $database;
    }

    private function connect()
    {
        $connect = mysqli_connect($this->server, $this->user, $this->password) or die('Error, no se ha podido conectar.');
        mysqli_select_db($connect, $this->database);

        return $connect;
    }

    private function diconnect($connect)
    {
        return mysqli_close($connect);
    }

    private function wheres($wheres)
    {
        $w = '';

        foreach ($wheres as $where => $key)
        {
            $w .= ' `'.$where.'` = "'.$key.'"';
        }

        return $w;
    }

    private function sets($sets)
    {
        $w = '';

        foreach ($sets as $set => $key)
        {
            $w .= ' `'.$set.'` = "'.$key.'",';
        }

        return substr($w, 0, -1);
    }

    public function select($table, $wheres = null)
    {
        $connect = $this->connect();

        if ($wheres == null)
        {
            $query = mysqli_query($connect, 'SELECT * FROM `'.$table.'`');
        } else {
            $query = mysqli_query($connect, 'SELECT * FROM `'.$table.'` WHERE '.$this->wheres($wheres));
        }

        $i = 0;
        $ret = array();

        while ($row = mysqli_fetch_assoc($query)) {
            foreach ($row as $key => $value) {
                $ret[$i][$key] = $value;
            }

            $i++;
        }

        return ($ret);
    }

    public function insert($table, $inserts)
    {
        $connect = $this->connect();
        $values = array_values($inserts);

        $keys = array_keys($inserts);

        return $query = mysqli_query($connect, 'INSERT INTO `'.$table.'` (`'.implode('`,`', $keys).'`) VALUES (\''.implode('\',\'', $values).'\')');
    }

    public function delete($table, $wheres = null)
    {
        $connect = $this->connect();

        if ($wheres == null)
        {
            return $query = mysqli_query($connect, 'DELETE FROM `'.$table.'`');
        } else {
            return $query = mysqli_query($connect, 'DELETE FROM `'.$table.'` WHERE '.$this->wheres($wheres));
        }
    }

    public function update($table, $id, $sets = null)
    {
        $connect = $this->connect();

        return $query = mysqli_query($connect, 'UPDATE `'.$table.'` SET'.$this->sets($sets).' WHERE `id` = "'.$id.'"');
    }

}

if (!empty($_POST))
{
    if (isset($_POST['method']))
    {
        if (isset($_POST['table']))
        {
            $DataBase = new DataBase('127.0.0.1', 'root', 'password', 'coord_tic');

            switch ($_POST['method'])
            {
                case 'insert':
                    $inserts = $_POST;
                    unset($inserts['table']);
                    unset($inserts['method']);
                    echo json_encode($DataBase->insert($_POST['table'], $inserts));
                    break;

                case 'delete':
                    $wheres = $_POST;
                    unset($wheres['table']);
                    unset($wheres['method']);
                    echo json_encode($DataBase->delete($_POST['table'], $wheres));
                    break;

                case 'select':
                    $wheres = $_POST;
                    unset($wheres['table']);
                    unset($wheres['method']);
                    echo json_encode($DataBase->select($_POST['table'], $wheres));
                    break;

                case 'update':
                    $wheres = $_POST;
                    unset($wheres['table']);
                    unset($wheres['method']);
                    unset($wheres['id']);
                    echo json_encode($DataBase->update($_POST['table'], $_POST['id'], $wheres));
                    break;

                default:
                    echo json_encode(false);
                    break;
            }
        } else {
            echo json_encode(false);
        }
    } else {
        echo json_encode(false);
    }
} else {
    echo json_encode(false);
}

I really hope you can help me because my work depends on these right now,

big thanks!

  • 写回答

1条回答 默认 最新

  • dongya1228 2016-03-08 10:26
    关注

    Your code working in simple cases.

    Test code:

    // ddl for x table is 
    // create table x (id int, val int);
    $DataBase = new DataBase('127.0.0.1', 'root', 'password', 'test');
    var_dump($DataBase->insert('x', array('id' => 8, 'val' => 13)));
    var_dump($DataBase->select('x', array('id' => 8)));
    var_dump($DataBase->update('x', 8, array('val' => 25)));
    var_dump($DataBase->select('x', array('id' => 8)));
    

    Output:

    bool(true)
    array(1) {
      [0]=>
      array(2) {
        ["id"]=>
        string(1) "8"
        ["val"]=>
        string(2) "13"
      }
    }
    bool(true)
    array(1) {
      [0]=>
      array(2) {
        ["id"]=>
        string(1) "8"
        ["val"]=>
        string(2) "25"
      }
    }
    

    You should add test-data which is not working for you. For example, data structures and POST- data for which update is not working.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端