我正在编写一个将数据插入 MySQL 数据库的应用程序。我用 PHP 编写了 sql 查询,当我在 android 应用程序中输入值时,我在 log cat 中没有看到任何错误,但是这些值没有存储在数据库中。
这是PHP代码:
<?php
$connect = mysqli_connect("localhost","root","","easyassigndroid");
if(mysqli_connect_errno($connect))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
echo "success";
}
$username = isset($_POST['sPhone']) ? $_POST['sPhone'] : '';
$password = isset($_POST['sPassword']) ? $_POST['sPassword'] : '';
$query = mysqli_query($connect, "insert into users (sphone, spassword) values ('$username' ,'$password') ");
mysqli_close($connect);
?>
这是安卓部分:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.register_lecturer);
phonenum = (EditText) findViewById(R.id.id_phone);
password = (EditText) findViewById(R.id.id_password);
sPhone = phonenum.toString();
sPassword = password.toString();
registerLecturer=(Button)findViewById(R.id.lecturerregister);
registerLecturer.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://10.0.2.2/project/insert.php");
try
{
nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("sphone", sPhone));
nameValuePairs.add(new BasicNameValuePair("spassword", sPassword));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
}
我是 Android 和 PHP 编码的初学者。谁能告诉我代码中的问题出在哪里?我的项目工作需要这个。