Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a basic form where I am storing (in the client side) the values in a json format inside a textarea. I then plan to store these values in mysql database. I am using json_decode to place the json object into a regular php array. But when I check print_r($personArray) there is nothing. Therefore everytime I submit the form nothing gets stored in mysql database because the php array is empty. How can I store the values in the text area in mysql database? Here is a LIVE DEMO

if(isset($_POST['submit'])) {
$data = $_POST['data'];
echo $data;
$personArray = json_decode($data, true);
print_r($personArray);


      foreach($personArray as $key => $value){

          $main_role1 = ($value['main1'] == "true") ? 1 : 0;
          $main_role2 = ($value['main2'] == "true") ? 1 : 0;
          $person_fname = $value['firstName'];
          $person_lname = $value['lastName'];
          $person_phone = $value['phone'];

          $query_init2 = "INSERT INTO person (main_role1, main_role2, first_name, last_name, person_phone) VALUES (:main_role1, :main_role2,:person_fname,:person_lname, :person_phone);";
          $query_prep2 = $db_con->prepare($query_init2);
          $insert_result2 = $query_prep2->execute(array(
            "main_role1" => $main_role1,
            "nmain_role2" => $main_role2,
            "person_fname" => $person_fname,
            "person_lname" => $person_lname,
            "person_phone" => $person_phone
          ));

        }
      }

HTML

<textarea name="data" rows='5' cols='60'>

JSON Object

    [
    {
        "firstName": "Danny",
        "lastName": "LaRusso",
        "ciscoID": "123",
        "academyID": "1",
        "email": "[email protected]",
        "phone": "(555) 121-2121",
        "fax": "(123) 123-4567",
        "contact_role": true,
        "netacadContact": true,
        "netacadStaff": false,
        "netacadSuccess": false,
        "instructor_role": false
    },
    {
        "firstName": "Sensei",
        "lastName": "Miyagi",
        "ciscoID": "456",
        "academyID": "1",
        "email": "[email protected]",
        "phone": "(555) 444-2222",
        "fax": "(123) 123-4567",
        "contact_role": false,
        "netacadContact": false,
        "netacadStaff": false,
        "netacadSuccess": false,
        "instructor_role": true
    }
]
share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

The json_decode is working fine. The problem is when you are binding values into your SQL Statement.

$insert_result2 = $query_prep2->execute(array(
    ":main_role1" => $main_role1,
    ":nmain_role2" => $main_role2,
    ":person_fname" => $person_fname,
    ":person_lname" => $person_lname,
    ":person_phone" => $person_phone
));

You should include the ':' in the keys of the array.

Also, at the end of you query, you do not need another ;.

$query_init2 = "INSERT INTO person (main_role1, main_role2, first_name, last_name, person_phone) VALUES (:main_role1, :main_role2, :person_fname, :person_lname, :person_phone)";
share|improve this answer
    
: does not change anything. The json_object is not returning anything when I call print_r($personArray); –  Code_Ed_Student Mar 1 at 18:14
    
The decoding works fine: ideone.com/12KIFu Is the $data echoing fine? –  Akshay2598 Mar 1 at 18:21
    
Yes, correct. But I doesn't work when I directly grab the value from the textarea $data = $_POST['data'] and do json_decode($data, true) –  Code_Ed_Student Mar 1 at 18:25
    
Hmm, what is $_POST['submit']? the is no name="submit" on the html of your DEMO. –  Akshay2598 Mar 1 at 18:32
    
Oops for got to give the button name='submit'. Now there is another error but showing for this foreach($personArray as $key => $value){ –  Code_Ed_Student Mar 1 at 18:38
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.