Android: send data to a user with foreign key in MySQL server
I have an application that as starting activity, should let the user log in in a MySQL remote DB by means of PHP (the users have to be registered by the admin). If the user has logged in properly, another activity is executed, and where after changing some preferences, a service should be started and continuously send data to this MySQL DB through PHP.
My concern is that as this data should be linked to the user_id as foreign key, how can I make sure this data is sent to the correct user_id? I want to insert certain data into the 开发者_高级运维table calls_data, and for that, I need a user_id field.
I am not sure because when a user logs in, this is what I do:
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());
}
But I am not sure if I should grab from the DB the user_id so I can pass it to the other activity when sending the data.
This is the structure of the tables in my DB:
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 ;
Thanks a lot for your help in advance!
Ok I got it :) I just grab from the MySQL server the username
and pass it through the Intent
Extra
feature so my other activity can grab that username, and based on this, send it to the PHP server to retrieve the user_id
!
精彩评论