MySQL Inserting Problem with Ajax and Php - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Sunday, November 21, 2021

MySQL Inserting Problem with Ajax and Php

I have a photo that I capture with javascript and with ajax I send it to the php side. Then I can't save the photo to the file on the php side, there is no problem at this time, but it does not save it to the database.

Note that : When I check it, no error is returned, but it does not save to the database.

Javascript code :

        var canvas = document.getElementById("my-canvas");
        var photo = canvas.toDataURL('image/jpeg');                
        $.ajax({
          method: 'POST',
          url: 'photo_upload.php',
          data: {
          photo: photo
          }
        });

PHP Code :

if (isset($_POST['photo'])){
    $data = $_POST['photo'];
    if($data != "" || $data != null){
    list($type, $data) = explode(';', $data);
    list(, $data)      = explode(',', $data);
    $data = base64_decode($data);

    mkdir($_SERVER['DOCUMENT_ROOT'] . "/Proje/Dosyalar/HandDetection/photos");

    file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/Proje/Dosyalar/HandDetection/photos/".time().'.jpeg', $data);
    die;

    //img db insert
    $handsImgQuery=$db->prepare("INSERT INTO detections SET
        detection_title=:detTitle,
        detection_time=:detTime,
        detection_photo=:detPhoto,
        det_email_status=:detEmailStatus
    ");

    $result=$handsImgQuery->execute(array(
        'detTitle' => "Hand Detection Image",
        'detTime' => date("Y-m-d h:i"),
        'detPhoto' => time()+".jpeg",
        'detEmailStatus' => true
    ));

    }
}


source https://stackoverflow.com/questions/70048585/mysql-inserting-problem-with-ajax-and-php

No comments:

Post a Comment