0

Hello !

I'm trying to build a link between an applet in java and php. My applet in Java sends data to php. And then php writes data in the database. I use json object

my code in java :

public static void main(String[] args) {
    // TODO Auto-generated method stub
    JSONObject json = new JSONObject();
    json.put("json", "mkyong.com");
    //obj.put("age", new Integer(100));
    System.out.print(json);
    System.out.print("\n");
    System.out.print("\n");

    try {
    URL recup = new URL("http://localhost/json_good/recup_v2.php");
    java.net.HttpURLConnection connexion = (HttpURLConnection)recup.openConnection();
    connexion.setDoOutput(true); // Pour pouvoir envoyer des données
    connexion.setRequestMethod("POST");
    connexion.setRequestProperty("Content-type", "application/json");

    //envoi de la requête
    OutputStreamWriter writer = null;
    writer = new OutputStreamWriter(connexion.getOutputStream());
    //conversion en chaine
    //System.out.print(json.toJSONString());
    writer.write(json.toJSONString());
    writer.flush();
    writer.close();

    //#2 lecture de la réponse
    BufferedReader reader=null;
    reader = new BufferedReader(new InputStreamReader(connexion.getInputStream()));
    String ligne;

    while ((ligne = reader.readLine()) != null) {
        System.out.println(ligne);}

    } catch (Exception e) {
        System.out.println(" exception : " + e.getMessage());
    }


}    

My code in php to receive data :

include('config.php');
  mysql_connect('localhost', 'root', '');
  mysql_select_db('tangram');
  if(mysql_select_db('tangram'))
  echo 'OK';
  $json=file_get_contents("php://input");
  var_dump(json_decode($json));
  $raw_json = json_decode($json,true);
  echo $raw_json["json"];
  $var=$raw_json["json"];
  mysql_query('insert into tb_user(name, surname, email) values ("'.$var.'", "'.$var.'", "'.$var.'")');

Well the link between java and php works but when I add a request to write my data in my database it doesn't work. Why ? Help me please

Lucile

Edited by superlucile: mistake

3
Contributors
5
Replies
22
Views
2 Years
Discussion Span
Last Post by diafol
0

<Sidenote>

Don't use this php to insert data to your db. Not safe. Use PDO or mysqli and prepared statements. Example...

$stmt = $db->prepare("INSERT INTO tb_user (`name`, `surname`, `email`) VALUES (:var1, :var2, :var3)");
$stmt->execute(array(":var1"=>$var1, ":var2"=>$var2, ":var3"=>$var3));
0

if I write

$db=mysql_select_db('tangram');

with your code.
the result is :

{"json":"mkyong.com"}

OK<br />
<b>Fatal error</b>:  Call to a member function prepare() on a non-object in <b>C:\Users\lucile\Desktop\PROJET 2A\logiciels\serveruXAMPPdossier\htdocs\json_good\recup_v2.php</b> on line <b>8</b><br />

why ?

Thanks for your answers

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.