gpt4 book ai didi

Android php 连接不工作

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

Android 代码

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.lpu.delight.lovelysweets.R;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;

public class AddNewItem extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_add_new_item);
EditText proname = (EditText)findViewById(R.id.adduprice);
EditText unitprice = (EditText) findViewById(R.id.addpnameet);

final String PName = proname.getText().toString();
final String UPrice = unitprice.getText().toString();
Button btninsert = (Button)findViewById(R.id.addbtninsert);

btninsert.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://10.0.2.2/insert.php");

try
{
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("pname",PName));
nameValuePairs.add(new BasicNameValuePair("uprice", UPrice));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httpPost);
Toast.makeText(getApplicationContext(),
"Inserted", Toast.LENGTH_LONG)
.show();

}
catch (Exception e)
{
e.printStackTrace();
}
}
});

}
}

插入.PHP 代码

<?php

$username = "root";
$password = "toor";
$hostname = "localhost";

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";


//select a database to work with
$selected = mysql_select_db("android_api",$dbhandle)
or die("Could not select examples");


//retrieve the data
$pnm = $_POST['pname'];
$pp = $_POST['uprice'];


mysql_query ("INSERT INTO tblproduct (pname,uprice) VALUES('$pnm', '$pp', NOW())");

mysql_close($dbhandle);

?>

无法工作,没有任何反应,请帮忙

当我运行时,它也没有打印 toast 消息。请帮我看看 php 文件中的变量是 pname 和 uprice 但它仍然没有执行 try block 有什么问题。

最佳答案

在您的查询中,INTO 中缺少一个参数

mysql_query ("INSERT INTO tblproduct (pname,uprice) VALUES('$pnm', '$pp')") or die(mysql_error());

;//值是3个参数,插入到有两列的表中

关于Android php 连接不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34837545/

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