我有一个应用程序,作为启动活动,应该让用户登录在MySQL远程数据库通过PHP的方式(用户必须由管理员注册)。如果用户已正确登录,则执行另一个活动,并且在更改一些首选项后,应该启动一个服务并通过PHP不断地向该MySQL数据库发送数据。
我关心的是,因为这个数据应该链接到user_id作为外键,我怎么能确保这个数据被发送到正确的user_id?我想在表calls_data中插入某些数据,为此,我需要一个user_id字段。
我不确定,因为当用户登录时,我是这样做的:
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("username", un.getText().toString()));
postParameters.add(new BasicNameValuePair("password", pw.getText().toString()));
String response = null;
try {
response = CustomHttpClient.executeHttpPost("http://10.0.2.2/science/login.php", postParameters);
String res = response.toString();
res = res.replaceAll("''s+", "");
if (res.equals("1")) {
// We launch main activity to get the app running after successful login
Intent i = new Intent(getApplicationContext(), MtprojectActivity.class);
/* Set the request code to any code you like, you can
* identify the callback via this code
*/
startActivityForResult(i, REQUEST_CODE);
} else {
error.setText(res.toString());
}
} catch (Exception e) {
un.setText(e.toString());
}
但我不确定是否应该从DB中抓取user_id,以便在发送数据时将其传递给其他活动。
这是我的数据库表的结构:
CREATE TABLE IF NOT EXISTS `calls_data` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`date` varchar(100) DEFAULT NULL,
`duration` varchar(100) DEFAULT NULL,
`type` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(100) DEFAULT NULL,
`password` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
提前感谢您的帮助!
好的,我明白了:)我只是从MySQL服务器抓取username
并通过Intent
Extra
功能传递它,所以我的其他活动可以抓取该用户名,并基于此,将其发送到PHP服务器以检索user_id
!