0

I have written insertion program in php. here it is:

<?php
$conn = new mysqli('localhost', 'root', '','optimize_statusdb');
 $email = $_POST['user_email'];
$m_id = $_POST['message_id'];
$t_id = $_POST['thread_id']; 
    $conn->query("INSERT INTO `trackid`(`user_email`, `message_id`, `thread_id`) VALUES ('$email',$m_id,$t_id)");
?>

Here is the ajax function through which I trying to post the data into mysql:

    View.on("sent", function(event) {
        // If the user doesn't want the email to be tracked, we do nothing.
        if (window.hunter_tracking_activated == true) {
          $.ajax({
                  type: "POST",
                  url: "http://localhost:90/track/track.php",
                  data: {
                        thread_id: event.threadID,
                        message_id: event.messageID,
                        user_email: sdk.User.getEmailAddress(),
              token: token
                    },
                  dataType: "JSON"
                });

          countUsage();
          chrome.storage.sync.set({ "last_tracked_email_date": Date.now() });
          alert("hello");
        }
      });
    });
  }

I am trying to insert into the values from the data field in the JSON from the ajax code. But after running the program, when I am trying to see in the table I could not see any values getting inserted.
Kindly, suggested me how I can achieve this using my code. How I can insert the values through Ajax to my mysql DB?

Jaffer Wilson
  • 7,029
  • 10
  • 62
  • 139
  • 2
    Your code is vulnerable to [SQL-Injections](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). Please start using Prepared, Parameterized Queries. – Charlotte Dunois Apr 08 '17 at 13:26
  • Not clear what is or isn't working. Is ajax request being made and alert firing? Should parameterize the insertion query also – charlietfl Apr 08 '17 at 13:27
  • @charlietfl Well, as I have mentioned that the values in the json `data` is what I am trying to insert. Hope this make sense. :) – Jaffer Wilson Apr 08 '17 at 13:29
  • 1
    not really, there is no **json** and you didn't answer if request is made or not – charlietfl Apr 08 '17 at 13:31
  • Yes the request is made when I compose a mail. I think my request is not reaching the ` url: "http://localhost:90/track/track.php" `. I tried to check whether the request is served or not using `alert`. – Jaffer Wilson Apr 08 '17 at 13:37
  • Add some error handling and inspect actual request in browser dev tools network. Too many unknowns without some basic debugging info – charlietfl Apr 08 '17 at 14:00
  • Create a `success: function(response) { console.log(response); }` in your ajax, then comment out the `dataType: "JSON"`, then in your php just at the top of the page do `error_reporting(E_ALL); print_r($_POST); exit;` You should see feedback in the ajax success via the console log. If you don't try doing this kind of stuff you are flying blind and hoping everything is just supposed to work without error. – Rasclatt Apr 08 '17 at 15:12
  • @Rasclatt May be you right. Lt me try it and I will let you know. – Jaffer Wilson Apr 10 '17 at 05:10

0 Answers0