gpt4 book ai didi

php - 使用 PHP 从 android 插入数据库

转载 作者:行者123 更新时间:2023-11-29 12:04:27 27 4
gpt4 key购买 nike

我正在尝试使用 php 将一些数据从我的 android 应用程序插入到我的数据库中,我尝试了下面的脚本,但我得到了这个错误

Fail 3﹕ org.json.JSONException: End of input at character 0 of

PHP 脚本

<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";


// create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// check connection

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// insert values into table
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$message=$_REQUEST['message'];


$sql = "INSERT INTO user_message (`NM_id_mes`, `NM_mail`, `NM_content`, `NM_datetime`, `NM_name`)
VALUES (Null,$email,$message,NOW()+ INTERVAL 7 HOUR,$name)";

$flag['code']=0;

if ($conn->query($sql) === TRUE) {
//echo "New record created successfully";
$flag['code']=1;
} else {
$flag[code']=0;
}
echo json_encode($flag);
$conn->close();
?>

Android部分

@Override
protected Void doInBackground(Void... params) {
nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name", ContactUsFragment.name));
nameValuePairs.add(new BasicNameValuePair("email", ContactUsFragment.email));
nameValuePairs.add(new BasicNameValuePair("message", ContactUsFragment.message));

Log.d("name , email , message ",
ContactUsFragment.name+"--- "+ContactUsFragment.email+"--- "+ContactUsFragment.message );
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://example.com/Message.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();
result = sb.toString();
Log.e("pass 2", "connection success ");
} catch (Exception e) {
Log.e("Fail 2", e.toString());
}

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

if (code == 1) {
Log.d("Inserted Successfully", "");
} else {
Log.d("Sorry Try Again", "");
}
} catch (Exception e) {
Log.e("Fail 3", e.toString());
}

return null;
}

最佳答案

您的 PHP 脚本中有一个小错误

<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";


// create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// check connection

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// insert values into table
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$message=$_REQUEST['message'];


$sql = "INSERT INTO user_message (`NM_id_mes`, `NM_mail`, `NM_content`, `NM_datetime`, `NM_name`)
VALUES (Null,$email,$message,NOW()+ INTERVAL 7 HOUR,$name)";

$flag['code']=0;

if ($conn->query($sql) === TRUE) {
//echo "New record created successfully";
$flag['code']=1;
} else {
//$flag[code']=0; !!!!!!!!!! ERROR
$flag['code']=0;
}
echo json_encode($flag);
$conn->close();
?>

关于php - 使用 PHP 从 android 插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31780931/

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