[英]Can't insert data into mysql database with android
EditText tv_username;
EditText tv_firstname;
EditText tv_age;
Button reg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_username = (EditText) findViewById(R.id.username);
tv_firstname = (EditText) findViewById(R.id.firstname);
tv_age = (EditText) findViewById(R.id.age);
reg = (Button) findViewById(R.id.register);
reg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/regandroid.php");
if(httppost != null)
{
Context context = getApplicationContext();
CharSequence text = "Connected";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
try
{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username", tv_username.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("firstname", tv_firstname.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("age", tv_age.getText().toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
}
catch(Exception e)
{
e.printStackTrace();
}
This is my phpcode 这是我的phpcode
$username = $_POST['username'];
$firstname = $_POST['firstname'];
$age = $_POST['age'];
$query = mysql_query($connect, "insert into users
(username, firstname,age) values ('$username'
,'$firstname','$age') ");
?>
Maybe my php code have a problem I couldn't find what the problem is.This is my Php and java code I am unable to insert data although it's connected to the database. 也许我的php代码有问题,我找不到问题所在。这是我的php和java代码,尽管它已连接到数据库,但我无法插入数据。 I can't figure it out what mistake I have done.
我无法弄清楚我犯了什么错误。
Your parameters for mysql_query are in the wrong order. mysql_query的参数顺序错误。
mysql_query ( string $query [, resource $link_identifier = NULL ] )
mysql_query(字符串$ query [,资源$ link_identifier = NULL])
The link identifier / connection should be added via the second parameter, whereas the query should be added via the first parameter. 链接标识符/连接应通过第二个参数添加,而查询应通过第一个参数添加。
Change: 更改:
$query = mysql_query($connect, "insert into users(username, firstname,age) values ('$username', '$firstname', '$age')");
to 至
$query = mysql_query("insert into users(username, firstname,age) values ('$username', '$firstname', '$age')", $connect);
Also, your code is open to SQL injection as you're inserting external data directly into your query. 另外,在将外部数据直接插入查询中时,您的代码也可以进行SQL注入。 Use prepared statements with PDO or at least use mysql_real_escape_string if that is not an option.
如果不可以,请使用带有PDO的预处理语句,或者至少使用mysql_real_escape_string 。
The database link isn't really required. 数据库链接并不是真正需要的。 If passed however, it is only passed at the end of the mysql_query statement.
但是,如果通过,则仅在mysql_query语句的末尾传递。 ie:
即:
$query = mysql_query("insert into users (username, firstname, age) values ('$username', '$firstname', '$age')", $connect);
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.