使用json字符串从android中插入多个复选框值到mysql数据库中

时间:2018-02-16 18:07:36

标签: php mysql json

我希望使用php将mutiple checkboxes中的值插入到mysql数据库中,但代码不起作用。计划是将复选框值发送为json string,并使用php在separate row上插入每个值。 勾选复选框并提交后,我有empty表。以下是我的代码。

<?php

         if (isset($_POST)){


        include("config.php");


        $name = $conn->real_escape_string($_POST['name']);

        $email = $conn->real_escape_string($_POST['email_address']);

        $id = $conn->real_escape_string($_POST['code']);

        $category = $_POST['speciality']; //this is the json string..example [{"a": "1", "b" : "2"}]

        $data = json_decode($category,true);

        $pass = $conn->real_escape_string($_POST['password']);

        $hashed_password =  password_hash($pass,PASSWORD_DEFAULT);

        if(($id === "hello") || ($id === "hiii")){


            foreach($data as $fields){
                foreach($fields as $key => $val){

                $query1 = "INSERT INTO category(email,cat_id) VALUES('$email', '$val')";
                $result1 = $conn->query($query1);
                }
            }
            if ( $result1) {
                    $query = "INSERT INTO friend(name,email,pass) VALUES('$name', '$email', '$hashed_password')";
                    $result = $conn->query($query);

                    if ( $result){

                        echo "success";
                    }
                    else {
                    echo "fail";
                    } 
            }    
        }
        else{

          echo "id_mismatch";
        }

     }



?>

0 个答案:

没有答案