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();
     }
jh314
  • 24,533
  • 14
  • 58
  • 79
  • See: [How to connect Android with PHP, MySQL](http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/) – kenorb May 19 '15 at 12:49

3 Answers3

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);
Jayce
  • 566
  • 3
  • 11
  • 31
  • 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.. – Jayce May 17 '15 at 06:51
  • 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. – Anjli Chhatwani May 17 '15 at 07:16
  • 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. – Jayce May 17 '15 at 07:21
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']
Community
  • 1
  • 1
WilliamSF
  • 284
  • 1
  • 8
  • 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? – Anjli Chhatwani May 16 '15 at 18:47
  • You are overcomplicating your problem. Try running the Java HTTP POST request code in http://stackoverflow.com/questions/3324717/sending-http-post-request-in-java – WilliamSF May 16 '15 at 18:51
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;
    }
}
?>