我正在开发一款Android应用,但我实际上是新手。我试图将一些数据发布到我的数据库,但我不知道我的代码出了什么问题。我正在使用齐射库来处理请求和后端的php / mysql。 我认为问题在于我的PHP代码,因为我正在尝试将我的数据保存到表格#34; social"在表用户中具有对uid的外键user_id引用。 我不知道我是否正确解释我的问题,但下面是我的代码。对不起英语不好并提前致谢
function.php
public function storeSocial( $itemName, $email, $password, $URL,$note ){
$em = $_SESSION['email'];
$query = mysql_query("Select * from users where email = '$em'");
$row = mysql_fetch_array($query);
if($row)
{
$id = $row['uid'];
}
else return false;
$res = mysql_query("INSERT INTO social ( user_id, ItemName, email, password, URL,note)
VALUES( '$id','$itemName', '$email', '$password', '$URL', '$note')" );
// check for successful store
if ($res) {
return true;
} else {
return false;
}
的index.php
<?php
session_start();
if (isset($_POST['tag']) && $_POST['tag'] != '') {
// get tag
$tag = $_POST['tag'];
// include db handler
require_once 'include/DB_Functions.php';
$db = new DB_Functions();
// response Array
$response = array("tag" => $tag, "error" => FALSE);
if ($tag == 'social')
{
// Request type is Saving data
$itemName = $_POST['ItemName'];
$URL = $_POST['URL'];
$email = $_POST['email'];
$password = $_POST['password'];
$note = $_POST['note'];
$data = $db ->storeSocial($itemName, $email, $password, $URL, $note);
// check for successful store
if ($data)
{
// user stored successfully
$response["error"] = FALSE;
echo json_encode($response);
}
else {
// user failed to store
$response["error"] = TRUE;
$response["error_msg"] = "Failed to store";
echo json_encode($response);
}
}
SocialMedia_Info.java
package com.appnucleus.passmanager;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ClipData;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import helper.SQLiteHandler;
import helper.SessionManager;
import volley.AppController;
import volley.Config_URL;
public class SocialMedia_Info extends Activity {
private static final String TAG = SocialMedia_Info.class.getSimpleName();
private Button btnSave;
private EditText inputURL;
private EditText inputEmail;
private EditText inputPassword;
private EditText inputNote;
private EditText inputItemName;
private ProgressDialog pDialog;
private SessionManager session;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_socialmedia);
inputEmail = (EditText) findViewById(R.id.et_email);
inputPassword = (EditText) findViewById(R.id.et_password);
btnSave = (Button) findViewById(R.id.btn_Save);
inputNote = (EditText) findViewById(R.id.et_note);
inputURL = (EditText) findViewById(R.id.et_URL);
inputItemName = (EditText) findViewById(R.id.ItemName);
// Progress dialog
pDialog = new ProgressDialog(this);
pDialog.setCancelable(false);
// Session manager
session = new SessionManager(getApplicationContext());
// Save Button Click event
btnSave.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String URL = inputURL.getText().toString();
String note = inputNote.getText().toString();
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
String ItemName = inputItemName.getText().toString();
if(!validateEmail(email))
{
inputEmail.setError("Invalid Email");
inputEmail.requestFocus();
}
else if(!validatePassword(password))
{
inputPassword.setError("Invalid Password");
inputPassword.requestFocus();
}
else if (( !ItemName.isEmpty() && !email.isEmpty() && !password.isEmpty() && !URL.isEmpty() && !note.isEmpty())
|| ( !ItemName.isEmpty() || !email.isEmpty() || !password.isEmpty() || !URL.isEmpty()))
{
saveData(ItemName,email,password, URL, note);
}
else
{
Toast.makeText(getApplicationContext(),
"Please enter your details!", Toast.LENGTH_LONG)
.show();
}
}
});
}
private boolean validateEmail(String vemail)
{
Pattern pattern;
Matcher matcher;
final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
pattern = Pattern.compile(EMAIL_PATTERN);
matcher = pattern.matcher(vemail);
return matcher.matches();
}
//at least one number
//at least one upper case
//at least one lowercase
//at least one special chars
//no white spaces
//at least 6 chars
private boolean validatePassword(String vpassword)
{
Pattern pattern;
Matcher matcher;
final String PASSWORD_PATTERN = "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\\S+$).{6,}$";
pattern = Pattern.compile(PASSWORD_PATTERN);
matcher = pattern.matcher(vpassword);
return matcher.matches();
}
private void saveData( final String ItemName, final String email, final String password, final String URL,
final String note) {
pDialog.setMessage("Saving Data ...");
showDialog();
StringRequest strReq = new StringRequest(Request.Method.POST,
Config_URL.URL_REGISTER, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Saving Response: " + response.toString());
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
if (!error) {
// Launch login activity
Intent intent = new Intent(
SocialMedia_Info.this,
Activity_SocialMedia.class);
startActivity(intent);
finish();
} else {
// Error occurred in registration. Get the error
// message
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
error.printStackTrace();
hideDialog();
}
}) {
@Override
protected Map<String, String> getParams() {
// Posting params to register url
Map<String, String> params = new HashMap<String, String>();
params.put("tag", "social");
params.put("ItemName", ItemName);
params.put("email", email);
params.put("URL", URL);
params.put("note", note);
params.put("password", password);
return params;
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq);
}
private void showDialog() {
if (!pDialog.isShowing())
pDialog.show();
}
private void hideDialog() {
if (pDialog.isShowing())
pDialog.dismiss();
}
}