0

I want to post this data in database through php file. Right now, Username,Company , Shares and Date are posted in database. I want to post "Edward", "AHCL", "10" and "23-04-2015"

public void Insert(View view){

          String UserName="Edward";

         //String Company = company.getText().toString();
         // Shares = shares.getText().toString();
         // String Date = date.getText().toString();

          String Company = "AHCL";
          String Shares= "10";
          String Date= "23-04-2015";


      try {
            // open a connection to the site
            URL url = new URL("http://10.0.2.2:8080//test/example.php");
            URLConnection con = url.openConnection();
            // activate the output
            con.setDoOutput(true);
            PrintStream ps = new PrintStream(con.getOutputStream());
            // send your parameters to your site
            ps.print("&Username=Username");
            ps.print("&Company=Company");               
            ps.print("&Shares=Shares");
            ps.print("&Date=Date");

            // we have to get the input stream in order to actually send the request
            con.getInputStream();

            // close the print stream
            ps.close();
            } catch (MalformedURLException e1) {
                e1.printStackTrace();
            } catch (IOException e2) {
                e2.printStackTrace();
            }







         // creating new product in background thread
         //new CreatePortfolio().execute();
     }
1

3 Answers 3

3

Try and change

 ps.print("&Username=Username");
    ps.print("&Company=Company");               
    ps.print("&Shares=Shares");
    ps.print("&Date=Date");

to

 ps.print("&Username=" + Username);
    ps.print("&Company=" + Company);               
    ps.print("&Shares=" + Shares);
    ps.print("&Date=" + Date);
Sign up to request clarification or add additional context in comments.

3 Comments

No problem, I always see these other guys on here over complicate things, I mainly do PHP work, so I go for easiest way, not the hardest most coded way. Glad it worked out for you..
I have another problem too. In my php file I am running this query. $result = mysql_query("SELECT Company,Shares,Date FROM portfolio WHERE Username='edward") or die(mysql_error()); Now instead of edward I want to pass a variable and retrieve rows when the username will be that variable value.
You should use mysqli instead of mysql, it's the newest form. But this might help you get started. $USER_NAME = $_REQUEST['username']; $Q = mysqli_query($conn, "SELECT * FROM portfolio WHERE Username = '$USER_NAME'"); if(mysqli_num_rows($Q) > 0) { //echo "Username already exists"; } else { do code } This box is making it hard to show all the correct coding, so if you can't make it out, post a link to a new question and I can put it all over there.
0

You should consider sending a GET or POST request to your PHP page. Check out how to send a POST request here: Sending HTTP POST Request In Java

Your PHP script can then get the value of your variables in the following using:

$_POST['Username']

2 Comments

I tried using that method but I was getting alot of errors through that method so I switched to this method. Isn't there any way to send variable data using this method?
You are overcomplicating your problem. Try running the Java HTTP POST request code in stackoverflow.com/questions/3324717/…
0

Building on the code to make it complete ie passing(post) data from JAVA to PHP ,fetching and displaying in both cases. JAVA side

      String UserName="Edward";
      String Company = "AHCL";
      String Shares= "10";
      String Date= "23-04-2015";

  try {
        // open a connection to the site
        URL url = new URL("http://10.0.2.2:8080//test/example.php");
        URLConnection con = url.openConnection();
        // activate the output
        con.setDoOutput(true);
        PrintStream ps = new PrintStream(con.getOutputStream());
        // send your parameters to your site
        ps.print("&Username=" + Username);
        ps.print("&Company=" + Company);               
        ps.print("&Shares=" + Shares);
        ps.print("&Date=" + Date);


        // we have to get the input stream in order to actually send the request
        con.getInputStream();
        // print out to confirm
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String line = null;
        while ((line = in.readLine()) != null) {
        System.out.println(line);
        // close the print stream
        ps.close();
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        } catch (IOException e2) {
            e2.printStackTrace();
        }







     // creating new product in background thread
     //new CreatePortfolio().execute();

PHP

<?php 
foreach ($_POST as $key => $value) {
    switch ($key) {
        case 'firstKey':
             $Username = $value;
            break;
        case 'secondKey':
            $Company = $value;
            break;
    case 'thirdKey':
              $Shares= $value;
            break;
        case 'fourthKey':
            $Date = $value; 
            
$file = fopen("data.txt","a"); 
$details=$Username."\t".$Company."\t".$Shares."\t".$Date;
fwrite($file,$details."\n");
fclose($file);
       break;
        default:
            break;
    }
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.