Forum Moderators: open

Message Too Old, No Replies

AJAX JSON INSERT INTO MySQL failed

I am total lost. Pleas help me to get it right!

         

FrankyBkk

1:54 am on Feb 24, 2015 (gmt 0)

10+ Year Member



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

It add a row in MySQL , but no data.

Here is the code:

javascript function:

function jsRecordInsertWrite()
{

var jsObject = {
"Item": document.form_articles.Item.value,
"ItemNo": document.form_articles.ItemNo.value,
"Price": document.form_articles.Price.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 : JSON.stringify(jsObject),
type : 'POST',
success: updatePage,
error: printError
});
}


insertarticle.php

<?php

$link = mysql_connect('test.test.com:3306', 'admin0', 'password1234');

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
$jsondata = file_get_contents('data.json');
$data = json_decode($jsondata, true);

$Item = $data['Item'];
$ItemNo = $data['ItemNo'];
$Price = $data['Price'];

//insert into mysql table
$sql = "INSERT INTO articles (Item, ItemNo, Price) VALUES('$Item','$ItemNo','$Price')";

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

//database connection close
mysql_close($link);

//}

?>

Fotiman

2:18 am on Feb 24, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Welcome to WebmasterWorld!
I would look closely at this:
$jsondata = file_get_contents('data.json');

You're telling it to get the contents of a file, but what you really need to do is get the data from the $_POST [php.net] variable.

FrankyBkk

3:19 am on Feb 24, 2015 (gmt 0)

10+ Year Member



Thank you for your incredible fast answer. I will look into it.

Can you please give me the correct code for this? I tried so many things. I am running out of ideas.

You would make me happy!

Fotiman

3:39 am on Feb 24, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I haven't written PHP code in some time, but I think you want to do something like this:

$Item = $_POST["Item"];
$ItemNo = $_POST["ItemNo"];
$Price = $_POST["Price"];

But instead of passing the stringified version, you want to pass the object:

$.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
});


But note, this code is subject to SQL Injection attacks. So you probably want to look into that once you get this part working.

FrankyBkk

3:42 am on Feb 24, 2015 (gmt 0)

10+ Year Member



Yippieh Yeah!

A very big thank you. I will prepare against injection, you opened my eyes.

THANK YOU VERY MUCH!

FrankyBkk

4:04 am on Feb 24, 2015 (gmt 0)

10+ Year Member



I just tested it. It does not work.

Fotiman

4:04 am on Feb 24, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Glad to help. :)

FrankyBkk

4:06 am on Feb 24, 2015 (gmt 0)

10+ Year Member



Fotiman. I am very sorry, but it did not work. Still the same problem.

It add the row, but no data in it.

Fotiman

4:25 am on Feb 24, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Haha... celebrated too quickly. :)
Ok, first you should make sure that the jsObject has the values you think it does. So if you do something like:
console.log(jsObject);
prior to the ajax call, then you can examine it in the browser developer tools.
Also, you have an extra comma at the end of the line that assigns to "Price".

FrankyBkk

4:58 am on Feb 24, 2015 (gmt 0)

10+ Year Member



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 auto increment ID, the other NULL is for automatic timestamp

Fotiman

1:16 pm on Feb 24, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ok, now try using console.log to examine jsObject before the ajax call.