[英]Android Studio using PHP to insert data to MySQL workbench
I want to insert new data in my android application, the data will be sent to MYSQL workbench database.我想在我的 android 应用程序中插入新数据,数据将被发送到 MYSQL 工作台数据库。 But when I try to edit the new data and click save button, the data will not go into the database.
但是当我尝试编辑新数据并单击保存按钮时,数据不会进入数据库。 It got the input result when I try to print it out.
当我尝试打印它时,它得到了输入结果。 I using PHP file to let the application to connect to the database.
我使用 PHP 文件让应用程序连接到数据库。 Is there anything I miss for setting up the method?
设置方法有什么我想念的吗?
There are no error on my code and Logcat.我的代码和 Logcat 没有错误。 And I should those line when I click the save button:
当我单击保存按钮时,我应该使用这些行:
03-28 02:58:07.697 4985-5433/advprog.mmu.ac.uk.projectapp I/System.out: Test:customerName=test&carName=test&appointmentDate=tte&email=testes&issueDescribe=tstes&timeForJob=testes 03-28 02:58:07.697 4985-5433/advprog.mmu.ac.uk.projectapp I/System.out:测试:客户名称=测试&汽车名称=测试&appointmentDate=tte&email=testes&issueDescribe=tstes&timeForJob=testes
03-28 02:58:13.597 4985-5433/advprog.mmu.ac.uk.projectapp W/zygote: Verification of void com.android.tools.profiler.support.network.HttpTracker$InputStreamTracker.(java.io.InputStream, com.android.tools.profiler.support.network.HttpTracker$Connection) took 143.013ms 03-28 02:58:13.597 4985-5433/advprog.mmu.ac.uk.projectapp W/zygote:验证无效 com.android.tools.profiler.support.network.HttpTracker$InputStreamTracker.(java.io.InputStream , com.android.tools.profiler.support.network.HttpTracker$Connection) 耗时 143.013 毫秒
03-28 02:58:15.090 4985-5031/advprog.mmu.ac.uk.projectapp D/EGL_emulation: eglMakeCurrent: 0x9e4afd00: ver 3 0 (tinfo 0x9d53ea40) 03-28 02:58:15.090 4985-5031/advprog.mmu.ac.uk.projectapp D/EGL_emulation: eglMakeCurrent: 0x9e4afd00: ver 3 0 (tinfo 0x9d53ea40)
03-28 02:58:15.285 4985-5031/advprog.mmu.ac.uk.projectapp D/EGL_emulation: eglMakeCurrent: 0x9e4afd00: ver 3 0 (tinfo 0x9d53ea40) 03-28 02:58:15.285 4985-5031/advprog.mmu.ac.uk.projectapp D/EGL_emulation: eglMakeCurrent: 0x9e4afd00: ver 3 0 (tinfo 0x9d53ea40)
03-28 02:58:16.752 4985-4993/advprog.mmu.ac.uk.projectapp I/zygote: Do full code cache collection, code=244KB, data=163KB After code cache collection, code=244KB, data=133KB 03-28 02:58:16.752 4985-4993/advprog.mmu.ac.uk.projectapp I/zygote:做全码缓存收集,code=244KB,data=163KB,code缓存收集后,code=244KB,data=133KB
It get the value from the EditText but just doesn't pass to database.它从 EditText 获取值,但不传递到数据库。
The is the Java Code in the application:这是应用程序中的 Java 代码:
public class BackgroundWorker extends AsyncTask<String, Void, String> implements advprog.mmu.ac.uk.projectapp.BackgroundWorker {
Context context;
AlertDialog alertDialog;
BackgroundWorker (Context ctx) {
context = ctx;
}
protected String doInBackground(String... params) {
String newUrl = "http://10.0.2.2:8080/projectWeb/insert.php";
String customerName = params[0];
String carName = params[1];
String appointmentDate = params[2];
String email = params[3];
String issueDescribe = params[4];
String timeForJob = params[5];
try {
URL url = new URL(newUrl);
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 postData = URLEncoder.encode("customerName", "UTF-8") + "=" + URLEncoder.encode(customerName, "UTF-8") +"&"
+ URLEncoder.encode("carName", "UTF-8") + "=" + URLEncoder.encode(carName, "UTF-8") +"&"
+ URLEncoder.encode("appointmentDate", "UTF-8") + "=" + URLEncoder.encode(appointmentDate, "UTF-8") +"&"
+ URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(email, "UTF-8") +"&"
+ URLEncoder.encode("issueDescribe", "UTF-8") + "=" + URLEncoder.encode(issueDescribe, "UTF-8") +"&"
+ URLEncoder.encode("timeForJob", "UTF-8") + "=" + URLEncoder.encode(timeForJob, "UTF-8");
System.out.println("Test:" + postData);
bufferedWriter.write(postData);
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();
}
return null;
}
@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(context).create();
}
@Override
protected void onPostExecute(String result) {
alertDialog.setMessage(result);
alertDialog.show();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
PHP file: PHP文件:
<? php
$servername = "mudfoot.doc.stu.mmu.ac.uk:6306";
$username = "username";
$password = "password";
$dbname = "pangh";
$dbconn = mysqli_connect($servername, $username, $password, $dbname);
$cuName = $_POST["customerName"];
$caName = $_POST["carName"];
$appDate = $_POST["appointmentDate"];
$ema = $_POST["email"];
$isDescribe = $_POST["issueDescribe"];
$tiJob = $_POST["timeForJob"];
$sql = "INSERT INTO projectData(customerName, carName, appointmentDate, email, issueDescribe)
VALUES ('$cuName', '$caName', '$appDate', '$ema', '$isDescribe', '$tiJob')";
if($dbconn->query($sql) === TRUE) {
echo "Insert Successful";
} else {
echo "Error: " .$sql . "<br>" .$dbconn->error;
}
mysqli_close($dbconn);
?>
And this is the screenshot of my data base table:这是我的数据库表的屏幕截图:
You have an error in your mysql query.您的 mysql 查询出错。 You have only 5 columns in your fields, but you are passing 6 fields.
您的字段中只有 5 列,但您正在传递 6 个字段。
$sql = "INSERT INTO projectData(customerName, carName, appointmentDate, email, issueDescribe)
VALUES ('$cuName', '$caName', '$appDate', '$ema', '$isDescribe', '$tiJob')";
$tiJob is the extra field. $tiJob 是额外的字段。
check your code;检查您的代码; this line of insert:
这行插入:
$sql = "INSERT INTO projectData(customerName, carName, appointmentDate, email, issueDescribe)
VALUES ('$cuName', '$caName', '$appDate', '$ema', '$isDescribe', '$tiJob')";
have 5 column in the structure:结构中有 5 列:
customerName, carName, appointmentDate, email, issueDescribe
And you send 6 variable to populate the table.并且您发送 6 个变量来填充表。
'$cuName', '$caName', '$appDate', '$ema', '$isDescribe', '$tiJob'
i recommend you to migrate to PDO instead of use mysqli
我建议你迁移到 PDO 而不是使用
mysqli
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.