1

I want to send data via AJAX to a PHP file and in the PHP file the data will save into a mysql database.

This is my AJAX code:

$('a').on('click', function(){
    var href = $(this).attr('href'); 
    var JsSessionID = $(this).data('info');
    var data = 'url=' + href + '&JsSessionID=' + JsSessionID;
    $.ajax({
        url: "/wp-content/themes/twentyfifteen/js/test.php",
        type: "POST",
        data: data,
        success: function(data) {

        }
    });          
});

This is my PHP code: Yes no mysqli.. it is only for testing.

$url = $_POST['url'];
$JsSessionID = $_POST['JsSessionID'];

$verbindung = mysql_connect ("abc","db1", "123")
or die ("keine Verbindung möglich. Benutzername oder Passwort sind falsch");

mysql_select_db("db1")
or die ("Die Datenbank existiert nicht.");


$eintrag = "INSERT INTO test (ID, JsSessionID, URL) VALUES ('', $JsSessionID, $url)";
$eintragen = mysql_query($eintrag);

This code doesn't work.

The data weren't save into my database and I don't know why. The connection is right and the AJAX sends the data correct to my PHP file. I tested it - I try to catch the data and save it into a txt file.

This works.

$datei = fopen("daten.txt","w");
echo fwrite($datei, $url,100);
cgee
  • 1,910
  • 2
  • 22
  • 38
  • Check this link to see if there is a warning on mysql side http://stackoverflow.com/a/47662/5303401 – Ozan Sep 18 '15 at 07:04

1 Answers1

1

You are inserting string (VARCHAR) data in MySQL query without single quotes:

Change:

$eintrag = "INSERT INTO test (ID, JsSessionID, URL) 
VALUES ('', $JsSessionID, $url)";

To:

$eintrag = "INSERT INTO test (ID, JsSessionID, URL) 
VALUES ('', '$JsSessionID', '$url')"; // Observe additional single quotes.
Pupil
  • 23,834
  • 6
  • 44
  • 66
  • It works. I saw my issue before your post... It is realy friday? I feel as if it is Monday. Thank you. – cgee Sep 18 '15 at 07:08