0

this is my ajax code

var fd = new FormData();    
            fd.append( 'file', $('#img')[0].files[0]);

var data = '&com='+company+'&loc='+location+'&year='+year+'&desc='+des+'&userid='+userid+'&fd='+fd;



        $.ajax({
             type : "POST",
             url: "insert.php",
             data : data,


                 success: function (response){



                 }
          });

this is php code

$conn =new mysqli("localhost", "root", "","test2");


 $company=isset($_POST['com']) ? $_POST['com']: '';

$local=isset($_POST['loc']) ? $_POST['loc']: '';
$year=isset($_POST['year']) ? $_POST['year']: '';
$description=isset($_POST['desc']) ? $_POST['desc']: '';
$userid=isset($_POST['userid']) ? $_POST['userid']: '';


$query = mysqli_query($conn,"call exp('$userid', '$company', '$local', '$year', '$description')");

$target = "C:/xampp/htdocs/img/";
$target = $target . basename( $_FILES['file']['name']);

$Filename=basename( $_FILES['file']['name']);
if(move_uploaded_file($_FILES['file']['tmp_name'], $target)) {
    $conn =new mysqli("localhost", "root", "","test2");

   $query = mysqli_query($conn,"INSERT INTO experience (image , PersonID) VALUES ('$Filename','$userid')");
    print_r($query);


} else {

    echo "Sorry, there was a problem uploading your file.";
}

how to send param and image in ajax ....As i can send only image separately and data separately in data . as i have try many different code but not working perfectly

knight007
  • 55
  • 7
  • you need to set the content type appropriately in the ajax options. – ADyson Aug 24 '17 at 09:47
  • how to set that – knight007 Aug 24 '17 at 09:48
  • 1
    It's been asked many times before if you google it. Here's just one example https://stackoverflow.com/a/5976031/5947043 – ADyson Aug 24 '17 at 09:49
  • i need pass multiple param data in to php..and i have tried many site code not working – knight007 Aug 24 '17 at 09:51
  • well the code you've got now will never work. So I suggest you follow some examples which are shown to work. If you still can't get it right then post your updated code here. BTW building your querystring data by hand like that is a bad idea, you can get encoding issues. Just pass `data` as a JS object instead and let jQuery handle the encoding for you. – ADyson Aug 24 '17 at 09:56
  • P.S. your code is vulnerable to SQL injection attacks because you directly insert unsanitised user input into your query. use parameterised queries and prepared statements to better protect your data from hackers. See http://bobby-tables.com for an explanation of the problem and also some examples of how to do it safely using PHP/mysqli – ADyson Aug 24 '17 at 10:02

1 Answers1

0
var guestbookSendMessage = new FormData();

guestbookSendMessage.append('com',company);
guestbookSendMessage.append('loc', location);
guestbookSendMessage.append('year',year);
guestbookSendMessage.append('desc',des);
guestbookSendMessage.append('userid',userid);

guestbookSendMessage.append('file', $("#img")[0].files[0]);

        $.ajax({
             type : "POST",
             url: "insert.php",

             data : guestbookSendMessage,
             cache: false,
               contentType: false,
               processData: false,


                 success: function (response){



                 }
          });

this how its done.....

knight007
  • 55
  • 7