英文:
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")+"&"+
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);
}
}
英文:
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);
}
}
答案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 "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();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论