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
https://forums.phpfreaks.com/topic/294856-php-ajax-json-insert-into-mysql-failed/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.