gpt4 book ai didi

php - Android:无法使用php pdo在mysql中插入数据

转载 作者:行者123 更新时间:2023-11-29 13:02:30 25 4
gpt4 key购买 nike

我是 Android 开发新手。我正在制作一个应用程序,用户需要从应用程序本身进行注册。我遇到的问题是记录插入一次后无法再次插入。换句话说,如果我从我的设备插入一次,我将无法插入记录。我需要等待至少 15 分钟才能插入另一条记录。我正在使用 PHP PDO 将记录插入 mysql 数据库。

这是 PHP 代码:

<?php
$nm = $_GET['nm'];
$email = $_GET['email'];
$password = $_GET['pass'];

$con = new PDO("mysql:host=hostname; dbname=database", "username", "passwoprd");
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

try {
$query = $con->prepare("INSERT INTO Entrepreneurs(entFullName, entEmailId, entPassword)
VALUES(:name, :email, :passWord)");
$query->bindParam(':name', $nm);
$query->bindParam(':email', $email);
$query->bindParam(':passWord', $password);
$query->execute();
}catch(PDOException $ex) {
echo "Some Exception Occured: <br />" . $ex->getMessage();
}
?>

这是java代码:

package com.example.entrepreneurexpress.entrepreneurs;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.entrepreneurexpress.R;

public class EntrepreneurRegister extends Activity {

String extracted_email, extracted_password, extracted_fullName;
EditText YourName, email, password;
ProgressDialog pDialog;

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.entrepreneur_register);

ActionBar aBar = getActionBar();
aBar.setDisplayHomeAsUpEnabled(true);

Button btnEntClear = (Button) findViewById(R.id.btnEntRegClear);
Button btnEntRegister = (Button) findViewById(R.id.btnEntRegRegister);

YourName = (EditText) findViewById(R.id.txtEntRegFullName);
email = (EditText) findViewById(R.id.txtEntRegEmailId);
password = (EditText) findViewById(R.id.txtEntRegPassword);

btnEntClear.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (YourName.length() >= 1) {
YourName.setText("");
}
if (email.length() >= 1) {
email.setText("");
}
if (password.length() >= 1) {
password.setText("");
}
}
});

btnEntRegister.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(YourName.length() < 1 || email.length() < 1 || password.length() < 1) {
Toast.makeText(getApplicationContext(), "Please Fill In All The Details", Toast.LENGTH_LONG).show();
} else {
new RegisterTask().execute("register");
}
}
});
}

class RegisterTask extends AsyncTask<String,Void,String>{

protected void onPreExecute(){
pDialog = new ProgressDialog(EntrepreneurRegister.this);
pDialog.setTitle("Processing..");
pDialog.setMessage("Registering Yourself With Us.......");
pDialog.setCancelable(true);
pDialog.setIndeterminate(true);
pDialog.show();
}

@Override
protected String doInBackground(String... params){
// TODO Auto-generated method stub
extracted_email = email.getText().toString();
extracted_fullName = YourName.getText().toString();
extracted_password = password.getText().toString();

String requesturl = "http://mehul.wink.ws/insertEntrepreneur.php?nm=" + extracted_fullName +
"&email=" + extracted_email + "&pass=" + extracted_password;

try {
HttpClient client = new DefaultHttpClient();
URI website;
website = new URI(requesturl);
HttpGet request = new HttpGet();
request.setURI(website);
client.execute(request);
} catch (IOException e) {
e.printStackTrace();
} catch(URISyntaxException e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(String result) {
if(pDialog != null) {
pDialog.dismiss();
}
Toast.makeText(getApplicationContext(), "Sucessfully Inserted !", Toast.LENGTH_LONG).show();
Intent i = new Intent(getApplicationContext(), WelcomeEntrepreneur.class);
startActivity(i);
}
}
}

更新

我开始知道错误是什么:

当我在 Android 应用程序中输入带有诸如 FirstName LastName 之类的空格的名称时,它不会被插入,但是当我在键入诸如 FirstNameLastName 之类的名称时不插入任何空格时,它被插入。

现在的问题是:如何解决上述错误?

请帮我解决这个问题。谢谢。

最佳答案

您需要对在 URL 字符串中传递的输入进行 URL 编码。改变这个

String requesturl = "http://mehul.wink.ws/insertEntrepreneur.php?nm=" + extracted_fullName + 
"&email=" + extracted_email + "&pass=" + extracted_password;

类似于

String requesturl = "http://mehul.wink.ws/insertEntrepreneur.php?nm=" + URLEncoder.encode(extracted_fullName= + 
"&email=" + URLEncoder.encode(extracted_email) + "&pass=" + URLEncoder.encode(extracted_password);

还可以考虑将 HTTP 动词从 GET 更改为 e.g.邮政。对于修改某些内容的请求,GET 在语义上是错误的。

关于php - Android:无法使用php pdo在mysql中插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23148605/

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