2016-03-08 52 views
-1

所以我得到了這段代碼,它是一個PHP腳本,它將我的數據庫與我的應用程序進行通信。但是,不知何故,更新功能根本不起作用,我無法弄清楚什麼是錯誤的 - 雖然我99%肯定它不是應用程序本身。數據庫<> PHP通信腳本不工作

這裏是整個PHP素文字

<?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); 
} 

我真的希望你能幫助我,因爲我的工作依賴於現在這些權利,

非常感謝!

+2

你有沒有做過任何調試?像echo'ing你的查詢,看看它是否會直接在phpmyadmin/mysql中執行? – Epodax

+1

提示:在這裏傾倒160行代碼,希望有人會發現它們的錯誤不是這是如何工作的... – deceze

+0

問題是這是由一些不是朋友寫的,不再是兄弟,我有點依賴於這個但實際上我對PHP一無所知......我知道這不是最好的方式,我不想傳播我的戲劇,但我需要幫助:P – marsh

回答

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-數據。