Home ยป Php ยป I'd like to send list to php file and save list to mysqli

I'd like to send list to php file and save list to mysqli

Posted by: admin October 26, 2017 Leave a comment

Questions:

Hi i’m trying to send course list which has more than 2,000 course info to mysqli through php file. but ! whenever i try to send list, it doesn’t send it to server.
so can you help me to solve this problem..? ๐Ÿ™

First, java source

public static void sendCourseInfoToDB(List<Subject> subjects, String url) {

    try {
        // url is my *.php file 
        URL target = new URL(url);
        HttpURLConnection con = (HttpURLConnection) target.openConnection();

        con.setRequestMethod("POST");
        con.setDoOutput(true);
        con.setDoInput(true);
        con.setUseCaches(false);
        con.setRequestProperty("Content-Type", "text/html; charset = utf-8");

        DataOutputStream out = new DataOutputStream(con.getOutputStream());


        int len = subjects.size();

        for (int i = 0; i < len; ++i) {
            //String t = subjects.get(i).toString();
            out.writeBytes(subjects.get(i).toString());
            out.flush();
        }   

        out.flush();
        out.close();            

        int responseCode = con.getResponseCode();
        System.out.println("Post rqeust to Url : " + url);
        System.out.println("Post Params : " + subjects.get(0).toString());
        System.out.println("Resoponse Code : " + Integer.toString(responseCode));

        con.disconnect();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

Subject class overrides toString. return-statement used parameter is encoded UTF-8
like this :

courseCode = 12156&courseName = %EC%8B%A0%EC%86%8C%EC%9E%AC%EA%B3%B5%ED%95%99%EB%B6%80&subjectName = %EC%A2%85%ED%95%A9%EA%B3%BC%EC%A0%9C%EC%84%A4%EA%B3%841&kindOfSubject = %EC%A0%84%EA%B3%B5&score = 2

and php file

<?php

header("Content-Type : text/html; charset = utf-8");

$mysqli = new mysqli("localhost", "user", "password", "db");

if($mysqli->mysqli_errno) {
    print $mysqli_error;
    exit();
}

$courseCode = $_POST["courseCode"];
$courseName = $_POST["courseName"];
$subjectName = $_POST["subjectName"];
$kindOfSubject = $_POST["kindOfSubject"];
$score = $_POST["score"];

$mysqli->query("INSERT INTO COURSE VALUES('$courseCode', '$courseName', '$subjectName', '$kindOfSubject', '$score')");

$response = $courseCode;

echo $response;
?>

should i call ‘sendCourseInfoToDB function every time when i send course info to DB ? i dont know what is wrong.. help me crazy coding people~!~

Answers: