在Android Studio/PHP/Java中插入数据的问题。

huangapple go评论87阅读模式
英文:

Problem with inserting data AndroidStudio/PHP/Java

问题

当我向数据库插入数据时,我会得到以下错误:
https://gyazo.com/501ee97212001fc7ffe65c1abc1048c4
“注意:在C:/wampp64/www/wspoladm.php的第3行中未定义变量:$POST”
值已添加,但在第3行中的第一个未添加。
我在每个脚本中都声明了这个变量,我不知道是什么问题。谢谢大家的答复。
PHP脚本:

require "conn.php";
$czy_dane=$POST["czy_dane"]; -- 第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")+"&amp;"+
                    URLEncoder.encode("umowa","UTF-8")+"="+URLEncoder.encode(umowa,"UTF-8")+"&amp;"
            +URLEncoder.encode("nazwa","UTF-8")+"="+URLEncoder.encode(nazwa,"UTF-8")+"&amp;"
            +URLEncoder.encode("kategorie","UTF-8")+"="+URLEncoder.encode(kategorie,"UTF-8")+"&amp;"
            +URLEncoder.encode("opis","UTF-8")+"="+URLEncoder.encode(opis,"UTF-8")+"&amp;"
            +URLEncoder.encode("transfer","UTF-8")+"="+URLEncoder.encode(transfer,"UTF-8")+"&amp;"
            +URLEncoder.encode("zabezpieczenia","UTF-8")+"="+URLEncoder.encode(zabezpieczenia,"UTF-8")+"&amp;"
                    +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);
}

}

英文:

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 &quot;conn.php&quot;;
$czy_dane=$POST[&quot;czy_dane&quot;]; -- line 3
$umowa = $_POST[&quot;umowa&quot;];
$nazwa=$_POST[&quot;nazwa&quot;];
$kategorie=$_POST[&quot;kategorie&quot;];
$opis=$_POST[&quot;opis&quot;];
$transfer=$_POST[&quot;transfer&quot;];
$zabezpieczenia=$_POST[&quot;zabezpieczenia&quot;];
$dane=$_POST[&quot;dane&quot;];
$mysql_qry1 = &quot;insert into wspoladm_panel(czy_dane,nr_umowy,nazwa,kategorie,opis,transfer,zabezpieczenia,dane)
values(&#39;$czy_dane&#39;,&#39;$umowa&#39;,&#39;$nazwa&#39;,&#39;$kategorie&#39;,&#39;$opis&#39;,&#39;$zabezpieczenia&#39;,&#39;$transfer&#39;,&#39;$dane&#39;)&quot;;
if($conn-&gt;query($mysql_qry1) === TRUE)  {
echo &quot;Pomyslnie dodano dane&quot;;
}
else
{echo &quot;Error: &quot; . $mysql_qry1 . &quot;&lt;br&quot; . $conn-&gt;error;
}
$conn-&gt;close();

BackgroundWorker.java:

if(type.equals(&quot;insert&quot;)){

        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(&quot;POST&quot;);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,&quot;UTF-8&quot;));
            String post_data = URLEncoder.encode(&quot;czy_dane&quot;,&quot;UTF-8&quot;)+&quot;=&quot;+URLEncoder.encode(czy_dane,&quot;UTF-8&quot;)+&quot;&amp;&quot;+
                    URLEncoder.encode(&quot;umowa&quot;,&quot;UTF-8&quot;)+&quot;=&quot;+URLEncoder.encode(umowa,&quot;UTF-8&quot;)+&quot;&amp;&quot;
            +URLEncoder.encode(&quot;nazwa&quot;,&quot;UTF-8&quot;)+&quot;=&quot;+URLEncoder.encode(nazwa,&quot;UTF-8&quot;)+&quot;&amp;&quot;
            +URLEncoder.encode(&quot;kategorie&quot;,&quot;UTF-8&quot;)+&quot;=&quot;+URLEncoder.encode(kategorie,&quot;UTF-8&quot;)+&quot;&amp;&quot;
            +URLEncoder.encode(&quot;opis&quot;,&quot;UTF-8&quot;)+&quot;=&quot;+URLEncoder.encode(opis,&quot;UTF-8&quot;)+&quot;&amp;&quot;
            +URLEncoder.encode(&quot;transfer&quot;,&quot;UTF-8&quot;)+&quot;=&quot;+URLEncoder.encode(transfer,&quot;UTF-8&quot;)+&quot;&amp;&quot;
            +URLEncoder.encode(&quot;zabezpieczenia&quot;,&quot;UTF-8&quot;)+&quot;=&quot;+URLEncoder.encode(zabezpieczenia,&quot;UTF-8&quot;)+&quot;&amp;&quot;
                    +URLEncoder.encode(&quot;dane&quot;,&quot;UTF-8&quot;)+&quot;=&quot;+URLEncoder.encode(dane,&quot;UTF-8&quot;);
            bufferedWriter.write(post_data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,&quot;iso-8859-1&quot;)) ;
            String result=&quot;&quot;;
            String line=&quot;&quot;;
            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&lt;CharSequence&gt; FirstAdapter = new ArrayAdapter&lt;CharSequence&gt;(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&lt;CharSequence&gt; Secondadapter = new ArrayAdapter&lt;CharSequence&gt;(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&lt;?&gt; 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 = &quot;insert&quot;;
    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);
}

}

答案1

得分: 1

在你的 PHP 代码中。
使用 $_POST 替代 $POST

代码

require "conn.php";
$czy_dane = $_POST["czy_dane"]; --  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 "Pomyślnie dodano dane";
} else {
    echo "Błąd: " . $mysql_qry1 . "<br>" . $conn->error;
}
$conn->close();

请注意,我已经根据你的要求将代码翻译为中文。如果你需要任何进一步的帮助,请随时告诉我。

英文:

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

Code

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

huangapple
  • 本文由 发表于 2020年9月27日 15:17:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/64085879.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定