Last active
          March 29, 2024 08:20 
        
      - 
      
 - 
        
Save arvindsvt/61e2fc1b5755b264f35779699bf5fa5f to your computer and use it in GitHub Desktop.  
    CRUD API Integration using Javascript HTML - AJAX CALL
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | https://www.youtube.com/watch?v=e5Tnmw_ie_w | |
| https://gitlab.com/balaji123balaji/javascropt-api-integration | |
| https://www.youtube.com/watch?v=nSDy_g8tTGE&list=PLMPZIgg1n4JmLEL3kNVbVRTTBgkiFIzF8 | |
| https://github.com/hilalahmad32/php-api-with-jwt-auth/blob/master/database/Database.php | |
| https://dcodemania.com/post/crud-app-php-oop-pdo-mysql-fetch-api-es6 | |
| https://dcodemania.com/post/crud-app-php-mysqli-prepared-statement | |
| https://dcodemania.com/post/crud-application-image-upload-laravel-8-ajax-sweetalert-datatable | |
| https://github.com/ruhid206/php-ajax-crud-yt/blob/master/server.php | |
| https://www.youtube.com/watch?app=desktop&v=e0HLuJ9QK9k | |
| function insertOrUpdate($table,$params=array()){ | |
| global $conn; | |
| $sqlQuery = 'CREATE TABLE IF NOT EXISTS '.$table.' (id INT NOT NULL AUTO_INCREMENT, '; | |
| foreach($params as $field=>$val){ | |
| if($field == 'id'){ continue ; } | |
| $sqlQuery .= ' '.$field.' VARCHAR( 250 ),'; | |
| } | |
| $sqlQuery .= ' PRIMARY KEY ( `id` ));'; | |
| $sqlQuery .='INSERT INTO `'.$table.'` (`'.implode('`, `',array_keys($params)).'`) VALUES ("' . implode('", "', $params) . '")'; | |
| $args=array(); | |
| foreach($params as $field=>$value){ | |
| $args[]= $field.'=VALUES('.$field.')'; | |
| } | |
| $sqlQuery .=' ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id) , '.implode(',',$args); | |
| if ( mysqli_query($conn, $sqlQuery) ) { | |
| return mysqli_insert_id($conn); | |
| } else { | |
| return false; | |
| } | |
| } | |
| <?php | |
| class Database | |
| { | |
| private $localhost = "localhost"; | |
| private $username = "root"; | |
| private $password = ""; | |
| private $database = "php_api_jwt"; | |
| private $mysqli = ""; | |
| private $result = array(); | |
| private $conn = false; | |
| //connect database using consturcted method | |
| public function __construct() | |
| { | |
| if (!$this->conn) { | |
| $this->mysqli = new mysqli($this->localhost, $this->username, $this->password, $this->database); | |
| $this->conn = true; | |
| if ($this->mysqli->connect_error) { | |
| array_push($this->result, $this->mysqli_connection_error); | |
| return false; | |
| } | |
| } else { | |
| return true; | |
| } | |
| } | |
| // insert data | |
| public function insert($table, $params = array()) | |
| { | |
| if ($this->tableExist($table)) { | |
| $table_column = implode(', ', array_keys($params)); | |
| $table_value = implode("', '", array_values($params)); | |
| $sql = "INSERT INTO $table ($table_column) VALUES ('$table_value')"; | |
| if ($this->mysqli->query($sql)) { | |
| array_push($this->result, true); | |
| return true; | |
| } else { | |
| array_push($this->result, false); | |
| return false; | |
| } | |
| } else { | |
| return false; | |
| } | |
| } | |
| // get data | |
| public function select($table, $row = "*", $join = null, $where = null, $order = null, $limit = null) | |
| { | |
| if ($this->tableExist($table)) { | |
| $sql = "SELECT $row FROM $table"; | |
| if ($join != null) { | |
| $sql .= " JOIN $join"; | |
| } | |
| if ($where != null) { | |
| $sql .= " WHERE $where"; | |
| } | |
| if ($order != null) { | |
| $sql .= " ORDER BY $order"; | |
| } | |
| if ($limit != null) { | |
| $sql .= " LIMIT $limit"; | |
| } | |
| $query = $this->mysqli->query($sql); | |
| if ($query) { | |
| $this->result = $query->fetch_all(MYSQLI_ASSOC); | |
| return true; | |
| } else { | |
| return false; | |
| } | |
| } else { | |
| return false; | |
| } | |
| } | |
| // update data | |
| public function update($table, $params = array(), $where = null) | |
| { | |
| if ($this->tableExist($table)) { | |
| $arg = array(); | |
| foreach ($params as $key => $val) { | |
| $arg[] = "$key = '{$val}'"; | |
| } | |
| $sql = "UPDATE $table SET " . implode(', ', $arg); | |
| if($where != null){ | |
| $sql .=" WHERE $where"; | |
| } | |
| if ($this->mysqli->query($sql)) { | |
| array_push($this->result, true); | |
| return true; | |
| } else { | |
| array_push($this->result, false); | |
| return false; | |
| } | |
| } else { | |
| return false; | |
| } | |
| } | |
| // delete data | |
| public function delete($table, $where = null) | |
| { | |
| if ($this->tableExist($table)) { | |
| $sql = "DELETE FROM $table"; | |
| if ($where != null) { | |
| $sql .= " WHERE $where"; | |
| } | |
| if ($this->mysqli->query($sql)) { | |
| array_push($this->result, true); | |
| return true; | |
| } else { | |
| array_push($this->result, false); | |
| return false; | |
| } | |
| } else { | |
| return false; | |
| } | |
| } | |
| // table exist | |
| private function tableExist($table) | |
| { | |
| $sql = "SHOW TABLES FROM $this->database LIKE '{$table}'"; | |
| $tableInDb = $this->mysqli->query($sql); | |
| if ($tableInDb) { | |
| if ($tableInDb->num_rows == 1) { | |
| return true; | |
| } else { | |
| array_push($this->result, $table . " Does not Exist"); | |
| } | |
| } else { | |
| return false; | |
| } | |
| } | |
| // get result | |
| public function getResult() | |
| { | |
| $val = $this->result; | |
| $this->result = array(); | |
| return $val; | |
| } | |
| // close the connection | |
| public function __destruct() | |
| { | |
| if ($this->conn) { | |
| if ($this->mysqli->close()) { | |
| $this->conn = false; | |
| return true; | |
| } | |
| } else { | |
| return false; | |
| } | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment