Brutally Roll Your Own Backend – Part 4

7 Responses

  1. MJ says:

    Hello Craig,
    I figured it out , problem with Delphi 10.4 IDE Rest Debugger . I uses Postman and worked. I will move to the next Videos.

    Best regards,

    Mohamed

  2. MJ says:

    Dear Craig,

    Sorry , for many replies . I am sharing the scripts

    Problem:
    Post is not working , no insertion of recordes into regions
    table. Using the delphi Delphi 10.4 IDE Rest Debugger

    This is a link to the scripts:

    https://www.dropbox.com/sh/6rb8s1ekzvnyn3c/AADBy2Ag6InxdmCAxgvOMBrGa?dl=0

    Best regards,

    Mohamed

  3. MJ says:

    Sorry , format is missed up . You can delete it. I will share it with a link

  4. MJ says:

    ———————————–crud.php———————–
    ?php

    class CRUD {

    function doRead() {

    return “[GET] Not implemented.”;

    };

    function doCreate() {

    return “[POST] Not implemented.” ;

    };

    function doUpdate() {

    return “[PUT] Not implemented.” ;

    };

    function doDelete() {

    return “[DELETE] Not implemented.” ;

    };

    function getResponse() {

    $method = $_SERVER[‘REQUEST_METHOD’] ;

    if ( $method == ‘GET’) {

    return $this->doRead() ;
    };

    if ( $method == “POST”) {

    return $this->doCreate() ;
    } ;

    if ( $method == ‘PUT’) {

    return $this->doUpdate() ;
    };

    if ( $method == ‘DELETE’) {

    return $this->doDelete() ;

    } ;

    };

    }
    ?>

  5. MJ says:

    Hi Craig,
    Thanks for the series , Following is the script , I have proplem with post . it is not inserting into regions :

    table = $table ;
    $this->keyField = $keyField ;

    }

    function doRead() {
    global $conn;
    $sql = “select * from ” . $this->table . “;” ;
    $stmt = $conn->prepare($sql);
    $stmt->execute();
    $results = $stmt->fetchAll(PDO::FETCH_ASSOC) ;
    return json_encode($results) ;
    }

    function doCreate() {
    global $conn;

    $inserted = array() ;

    $data = json_decode(file_get_contents(“php://input”)) ;

    foreach($data as $entry) {

    $paramstr = “” ;
    $fieldstr = “” ;
    $first = true ;
    foreach( $entry as $key => $value ) {

    if ($first) {

    $fieldstr = $fieldstr . $key ;
    $paramstr = $paramstr . “:$key” ;
    $first = false ;

    } else {

    $fieldstr = $fieldstr . “,$key” ;
    $paramstr = $paramstr . “,:$key” ;

    }

    }
    $sql = “insert into ” . $this->table . “( $fieldstr ) VALUES ( $paramstr ) ;”;

    $stmt = $conn->prepare($sql);

    foreach($entry as $key => $value ) {
    $stmt->bindValue(“:$key”, $value ) ;

    }

    $stmt->execute();

    $inserted[] = $conn->lastInsertId();

    }

    $sql = “insert into ” . $this->table .
    “WHERE” . $this->keyField .
    ” in ( ” . implode( “,” , $inserted ) . “) ;” ;
    $stmt = $conn->prepare($sql);
    $stmt->execute();
    $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
    return json_encode($results);
    }

    }
    ?>

    —————————————–db.php—————-
    setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch (PDOException $e) {
    echo “Connection failed : ” . $e->getMessage();
    die();
    }
    ?>
    ——————————-crud.php————————
    doRead() ;
    };

    if ( $method == “POST”) {

    return $this->doCreate() ;
    } ;

    if ( $method == ‘PUT’) {

    return $this->doUpdate() ;
    };

    if ( $method == ‘DELETE’) {

    return $this->doDelete() ;

    } ;

    };

    }
    ?>
    ———————————–index.php—————-
    getResponse();

    ?>

    ————————————

    Best regards,

    Mohamed

    Best regards,

    Mohamed

  6. MJ says:

    Dear Graig,

    I have registered with Linode . I am following along , but got stuck with crudtable script . Can you share the correct scripts.

    Thanks and best regrads,

    Mohamed Aradi

    • This series is several years old, and some things have changed with the google push services in the meanwhile.
      You may not be able to fully follow along at this point.
      I can certainly try to help with mysql/php scripts though, can you give more information about the problem you’re having?

Leave a Reply