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

时间:2016-03-08 09:58:33

标签: php mysql database mysqli

所以我得到了这个代码,它是一个PHP脚本,用我的应用程序传递我的数据库。但是,某种程度上更新功能根本不起作用,我无法弄清楚哪些是错的 - 尽管我99%确定它不是应用程序本身。<\ n / p>

这里是整个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);
}

我真的希望你能帮助我,因为我的工作现在依赖于这些,

非常感谢!

1 个答案:

答案 0 :(得分:0)

您的代码在简单的情况下工作。

测试代码:

// 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)));

输出:

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"
  }
}

您应该添加不适合您的测试数据。例如,更新无效的数据结构和POST数据。