ploppy Posted September 12, 2011 Share Posted September 12, 2011 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; Quote Link to comment https://forums.phpfreaks.com/topic/246957-pass-json-array-to-mysql-help/ Share on other sites More sharing options...
AyKay47 Posted September 12, 2011 Share Posted September 12, 2011 i bleieve that serialize will better suite your needs here.. creates a storable representation of a value Quote Link to comment https://forums.phpfreaks.com/topic/246957-pass-json-array-to-mysql-help/#findComment-1268298 Share on other sites More sharing options...
ploppy Posted September 12, 2011 Author Share Posted September 12, 2011 @AyKay47 I am a noob so could you perhaps get me started with how serialize() would apply to my code? many thanks Quote Link to comment https://forums.phpfreaks.com/topic/246957-pass-json-array-to-mysql-help/#findComment-1268308 Share on other sites More sharing options...
AyKay47 Posted September 12, 2011 Share Posted September 12, 2011 replace json_encode with serialize.. Quote Link to comment https://forums.phpfreaks.com/topic/246957-pass-json-array-to-mysql-help/#findComment-1268310 Share on other sites More sharing options...
ploppy Posted September 12, 2011 Author Share Posted September 12, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/246957-pass-json-array-to-mysql-help/#findComment-1268316 Share on other sites More sharing options...
AyKay47 Posted September 12, 2011 Share Posted September 12, 2011 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.. Quote Link to comment https://forums.phpfreaks.com/topic/246957-pass-json-array-to-mysql-help/#findComment-1268317 Share on other sites More sharing options...
Pikachu2000 Posted September 12, 2011 Share Posted September 12, 2011 Post the code that inserts the data, and also post the output of this code, when placed after the echo $c; in your code. echo '<pre>'; print_r($list); var_dump($c); echo '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/246957-pass-json-array-to-mysql-help/#findComment-1268329 Share on other sites More sharing options...
ploppy Posted September 12, 2011 Author Share Posted September 12, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/246957-pass-json-array-to-mysql-help/#findComment-1268339 Share on other sites More sharing options...
Pikachu2000 Posted September 12, 2011 Share Posted September 12, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/246957-pass-json-array-to-mysql-help/#findComment-1268345 Share on other sites More sharing options...
ploppy Posted September 12, 2011 Author Share Posted September 12, 2011 It is still inserting as 'ARRAY'. I did not understand your question. post the code from the point where that data is taken from the form, through the INSERT query are you referring to the ajax code and the db query? Quote Link to comment https://forums.phpfreaks.com/topic/246957-pass-json-array-to-mysql-help/#findComment-1268349 Share on other sites More sharing options...
ploppy Posted September 12, 2011 Author Share Posted September 12, 2011 also, I get this error when applying escape string to post $boxnumber = mysql_real_escape_string($_POST['BRVbrtrv_boxnumber']); <b>Warning</b>: mysql_real_escape_string() expects parameter 1 to be string, array given in <b> Quote Link to comment https://forums.phpfreaks.com/topic/246957-pass-json-array-to-mysql-help/#findComment-1268351 Share on other sites More sharing options...
AyKay47 Posted September 12, 2011 Share Posted September 12, 2011 where is $_POST['BRVbrtrv_boxnumber'] coming from..? it looks like its a multi_dim array.. Quote Link to comment https://forums.phpfreaks.com/topic/246957-pass-json-array-to-mysql-help/#findComment-1268369 Share on other sites More sharing options...
ploppy Posted September 12, 2011 Author Share Posted September 12, 2011 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>') } Quote Link to comment https://forums.phpfreaks.com/topic/246957-pass-json-array-to-mysql-help/#findComment-1268384 Share on other sites More sharing options...
AyKay47 Posted September 12, 2011 Share Posted September 12, 2011 yeah your POST value will return an array.. so you will need to use a foreach loop to loop through the separate values of that array.. Quote Link to comment https://forums.phpfreaks.com/topic/246957-pass-json-array-to-mysql-help/#findComment-1268385 Share on other sites More sharing options...
ploppy Posted September 12, 2011 Author Share Posted September 12, 2011 do I do that on the $_POST before the json array or after? thanks Quote Link to comment https://forums.phpfreaks.com/topic/246957-pass-json-array-to-mysql-help/#findComment-1268386 Share on other sites More sharing options...
AyKay47 Posted September 12, 2011 Share Posted September 12, 2011 before.. Quote Link to comment https://forums.phpfreaks.com/topic/246957-pass-json-array-to-mysql-help/#findComment-1268388 Share on other sites More sharing options...
Pikachu2000 Posted September 12, 2011 Share Posted September 12, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/246957-pass-json-array-to-mysql-help/#findComment-1268392 Share on other sites More sharing options...
AyKay47 Posted September 12, 2011 Share Posted September 12, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/246957-pass-json-array-to-mysql-help/#findComment-1268396 Share on other sites More sharing options...
ploppy Posted September 13, 2011 Author Share Posted September 13, 2011 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'); Quote Link to comment https://forums.phpfreaks.com/topic/246957-pass-json-array-to-mysql-help/#findComment-1268634 Share on other sites More sharing options...
Pikachu2000 Posted September 13, 2011 Share Posted September 13, 2011 You json_encode the data and assign it to $c, but I don't see $c anywhere in that insert query. In fact, I see the variable that does hold an array, $box, in there instead. Quote Link to comment https://forums.phpfreaks.com/topic/246957-pass-json-array-to-mysql-help/#findComment-1268701 Share on other sites More sharing options...
ploppy Posted September 14, 2011 Author Share Posted September 14, 2011 @pikachu That is because I am only using json to return the data to ajax. I have $_POST variables in the code that I use to insert into database. $box is an array when it is passed from ajax using jquery serialize function. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/246957-pass-json-array-to-mysql-help/#findComment-1269104 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.