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 cplex运行后参数报错是为什么
  • ¥15 之前不小心删了pycharm的文件,后面重新安装之后软件打不开了
  • ¥15 vue3获取动态宽度,刷新后动态宽度值为0
  • ¥15 升腾威讯云桌面V2.0.0摄像头问题
  • ¥15 关于Python的会计设计
  • ¥15 聚类分析 设计k-均值算法分类器,对一组二维模式向量进行分类。
  • ¥15 stm32c8t6工程,使用hal库
  • ¥15 找能接spark如图片的,可议价
  • ¥15 关于#单片机#的问题,请各位专家解答!
  • ¥15 博通raid 的写入速度很高也很低