Jump to content

pass json array to mysql help


ploppy

Recommended Posts

I have a simple ajax that passes data to a php script using jquery serialize. This is working good but where I am getting stuck is trying to pass json array into mysql. The json is outputting the correct data, but when I insert into db, it just says Array in the field. How can I convert(if that is the right term) json array to use in mysql. In the code I have posted, $box is an array from jquery which I normally use foreach to loop through array. What I need to do is convert $box after is has been encoded. Thanks

 

$box = $_POST['BRVbrtrv_boxnumber'];

$list = array("activity" => $activity, "mobile" => $mobile, "company" =>
$company, "authorised" => $authorised, "service" => $service, "department" =>
$department, "address" => $address, "boxcount" => $boxcount, "box" => $box);

$c = json_encode($list);

echo $c;

Link to comment
Share on other sites

this then displays

a:9:{s:8:"activity";s:13:"Box Retrieval";s:6:"mobile";s:21:"Submitted from mobile";s:7:"company";s:4:"DEMO";s:10:"authorised";s:4:"DEMO";s:7:"service";s:8:"standard";s:10:"department";s:4:"DEMO";s:7:"address";s:32:"28 mile end road  london e1 7yu";s:8:"boxcount";s:1:"2";s:9:"boxnumber";a:2:{i:0;s:3:"rrr";i:1;s:3:"eee";}}.

 

how do i take that data and use it to insert into db? Thanks

Link to comment
Share on other sites

this then displays

a:9:{s:8:"activity";s:13:"Box Retrieval";s:6:"mobile";s:21:"Submitted from mobile";s:7:"company";s:4:"DEMO";s:10:"authorised";s:4:"DEMO";s:7:"service";s:8:"standard";s:10:"department";s:4:"DEMO";s:7:"address";s:32:"28 mile end road  london e1 7yu";s:8:"boxcount";s:1:"2";s:9:"boxnumber";a:2:{i:0;s:3:"rrr";i:1;s:3:"eee";}}.

 

how do i take that data and use it to insert into db? Thanks

insert the data into your db how you would normally insert a string.. then when you are ready to call that data.. use unserialize to display the data in its proper format..

Link to comment
Share on other sites

Is this what you need? Thanks

 

<pre>Array
(
    [activity] => Box Retrieval
    [mobile] => Submitted from mobile
    [company] => DEMO
    [authorised] => DEMO
    [service] => standard
    [department] => DEMO
    [address] => 28 mile end road   london e1 7yu
    [boxcount] => 2
    [boxnumber] => Array
        (
            [0] => ttt
            [1] => fff
        )

)
string(323) "a:9:{s:8:"activity";s:13:"Box Retrieval";s:6:"mobile";s:21:"Submitted from mobile";s:7:"company";s:4:"DEMO";s:10:"authorised";s:4:"DEMO";s:7:"service";s:8:"standard";s:10:"department";s:4:"DEMO";s:7:"address";s:32:" mile end road   london e1 7yu";s:8:"boxcount";s:1:"2";s:9:"boxnumber";a:2:{i:0;s:3:"ttt";i:1;s:3:"fff";}}"
</pre>

Link to comment
Share on other sites

From the looks of it, you shouldn't have a problem inserting into a database. Escape it with mysql_real_escape_string(), as you would with any string data and make sure the field into which you're inserting it is of the proper type/size. If it's still a problem, post the code from the point where that data is taken from the form, through the INSERT query.

Link to comment
Share on other sites

I am using jquery to send the data through ajax.

 

for(var i = 0;i < $(this).val();i++) {
   $("#BRVbrtrv_boxnumber").append('<div data-role="fieldcontain"><label for="BRVbrtrv_boxnumber" class="ui-input-text">Enter box ' + (i + 1) + ' number:</label><input type="text" name="BRVbrtrv_boxnumber['+i+']" id="BRVbrtrv_boxnumber['+i+']" class="BRV_boxnumber ui-input-text ui-body-null ui-corner-all ui-shadow-inset ui-body-c" /></div>')
      }

Link to comment
Share on other sites

json_encode() handles a multidimensional array just fine, as demonstrated by the output the OP posted previously. The problem has to be in the insert. Post all of the relevant code.

I thought so too, however the OP's code is simple so im not sure why it is being inserted as an array still..

OP can you post the relevant insertion code please

Link to comment
Share on other sites

here you go. sorry for the delay. Thanks

 

$query = 'INSERT INTO `act` (`service`, `activity`, `department`, `company`,  `address`, `user`, `item`, `destroydate`, `date`, `notes`, `new`)
         VALUES (\''.$service.'\', \''.$activity.'\', \''.$department.'\', \''.$company.'\', \''.$address.'\', \''.$authorised.'\', \''.strtoupper($box).'\', NULL, NOW(), \''.$mobile.'\', \''.$new.'\');';
mysql_query($query) or die('Error, query failed');

Link to comment
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.