Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: passing variables from java applet to php

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default passing variables from java applet to php

    I write a simple post from java to my php backend.
    After I run the script I am getting the output that I am passing in filename, filesize etc..


    int filesize=25;
             String filetype=".txt";
             String hash="sdfjksdfhljahe8wr897348957jsdfajlsdhfl48";
             String email="sangwan.ritesh@yahoo.in";
             String filename="songs.txt";
    String urlParameters = "filename="+filename+"&filesize="+filesize+"&filetype="+filetype+"&filehash="+hash+"&email="+email;    
    URL url = new URL("myurl");
    HttpURLConnection hp=(HttpURLConnection)url.openConnection();
    hp.setDoInput(true);
    hp.setDoOutput(true);
    hp.setInstanceFollowRedirects(false);
    hp.setRequestMethod("POST");
    hp.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
    hp.setRequestProperty("charset", "utf-8");
    hp.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
    hp.setUseCaches (false);
    DataOutputStream wr = new DataOutputStream(hp.getOutputStream ());
    wr.writeBytes(urlParameters);
    wr.flush();
    String line;
    BufferedReader reader = new BufferedReader(new InputStreamReader(hp.getInputStream()));
    line = reader.readLine();
     System.out.println(line);
    wr.close();
    reader.close();
    hp.disconnect();

    The problem is I want to insert these values into my mysql database running on server so I write the following php code.
     
    <?php
    include('dbc.php');
    $filename=$_POST['filename'];
    $filesize=$_POST['filesize'];
    $filetype=$_POST['filetype'];
    echo $POST['email'];
    $filehash=$_POST['filehash'];
    $email=$_POST['email'];
    $query = "INSERT INTO files ( `email`, `file_name`, `file_ext`, `file_size`, 'file_hash') VALUES ( '$email', '$filename', '$filetype', '12584', 'filehash')";
    $result=mysqli_query($dbc,$query);
    if(mysqli_affected_rows($dbc) == 1)
    {
      // DO Nothing
    }
     echo $_POST['filehash'];
    echo $_POST['email'];
    echo $_POST['filename'];
    echo $_POST['filesize'];
    echo $_POST['filetype'];
    ?>

    The data is being is posted and I am getting the same back in my java application but data is not being entered in mysql database. I have a table name files with the same columns described above.


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,235
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: passing variables from java applet to php

    Any reason you are trying to invoke a php script as opposed to using JDBC to use java to do database manipulations? You should check for errors using mysql_error function, and also note that you should use Mysqli, as the mysql_* functions are being deprecated.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: passing variables from java applet to php

    Quote Originally Posted by copeg View Post
    Any reason you are trying to invoke a php script as opposed to using JDBC to use java to do database manipulations? You should check for errors using mysql_error function, and also note that you should use Mysqli, as the mysql_* functions are being deprecated.
    I was not able to connect to mysql database using jdbc driver. I guess my web hosting provider doesn't allow me to connect remotely to my mysql database.

Similar Threads

  1. java applet to php server communication
    By sabertooth in forum Java Networking
    Replies: 7
    Last Post: April 12th, 2013, 01:11 PM
  2. passing parameters from Applet to JSP
    By haythem in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 4th, 2013, 01:06 AM
  3. Instance Variables with Applet Reflection
    By Azurit3 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 1st, 2012, 01:48 PM
  4. Help Passing Variables
    By jo15765 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: May 7th, 2012, 08:22 PM
  5. Passing variables with void methods
    By knightmetal in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 21st, 2012, 08:46 PM