Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2015

    Unanswered: AJAX JSON INSERT INTO MySQL failed

    Hello to everybody!

    I am total lost. What is wrong? I try to INSERT INTO MySQL.

    It add a row in MySQL , but it add no data.

    I am sitting since some days on it and dont understand it. I checked hundreds of web pages. I really need help!!!


    Here is the full code:


        function jsRecordInsertWrite()
            var jsObject = {
                "ID": document.form_articles.ID.value,
                "Item": document.form_articles.Item.value,
                "ItemNo": document.form_articles.ItemNo.value,
                "Material": document.form_articles.Material.value,
                "Age": document.form_articles.Age.value,
                "ItemSize": document.form_articles.ItemSize.value,
                "Price": document.form_articles.Price.value,
                "Info": document.form_articles.Info.value,
                "InfoRed": document.form_articles.InfoRed.value,
                "ArrivalDate": document.form_articles.ArrivalDate.value,
                "ArrivalDateShown": document.form_articles.ArrivalDateShown.value,
                "MainPicLink": document.form_articles.MainPicLink.value,
                "ItemCondition": document.form_articles.ItemCondition.value,
                "ItemTimestamp": document.form_articles.ItemTimestamp.value,
                "ItemCategory": document.form_articles.ItemCategory.value
            // ... the AJAX request is successful
            var updatePage = function (response) {
                alert("insert record successful");
            // ... the AJAX request fail
            var printError = function (req, status, err) {
                alert("insert record failed");
            // Create an object to describe the AJAX request
                url        : 'insertarticle.php',
                dataType   : 'json',
                contentType: 'application/json; charset=UTF-8', // This is the money shot
                data       : jsObject,
                type       : 'POST',
                success: updatePage,
                error: printError
    Here is insertarticle.php

    PHP Code:

        if (!
    $link) {
    'Could not connect: ' mysql_error());
    $db_selected mysql_select_db('sob'$link);
        if (!
    $db_selected) {
        die (
    'Can\'t use foo : ' mysql_error());

    //read the json file contents
    $ID =  $_POST['ID'];
    $Item =  $_POST['Item'];
    $ItemNo =  $_POST['ItemNo'];
    $Material =  $_POST['Material'];
    $Age =  $_POST['Age'];
    $ItemSize =  $_POST['ItemSize'];
    $Price =  $_POST['Price'];
    $Info =  $_POST['Info'];
    $InfoRed =  $_POST['InfoRed'];
    $ArrivalDate =  $_POST['ArrivalDate'];
    $ArrivalDateShown  =  $_POST['ArrivalDateShown'];
    $MainPicLink =  $_POST['MainPicLink'];
    $ItemCondition =  $_POST['ItemCondition'];
    $ItemTimestamp =  $_POST['timestamp'];
    $ItemCategory =  $_POST['ItemCategory'];

    //insert into mysql table
    $sql "INSERT INTO articles(ID, Item, ItemNo, Material, Age, ItemSize, Price, Info, InfoRed, ArrivalDate, 

    ArrivalDateShown, MainPicLink, ItemCondition, ItemTimestamp, ItemCategory) VALUES



    'Error : ' mysql_error());

    //database connection close


    The first NULL is for autoincrement ID, the other NULL is for automatic timestamp

  2. #2
    Join Date
    Jan 2007
    Provided Answers: 12
    What's the error thrown by MySQL?
    As with all of these things, you need to prove that the SQL is sound, before the water gets muddied by other languages.

    I suggest you print the value of $sql to screen and test this exact output directly against your MySQL instance.

    If you're still stuck post the value of $sql here along with the actual error message and we can assist further.
    Home | Blog

  3. #3
    Join Date
    Nov 2004
    out on a limb
    Provided Answers: 59
    As gvee suggests there are multipel places where the error could lurk. it could be in the Javascript, the AJAX/PHP script or the MySQl INSER or a mixture.

    normally I'd suggest start at the beginning of the chain, prove each element works and then move on, however in this instance I'd suggest workign back from the SQL insert
    the fact the sdcript is inserting a ro with bblank values suggests to me the SQL is working fine, but smewhere donw the line you aren't settign any values.

    to prove that
    in you PHP script after you have assigned values from the post variables I'd suggest repalcing the working variables with known good values... run thew script once and see if those values appear int he DB.

    if they do you've proved the SQL works

    You could short circuit that by testing the input in the script. at the very least you should be validating the data coming from an unknown untrusted external source such as a web page. even if you dont' want to check for 'sane' data (ie correct range, reasonable vlaues) you MUSt check to protect against SQL injection attacks. dont trust any input from any source, especially when dealing with web scripting

    it could be the that java script isn't setting the values to be picked up in the PHP script
    it could be that you aren't using the correct PHP working variables, you may need to IMPORT or you may need to use a different set to POST

    workj through each segment untill you find what is the root cause.
    p[rove each segemnt works and then move onto the next segment in the chain. it dopenst'# matter if you woirk fromt eh back forwards (SQL -> PHP -> Javascript) or the other way round.

    my expectation is that the proible,k is in the Javascript to PHP border. frankly I dont' know that much about JSON So I cna't identiofy the fault. but Im guessign you aren't sending the values you think you are form Javscript to the PHP script
    I'd rather be riding on the Tiger 800 or the Norton

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts