gpt4 book ai didi

php - 没有错误: Data is not inserting in PHP MySql from Android

转载 作者:行者123 更新时间:2023-11-29 10:44:53 26 4
gpt4 key购买 nike

数据没有从android插入MySql,代码中没有错误,我已经使用AsyncThread - doInBackground和onPostExecute定义了它们。它显示了我在Json对象 try catch 方法“抱歉再试一次”中定义的内容如果未插入数据则处于 else 条件。

InputStream is = null;
String res = null;
String line = null;
int code;

onClick代码

            AsyncTaskRunner runner = new AsyncTaskRunner();
runner.execute();

question = addque.getText().toString();
choice1 = addc1.getText().toString();
choice2 = addc2.getText().toString();
choice3 = addc3.getText().toString();
answer = addanswer.getText().toString();
Explanation = addexplan.getText().toString();

AsyncTaskRunner 类

 private class AsyncTaskRunner extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub

ArrayList<NameValuePair> nameValuePairs = new
ArrayList<NameValuePair>();

nameValuePairs.add(new BasicNameValuePair("question", question));
nameValuePairs.add(new BasicNameValuePair("choice1", choice1));
nameValuePairs.add(new BasicNameValuePair("choice2", choice2));
nameValuePairs.add(new BasicNameValuePair("choice3", choice3));
nameValuePairs.add(new BasicNameValuePair("answer", answer));
nameValuePairs.add(new BasicNameValuePair("Explanation",
Explanation));

try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
HttpPost("http://192.168.43.4/insert/insert.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("pass 1", "connection success ");
} catch (Exception e) {
Log.e("Fail 1", e.toString());

}

try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
res = sb.toString();
Log.e("pass 2", "connection success ");
} catch (Exception e) {
Log.e("Fail 2", e.toString());
}

return null;
}

@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);

try
{
JSONObject json_data;
json_data = new JSONObject(res);
code = json_data.getInt("code");

if(code==1)
{
Toast.makeText(getActivity(), "Inserted Successfully",
Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getActivity(), "Sorry, Try Again",
Toast.LENGTH_LONG).show();
}
}
catch(Exception e)
{
Log.e("Fail 3", e.toString());
}
}

}

PHP 代码

<?php
$host='localhost';
$uname='root';
$pwd='';
$db="quizDB";

$con = mysql_connect($host,$uname,$pwd) or die("connection failed");
mysql_select_db($db,$con) or die("db selection failed");

$question=$_REQUEST['question'];
$choice1=$_REQUEST['choice1'];
$choice2=$_REQUEST['choice2'];
$choice3=$_REQUEST['choice3'];
$answer=$_REQUEST['answer'];
$Explanation=$_REQUEST['Explanation'];

$flag['code']=0;

if($r=mysql_query("insert into quizquestion values('$question','$choice1',
$choice2, '$choice3', '$answer', '$Explanation') ",$con))
{
$flag['code']=1;
echo"hi";
}

print(json_encode($flag));
mysql_close($con);
?>

如果我错了,请纠正我。

最佳答案

首先获取要插入的所有字符串,然后调用AsyncTask。可能是这样解决的。

  question = addque.getText().toString();
choice1 = addc1.getText().toString();
choice2 = addc2.getText().toString();
choice3 = addc3.getText().toString();
answer = addanswer.getText().toString();
Explanation = addexplan.getText().toString();

AsyncTaskRunner runner = new AsyncTaskRunner();
runner.execute();

并更改这 2 行。将 UTF-8 传递给 UrlEncodeFormEntity

  UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairs, "UTF-8");
httppost.setEntity(formEntity);

已编辑

我认为您忘记将连接添加到 $choice2 上,并像这样更改查询。首先传递 fieldName,然后传递 Value。

$sql = "INSERT INTO MyGuests (fieldName1, fieldName2, fieldName3,fieldName4,fieldName5,fieldName6)
VALUES ('$question','$choice1','$choice2', '$choice3', '$answer', '$Explanation')";

if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
$flag['code']=1;
echo"hi";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

关于php - 没有错误: Data is not inserting in PHP MySql from Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44778482/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com