0

The below is posting in dev console as POST: 404. I'm guessing my php is not correct. What am I doing wrong here?

On this JS side I'm doing:

    ........
    user = JSON.stringify(user); // updated Data, want to push to db table blob column
    pushData ();

    function pushData (){
        const ajax = $.ajax({
            method: 'POST',
            url: `./Settings.php`,
            data: {obj_json: JSON.stringify(user) },
            dataType: "json",
            success: function(result) {
                //Write your code here
                console.log("data posted?");
            }
          });
        }
  .....

/Settings.php (the class used has valid db connection, my obj_json is the column that is set to blob in db table). I am not really sure what to do with $obj

class Settingz extends \myApp\Data
{

    public function insertBlob($obj, $last_update) {
        $obj = $_POST['obj_json'];

        $sql = "INSERT INTO MY_TABLE(last_update,obj_json) VALUES(:last_update,:obj_json)";
        $stmt = $this->pdo->prepare($sql);

        $stmt->bindParam(':last_update', $last_update);
        $stmt->bindParam(':obj_json', $blob, PDO::PARAM_LOB);

        return $stmt->execute();
    }
  }
5
  • 1
    For one thing, you have :obj_json in VALUES, but binding with :prefs_json. Commented Feb 29, 2020 at 22:15
  • Whoops honest copy paste typo. Fixed now here in SO. Commented Feb 29, 2020 at 22:15
  • And that is why this comments area exists :-)) Commented Feb 29, 2020 at 22:16
  • @FunkFortyNiner how do I handle it coming from POST? not filePath Commented Feb 29, 2020 at 22:16
  • You still have a typo objs_json with an "s". Handling files is usually done with $_FILES in a form when done in pure PHP instead of $_POST and with an proper enctype. If this is JS or OOP related, I'm not the guy for this. Commented Feb 29, 2020 at 22:19

1 Answer 1

1

With OCI8 you could use a Temporary LOB (see p.235 of The Underground PHP and Oracle Manual), but with PDO_OCI, use a RETURNING clause like:

<?php


try {
    $db = new PDO('oci:dbname=localhost/orclpdb1', 'cj', 'welcome');
}
catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
    exit;
}

$stmt = $db->prepare("drop table cjblob");
$stmt->execute();

$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$stmt = $db->prepare("create table cjblob (id number, data blob)");
$stmt->execute();

function do_insert($db, $id, $data)
{
    $stmt = $db->prepare("insert into cjblob (id, data) values (:id, empty_blob()) returning data into :blob");
    $stmt->bindParam(':id', $id);
    $stmt->bindParam(':blob', $blob, PDO::PARAM_LOB);
    $blob = null;
    $db->beginTransaction();
    $stmt->execute();

//  var_dump($blob);  // $blob becomes a stream

    fwrite($blob, $data);
    fclose($blob);
    $db->commit();
}

do_insert($db, 1, str_pad("Z",  40000, "Z"));

// Fetch it back
$stmt = $db->prepare('select data from cjblob where id = ?');
$id = 1;
$stmt->execute(array($id));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($row);
var_dump(stream_get_contents($row['DATA']));

?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.