0

Good day... I have to insert a photo into MySQL database,,,, But this message appears

Notice: Undefined index: fileToUpload in C:\wamp\www\great\index.php on line 41

Line 41:

                   GetSQLValueString($_POST['fileToUpload'], "int"));

 
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = 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;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>

<?php require_once('Connections/greatCONN.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form")) {
  $insertSQL = sprintf("INSERT INTO great (id) VALUES (%s)",
                       GetSQLValueString($_POST['fileToUpload'], "int"));

  mysql_select_db($database_greatCONN, $greatCONN);
  $Result1 = mysql_query($insertSQL, $greatCONN) or die(mysql_error());
}
?>
<!DOCTYPE html>
<html>
<body>

<form name="form" action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
    <input type="hidden" name="MM_insert" value="form">
</form>

</body>
</html>

enter image description here

I'll be very thankful for helping me :)

miss omo
  • 1
  • 2
  • Is `fileToUpload` the name of file browse input? Also please add more code. – AnkiiG Jan 05 '16 at 05:56
  • If `fileToUpload` is actually a file, then it would be in `$_FILES['fileToUpload']` and not `$_POST['fileToUpload']` – Sean Jan 05 '16 at 05:57
  • Yes @AnkiiG fileToUpload is the field of photo in MySQL database – miss omo Jan 05 '16 at 05:59
  • Don't edit with photos of your code. Post the actual code. – Sean Jan 05 '16 at 06:20
  • Also, your code photos don't even include `GetSQLValueString($_POST['fileToUpload'], "int"));`, and all references to `fileToUpload` in those photos are `$_FILES['fileToUpload']`. So why do you have `$_POST['fileToUpload']`? – Sean Jan 05 '16 at 06:22
  • @Sean I've posted the whole codes, for ur kind review and help – miss omo Jan 05 '16 at 07:06
  • Can you show your table schema? what is the data type of the column – Ahmed Khan Jan 05 '16 at 07:06
  • @AhmedKhan I've edited my question just right now,,, and insert my table's screenshot – miss omo Jan 05 '16 at 07:17
  • Data type should be blob for image upload and second there is an issue in your insert query it should be `"INSERT INTO great (fileToUpload) VALUES (%s)", GetSQLValueString($_POST['fileToUpload'], "int")` – Ahmed Khan Jan 05 '16 at 07:24
  • @AhmedKhan I've tried it,, and this message shown: Notice: Undefined index: fileToUpload in C:\wamp\www\great\index.php on line 40 Column 'fileToUpload' cannot be null................Line 40: $insertSQL = sprintf("INSERT INTO great (fileToUpload) VALUES (%s)", GetSQLValueString($_POST['fileToUpload'], "int")); !!! – miss omo Jan 05 '16 at 07:43
  • try this `sprintf("INSERT INTO great (id) VALUES (".GetSQLValueString($_POST['fileToUpload'], "int").")");` – Ahmed Khan Jan 05 '16 at 07:46
  • also I refer you to look at this question http://stackoverflow.com/questions/17717506/how-to-upload-images-into-mysql-database-using-php-code – Ahmed Khan Jan 05 '16 at 07:47
  • I'm so sorry for disturbing u @AhmedKhan :) please forgive me ,,,,,, tried it,still n't working :( ......................... error msg said:Notice: Undefined index: fileToUpload in C:\wamp\www\great\index.php on line 40................Line 40: $insertSQL = sprintf("INSERT INTO great (id) VALUES (".GetSQLValueString($_POST['fileToUpload'], "int").")"); – miss omo Jan 05 '16 at 07:54
  • change your POST to FILE – Ahmed Khan Jan 05 '16 at 07:57
  • Changed it to FILES @AhmedKhan ..........Warning: mysql_real_escape_string() expects parameter 1 to be string, array given in C:\wamp\www\great\index.php on line 10 ,,,,,,,,, Line 10: $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); – miss omo Jan 05 '16 at 08:04
  • Yes this will give error because the value of File is not a string. remove this mysql_escape_string( – Ahmed Khan Jan 05 '16 at 08:09
  • removed it @AhmedKhan ,,,,,server run this msg: Duplicate entry '1' for key 'PRIMARY' – miss omo Jan 05 '16 at 08:24
  • are you inserting your primary key too! – Ahmed Khan Jan 05 '16 at 08:26
  • Yess @AhmedKhan I've already insert ID as PRIMARY key – miss omo Jan 05 '16 at 08:30
  • that's why it is giving error! You have already made your primary key auto increment so you don't need to add it every time when you insert data – Ahmed Khan Jan 05 '16 at 08:32
  • done @AhmedKhan ,,,,,,,,,but I don't know how to avoid this msg:(( Undefined index: fileToUpload in C:\wamp\www\great\index.php on line 40 Column 'fileToUpload' cannot be null )) .....Line 40: $insertSQL = sprintf("INSERT INTO great (fileToUpload) VALUES (%s)", GetSQLValueString($_POST['fileToUpload'], "int")); – miss omo Jan 05 '16 at 09:27
  • use this link to know how to upload image on database http://stackoverflow.com/questions/17717506/how-to-upload-images-into-mysql-database-using-php-code – Ahmed Khan Jan 05 '16 at 09:47
  • thanks alot @AhmedKhan for ur cooporation,,,,,,,,,,,,,,one more question pls/ how to make the photo stored in my database be shown in my application form? – miss omo Jan 05 '16 at 09:51
  • Try to google that out – Ahmed Khan Jan 05 '16 at 10:01
  • I will ^__^ thank u very much @AhmedKhan – miss omo Jan 05 '16 at 10:08

3 Answers3

0

Choose datatype varchar instead of integer to upload photo.

0

There should be $_FILES['fileToUpload'] if it is any file

  • my file is (uploads),,,,,,,,(fileToUpload) is photos field in my database – miss omo Jan 05 '16 at 06:55
  • if the file is uploaded, it will be in $_FILES[nameOfFileUploadHtmlInput] <-- not the field in your database, but the html input that is used to upload the file. To verify, var_dump($_FILES) and see what the indexes are. The error is telling you $_POST['fileToUpload'] does not exist. – devlin carnate Jan 15 '16 at 03:24
0

Use $_FILES['fileToUpload'] instead of $_POST['fileToUpload']

Ahmed Khan
  • 518
  • 4
  • 12