Jump to content

PHP AJAX JSON INSERT INTO MySQL failed


ScoobeedoCool

Recommended Posts

Hello to everybody!

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

It add a row in MySQL , but no data.

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

...

Here is the full code:

//+++++++++++++++++++++++++++++++++++++++++++++++++
Javascript:
//+++++++++++++++++++++++++++++++++++++++++++++++++
 

    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
        $.ajax({
            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

     $link = mysql_connect('localhost', 'admin0', 'star1star1star0');
    if (!$link) {
    die('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

(NULL,'$Item','$ItemNo','$Material','$Age','$ItemSize','$Price','$Info','$InfoRed','$ArrivalDate','$ArrivalDateShown','$

MainPicLink','$ItemCondition',NULL,'$ItemCategory')";

    if(!mysql_query($sql))
    {
    die('Error : ' . mysql_error());
    }

    //database connection close
    mysql_close($link);

    //}

    ?>

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.