-1

i'm kind of new with php and i'm trying to upload some details into a database but it includes an image, something like this:

sql = "
INSERT INTO fileform 
(`FirstName`
,`LastName`
,'IMAGE'
) VALUES ('?', '?', '$fileName')";


mysqli_stmt_bind_param($stmt, "ss", $firstname, $LastName). 

On the second line of the codes above, i know that s-strings,i-integer,b-boolean etc, what do i use as the datatype for the image file?

Dharman
  • 30,962
  • 25
  • 85
  • 135
LEGIT
  • 21
  • 4
  • 4
    You should avoid saving file to database. Instead of directly save the file to database, use the file path instead. Your column type will be a varchar – JessGabriel Sep 20 '19 at 12:49
  • You were coming ok until you directly placed a value into the query keep on using prepared statements and I would advise that you save an image in file system and store that image path into the database – Masivuye Cokile Sep 20 '19 at 12:50
  • 1
    move file into the some folder/directory, dont store image in DB, just store name of image in DB. – Devsi Odedra Sep 20 '19 at 12:51
  • 1
    Note that 'image' is a string. Perhaps you were thinking of \`image\` – Strawberry Sep 20 '19 at 12:51
  • What i'm trying to create is a site where the user filling up the form in the website has to upload a requested image with any of these file extensions -> jpg, jpeg, png, and pdf. what i'm trying to say is...the image has to come from the user/applicant's device. – LEGIT Sep 20 '19 at 12:57
  • B stands for blob, which is what your image is. – Dharman Sep 20 '19 at 13:29
  • Also you need to remove the quotes around `?` and parameterize the file name too. – Dharman Sep 20 '19 at 13:30
  • 1
    @SandraChijioke you must learn one thing at a time. First, you need to learn how to work with a database from PHP. Only after that you may start working with files. Doing a lot of things at the same time will do you no good. – Your Common Sense Sep 20 '19 at 13:46
  • Thanks @Dharman, i'll try using the B – LEGIT Sep 20 '19 at 13:55

3 Answers3

1

You should NOT save files/images in databases, but instead you need to upload the file to your machine and then save the path of the file as text field in database see this link: https://www.w3schools.com/php/php_file_upload.asp

Majid
  • 17
  • 3
  • 1
    The question is not **Should I save images in database?** but OP is clearly asking for **How to insert images into a database?** – B001ᛦ Sep 20 '19 at 13:19
  • 1
    @B001ᛦ when someone asking how can I do the wrong thing you just tell him don't do that instead of that do the right thing. – Majid Sep 20 '19 at 13:29
  • Thanks Majid. I appreciate i'll check the link you gave me – LEGIT Sep 20 '19 at 13:54
1

You should not upload the file (image etc...) to the database! just upload the file using common php commands, then save the image path and other information to database! for example, using php upload the image(Name.png) or any file other, that goes in this path(just example) :

uploads/img/Name.png

then, save that path in your database for after uses.

Pooya Behravesh
  • 131
  • 2
  • 10
  • well i think,common databases actually are text base not file! best to read this link : https://stackoverflow.com/questions/43268664/what-is-a-file-based-database – Pooya Behravesh Sep 21 '19 at 20:40
1

Don't store image in database

Upload the images into preferred location and store the location in a global variable.Store filename to the database as varchar.

Code for uploading file:

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
?>
  • 2
    The question is not **Should I save images in database?** but OP is clearly asking for **How to insert images into a database?** – B001ᛦ Sep 20 '19 at 13:20
  • _On the second line of the codes above, i know that s-strings,i-integer,b-boolean etc, what do i use as the datatype for the image file?_ Here she asked for the type @B001ᛦ – Mohammed Dilruba Pamangadan Sep 20 '19 at 13:23
  • Two things: getimagesize emits a warning when the file is not an image. using getimagesize doesn't prevent the malicious upload – Your Common Sense Sep 20 '19 at 13:45