0

Hello guys hope you can help me with this, becuase i have no idea... I want to upload an image into a database table i just need to upload the image, and some other data, right now i save all the info in the data base using the following Code:

 $.ajax({
      url: 'AccionesPHP.php',
      type: 'POST',
      data: {funcion:'SaveFamily',name:nam,lastn:ape,pasp:pas,dateb:dob,pased:ped,type:typ}
    })

    .done(function(datos)
    {
      if(datos==1)
      {
        alert('success');
      }
    });

and in the PHP side i have the following code:

case 'SaveFamily':
        $nom = $_POST['name'];
        $lastname = $_POST['lastn'];
        $pasp = $_POST['pasp'];
        $dateob = $_POST['dateb'];
        $pexpd = $_POST['pased'];
        $type = $_POST['type'];
        $query= mysqli_query($link,"INSERT INTO ticketdatarecep VALUES(NULL,".$_SESSION['id_users'].", '$nom', '$lastname', '$pasp', '$dateob', '$pexpd', '$type')");
        if($query == true)
        {
            echo "1";
        }
        else
        {
            echo mysql_error();
        }

so i have no form in my HTML and i want to add an input so user can uoload an image and save it in the same table and everything, bu i have no clue how to do it, all i have seen use FORM to send the data but still dont know how to send the image using $.ajax and how to catch the image data in my PHP code.

Hope you can give me some ideas.

  • Use [FormData API](https://developer.mozilla.org/en-US/docs/Web/API/FormData) – charlietfl Feb 14 '16 at 23:02
  • @Gabriel Mendez Why do you wanna save the image file inside the database? I would suggest put it in the separate folder and save the path of the file in the database. Images in database will make the database bulks in turn reducing the performance of the application. Tell me if this solution woul work for you. I will give you the neceessary code – pritesh Feb 14 '16 at 23:03
  • This might help you: http://stackoverflow.com/questions/17/binary-data-in-mysql But storing binary data in a mysql database is usually not a good idea. – snixtho Feb 14 '16 at 23:07
  • Ok, let's here your words and i then upload the image and then just looked for the path, how do i do that and i would like to save the image with an specific name – Gabriel Mendez Feb 15 '16 at 16:24

1 Answers1

0

There are two options on storing images in a a database. The most common approach is a standard upload of a file, where you save your image to a specified locations (i.e. mysite.com/uploads) then in the database you save the URL to the image. The other option is a BLOB, where you actually save the Image in it's entirety into the database.

Figure out which method you want and the advantages, but you can see how to upload a file via here

http://php.net/manual/en/features.file-upload.post-method.php

VinnieS
  • 73
  • 11