Skip to content
Advertisement

Problem with inserting data AndroidStudio/PHP/Java

when I insert data to my database i get error like below: https://gyazo.com/501ee97212001fc7ffe65c1abc1048c4 “Notice: Undefined variable: POST in C:/wampp64/www/wspoladm.php on line 3” Values are added, expect the first one in lane 3. I declared this varriable in every script and I have no idea what’s wrong. Thanks for answer guys. Php script:

require "conn.php";
$czy_dane=$POST["czy_dane"]; -- line 3
$umowa = $_POST["umowa"];
$nazwa=$_POST["nazwa"];
$kategorie=$_POST["kategorie"];
$opis=$_POST["opis"];
$transfer=$_POST["transfer"];
$zabezpieczenia=$_POST["zabezpieczenia"];
$dane=$_POST["dane"];
$mysql_qry1 = "insert into wspoladm_panel(czy_dane,nr_umowy,nazwa,kategorie,opis,transfer,zabezpieczenia,dane)
values('$czy_dane','$umowa','$nazwa','$kategorie','$opis','$zabezpieczenia','$transfer','$dane')";
if($conn->query($mysql_qry1) === TRUE)  {
echo "Pomyslnie dodano dane";
}
else
{echo "Error: " . $mysql_qry1 . "<br" . $conn->error;
}
$conn->close();

BackgroundWorker.java:

if(type.equals("insert")){

        try {
            String czy_dane = params[1];
            String umowa = params[2];
            String nazwa = params[3];
            String kategorie = params[4];
            String opis = params[5];
            String transfer = params[6];
            String zabezpieczenia = params[7];
            String dane = params[8];

            URL url = new URL (insert_url);
            HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
            String post_data = URLEncoder.encode("czy_dane","UTF-8")+"="+URLEncoder.encode(czy_dane,"UTF-8")+"&"+
                    URLEncoder.encode("umowa","UTF-8")+"="+URLEncoder.encode(umowa,"UTF-8")+"&"
            +URLEncoder.encode("nazwa","UTF-8")+"="+URLEncoder.encode(nazwa,"UTF-8")+"&"
            +URLEncoder.encode("kategorie","UTF-8")+"="+URLEncoder.encode(kategorie,"UTF-8")+"&"
            +URLEncoder.encode("opis","UTF-8")+"="+URLEncoder.encode(opis,"UTF-8")+"&"
            +URLEncoder.encode("transfer","UTF-8")+"="+URLEncoder.encode(transfer,"UTF-8")+"&"
            +URLEncoder.encode("zabezpieczenia","UTF-8")+"="+URLEncoder.encode(zabezpieczenia,"UTF-8")+"&"
                    +URLEncoder.encode("dane","UTF-8")+"="+URLEncoder.encode(dane,"UTF-8");
            bufferedWriter.write(post_data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1")) ;
            String result="";
            String line="";
            while((line = bufferedReader.readLine())!=null) {
                result+= line;
            }
            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();
            return result;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

SecondPan.java:

public class SecondPan extends AppCompatActivity implements AdapterView.OnItemClickListener {
private Spinner sspnOption6, sspn2;

EditText umowa,nazwa,kategorie,opis,zabezpieczenia,dane;
String czy_dane, transfer;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second_pan);


    umowa = (EditText) findViewById(R.id.scnd_txt1);
    nazwa = (EditText) findViewById(R.id.scnd_txt2);
    kategorie = (EditText) findViewById(R.id.scnd_txt3);
    opis = (EditText) findViewById(R.id.scnd_tx4);
    zabezpieczenia = (EditText) findViewById(R.id.scnd_tx6);
    dane = (EditText) findViewById(R.id.scnd_tx7);


    sspnOption6 = findViewById(R.id.sspn);
    ArrayAdapter<CharSequence> FirstAdapter = new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.tab1));
    FirstAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    sspnOption6.setAdapter(FirstAdapter);


    sspn2=findViewById(R.id.sspn2);
    ArrayAdapter<CharSequence> Secondadapter = new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.transfer));
    Secondadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    sspn2.setAdapter(Secondadapter);
     czy_dane = sspn2.getSelectedItem().toString();

    transfer = sspnOption6.getSelectedItem().toString();


}
public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
    String text = parent.getItemAtPosition(position).toString();
    Toast.makeText(parent.getContext(), text, Toast.LENGTH_SHORT).show();


}



public void OnInsert(View view ) {
    String str_czy_dane = sspn2.getSelectedItem().toString();
    String str_umowa = umowa.getText().toString();
    String str_nazwa = nazwa.getText().toString();
    String str_kategorie = kategorie.getText().toString();
    String str_opis = opis.getText().toString();
    String str_transfer  = sspnOption6.getSelectedItem().toString();
    String str_zabezpieczenia = zabezpieczenia.getText().toString();
    String str_dane = dane.getText().toString();
    String type = "insert";
    BackgroundWorker backgroundWorker = new BackgroundWorker(this);
    backgroundWorker.execute(type, str_czy_dane,str_umowa, str_nazwa, str_kategorie, str_opis,str_transfer, str_zabezpieczenia, str_dane);
}

}

Advertisement

Answer

In your php code. Use $_POST instead of $POST.

Code

require "conn.php";
$czy_dane=$_POST["czy_dane"]; -- line 3
$umowa = $_POST["umowa"];
$nazwa=$_POST["nazwa"];
$kategorie=$_POST["kategorie"];
$opis=$_POST["opis"];
$transfer=$_POST["transfer"];
$zabezpieczenia=$_POST["zabezpieczenia"];
$dane=$_POST["dane"];
$mysql_qry1 = "insert into wspoladm_panel(czy_dane,nr_umowy,nazwa,kategorie,opis,transfer,zabezpieczenia,dane)
values('$czy_dane','$umowa','$nazwa','$kategorie','$opis','$zabezpieczenia','$transfer','$dane')";
if($conn->query($mysql_qry1) === TRUE)  {
echo "Pomyslnie dodano dane";
}
else
{echo "Error: " . $mysql_qry1 . "<br" . $conn->error;
}
$conn->close();
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement