我想要做的是:我正在使用Android将数据插入到数据库中。但是它必须RETRIEVE/SEARCH为第一个表中的这个板号,但是在将数据插入到另一个表之前,它应该首先检查这个表中是否存在这个板号,如果是true,那么将数据插入到表中如果错误,则不要将数据插入表中。SQL仍然插入数据到数据库中,即使它是错误的
我遇到的问题是:即使我输入的板号是错误的,我在我的EditText中输入的数据仍将INSERT插入到数据库/表中。
我认为我的SQL语句有问题吗?
我在PHP代码:
<?php
$host='localhost';
$user='root';
$password='';
$db='employee101';
$PLATE_NUM = $_POST["PLATE_NUM"];
$PUV_TYPE = $_POST["PUV_TYPE"];
$content = $_POST["content"];
$sql = "select * from employee_data where PLATE_NUM like '$PLATE_NUM';";//first table where you retrieve the plate number first
$sql_insert = "insert into employee_content (PLATE_NUM, PUV_TYPE, content) values('$PLATE_NUM', '$PUV_TYPE', '$content')";//insert the table
$con = mysqli_connect($host,$user,$password,$db);
$result = mysqli_query($con, $sql);
$result2 = mysqli_query($con, $sql_insert);
$response = array();
if (mysqli_num_rows($result)> 0 && ($result2)=== TRUE){
echo"Log-In Success!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
}else{
echo "Log-In not success";
}
mysqli_close($con);
?>
我的Android Studio中的代码:
onButtonClick代码:
public void Button(View view) {
String puv_plate = report_plate.getText().toString();
String puv_type = report_type.getText().toString();
String content = report_content.getText().toString();
String type="report";
BackgroundWorker backgroundWorker = new BackgroundWorker(this);
backgroundWorker.execute(type, puv_plate,puv_type,content);
}
后台辅助类:
if(type.equals("report")){
try {
String id_ret = params[1];
String puv_type = params[2];
String report = params[3];
URL url = new URL(retrieve_id);
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("PLATE_NUM","UTF-8")+"="+URLEncoder.encode(id_ret,"UTF-8") + "&"
+ URLEncoder.encode("PUV_TYPE", "UTF-8") + "=" + URLEncoder.encode(puv_type, "UTF-8") + "&"
+ URLEncoder.encode("content", "UTF-8") + "=" + URLEncoder.encode(report, "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();
}
检查我更新的答案,很确定这是真正的问题 – Bqin1