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);
1

1 Answer 1

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.
Sign up to request clarification or add additional context in comments.

1 Comment

It works. I saw my issue before your post... It is realy friday? I feel as if it is Monday. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.