php


PHP : unable to insert json string into database


I am new to php. I am sending a json string in post. That json string is array list of products. which I will be sending from android to php webservice. When I var_dump my array after decode it shows null.
Here is my code
<?php
$servername = "mysql.hostinger.in";
$username = "username";
$password = "password";
$dbname = "db_name";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$orderDetails = mysqli_real_escape_string($conn, $_POST['orderDetails']);
$name = mysqli_real_escape_string($conn, $_POST['name']);
$refNo = mysqli_real_escape_string($conn, $_POST['refNo']);
$date = mysqli_real_escape_string($conn, $_POST['date']);
$amount = mysqli_real_escape_string($conn, $_POST['amount']);
$objs = json_decode($orderDetails, true);
//var_dump($objs);
$sql = "INSERT INTO tblOrder (name, refNo, date, quality, design, qty, pcs, amount) values ('$name', '$refNo', '$date', 'mix quality', 'mix design', '1250', '2', '$amount')";
mysqli_query($conn, $sql);
$orderId = mysqli_insert_id($conn);
if(is_array($objs) || is_object($objs)){
foreach($objs as $item) {
$sql2 = "INSERT INTO tblOrderDetails (orderId, quality, design, shade, quantity, rate)
VALUES ('$orderId', '".$item['qualityName']."', '".$item['designName']."', '".$item['name']."','".$item['pcs']."', '".$item['amount']."')";
mysqli_query($conn, $sql2);
}
}
mysqli_close($conn);
?>
And here is my json string which I am passing as orderDetails in post.
[
{
"amount": "1000",
"qualityName": "Cotton",
"designName": "11001",
"discPercent": "5",
"image": "http://website.com/Demo/images/1.jpg",
"name": "Black",
"position": 0,
"pcs": 3,
"id": 1
},
{
"amount": "900",
"qualityName": "Cotton",
"designName": "11001",
"discPercent": "9",
"image": "http://website.com/Demo/images/2.jpg",
"name": "Green",
"position": 0,
"pcs": 2,
"id": 2
}
]
In android I am using Volley Library to send data in post.
Please suggest me something.
The JSON you're getting on $_POST['orderDetails'] is valid (assuming it is the string you posted at the end of your question).
However you're invalidating it with
$orderDetails = mysqli_real_escape_string($conn, $_POST['orderDetails']);
You should decode it first with
$objs = json_decode($orderDetails, true);
and then escape each property into the foreach loop when building the SQL string:
foreach($objs as $item) {
$qualityName = mysqli_real_escape_string($conn, $item['qualityName'] );
$designName = mysqli_real_escape_string($conn, $item['designName'] );
/* ... */
$sql2 = "INSERT INTO tblOrderDetails (orderId, quality, design, shade, quantity, rate)
VALUES ('$orderId', $qualityName, $designName, ....... )";
mysqli_query($conn, $sql2);
}
Important note:
It is highly reccomended to use prepared statements instead of escaping data and then building queries by joining strings.
They're trivial to use, code is more readable, less error prone and not subject to SQL injection.

Related Links

PHP mailer change sender address using yahoo.com
Trim string to certain charchter in php
Fetch data from database from multiple tables one primary key
How to get the string until first occurrence of number [closed]
Laravel 4.2 Redirector Not Found
Export/import Joomla plugin settings
Error after upload the file
Use a string in PHP “as is”
Unable to SELECT ##IDENTITY after INSERT
utility::get_instance() should be compatible with & Singleton::get_instance()
ZF2 soap autodiscover not work with arrayofObjects
Delete polymorphic relation table automatically
post value through ajax of uploaded file in php
Resource ID #3 without using MySQL
Sort dropdown box in alphabetical order
Deploy updates for code igniter web application

Categories

HOME
qt
openshift
mod-rewrite
mc
xcode
freeradius
dotnetnuke
haproxy
command-prompt
plaid
odata
iverilog
docker-cloud
cryptography
bittorrent
getopenfilename
erd
jquery-file-upload
google-tasks-api
reselect
parceler
libgit2
connection-refused
fluent-migrator
ava
data-collection
echosign
iwebbrowser2
cucumber-junit
devtools
restful-architecture
express-session
amazon-kms
des
landscape
strapi
robust
morphline
sendinput
instafeedjs
researchkit
livereload
traversable
piping
yii2-model
node-glob
gravatar
enunciate
ui4j
variable-length-array
ruby-on-rails-4.1
mixins
green-threads
erlog
shell-extensions
debug-symbols
operands
rubber
cout
nesper
xcode6.3.1
redpitaya
armv6
pbkdf2
discovery
django-facebook
examine
xmlwriter
icenium
idispatch
uitextfielddelegate
data-dump
database-deadlocks
directshow.net
asp.net-mvc-views
servlet-container
resharper-5.1
spring-modules
rtti
objectinstantiation

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile