为什么数据没有从这些PDO语句中插入到表中(此表包含外键)?

时间:2018-04-14 19:39:41

标签: php android mysql pdo foreign-keys

我正在使用PHP / MYSQL在服务器中编写一个带有数据库的简单Android应用程序。以前我的注册表格数据会被插入到表格中。但是,当我从此表中创建与另一个表的关系时,问题就开始了,数据不再被插入。我使用了一些定制的排球库来进行网络连接,因此它不是问题。我无法找到确切的错误,但我确实知道php文件存在问题。我正在使用PDO,并且已使用SELECT方法将数据插入到外键列中。 Table 1 Structure Table 2 Strucutre   PHP CODE如下:

    <?php
require 'init.php';
error_reporting(E_ALL);

$category = $_POST['category']; 
$ownername = $_POST['ownername']; 
$ownerno = $_POST['ownerno']; 
$pricing=$_POST['pricing'];
$description=$_POST['description'];
$whatsappno=$_POST['whatsappno'];
$email=$_POST["email"];

$response = array();
$response["SUCCESS"] = 0;


//get database connection
$databse = new Database();

$con = $databse->getConnection();



try
  {

    $STH = $con->prepare("INSERT INTO Profile (Category_type, Full_name, Personal_no, Pricing, Description, Whatsapp_no, userid) value (?,?,?,?,?,?, (SELECT userid FROM users where email=$email)");
    $STH->bindParam(1,$category); 
    $STH->bindParam(2,$ownername); 
    $STH->bindParam(3,$ownerno);
    $STH->bindParam(4,$pricing);
    $STH->bindParam(5,$description);
    $STH->bindParam(6, $whatsappno);
    $STH->execute();
     if ($STH->rowCount() > 0) {

    $response["SUCCESS"]  = 1;
    $response["message"]="User profile is complete now !!";
     }else{
         $response["SUCCESS"]=2;
     }


}catch(PDOException $e){

    echo $e->getMessage();
}


echo json_encode($response);


?>

安卓代码在这里

public class ProfileComplete extends AppCompatActivity implements Urls {
    EditText Edit_OwnerName,Edit_OwnerNo,edit_Pricing,Edit_Business_Description,Edit_WhatsappNo;
    TextView Category;
    Button Submit;
    String category;
    String value;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile_complete);



        Category=  (TextView)findViewById(R.id.categorytype);
        Edit_OwnerName=(EditText)findViewById(R.id.editOwnername);
        Edit_OwnerNo=(EditText)findViewById(R.id.editOwnerNumber);
        edit_Pricing=(EditText)findViewById(R.id.editPricing);
        Edit_Business_Description=(EditText)findViewById(R.id.Description);
        Edit_WhatsappNo=(EditText)findViewById(R.id.editWhatsappNumber);
        Submit=(Button)findViewById(R.id.submitbutton);

        Spinner spinner = (Spinner) findViewById(R.id.service_cat_spinner);
        // Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.service_category_array, android.R.layout.simple_spinner_item);
        // Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
            {
                // An item was selected. You can retrieve the selected item using
                category=parent.getItemAtPosition(pos).toString();
                Category.setText(category);
            }
            public void onNothingSelected(AdapterView<?> parent)
            {

            }
        });

        Submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RegisterNow();
            }
        });

    }

    String ownername,pricing,businessdescription;
    String ownerno,whatsappno;
    private void setValues(){

        ownername= Edit_OwnerName.getText().toString().trim();
        ownerno=Edit_OwnerNo.getText().toString().trim();
        pricing=edit_Pricing.getText().toString().trim();
        businessdescription= Edit_Business_Description.getText().toString().trim();
        whatsappno=Edit_WhatsappNo.getText().toString().trim();



    }

    private boolean checkEmpty(){
        boolean ok = true;


        if (TextUtils.isEmpty(ownername)) {
            Edit_OwnerName.setError("Please enter your Full Name");
            Edit_OwnerName.requestFocus();
            return false;
        }

        if (TextUtils.isEmpty(ownerno)) {
            Edit_OwnerNo.setError("Please enter your personal number");
            Edit_OwnerNo.requestFocus();
            return false;
        }
        if (TextUtils.isEmpty(pricing)) {
            edit_Pricing.setError("Please enter your price range");
            edit_Pricing.requestFocus();
            return false;
        }

        if (TextUtils.isEmpty(businessdescription)) {
            Edit_Business_Description.setError("Please enter your Business Description");
            Edit_Business_Description.requestFocus();
            return false;
        }



        if (TextUtils.isEmpty(whatsappno)) {
            Edit_WhatsappNo.setError("Enter your whatsapp number");
            Edit_WhatsappNo.requestFocus();
            return false;
        }

        return ok;
    }


    private void RegisterNow(){
        setValues();
        if(checkEmpty()==false){return;}


        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            value = extras.getString("email");
        }



        Map<String, String> params = new HashMap<>();
        params.put("category", category);
        params.put("ownername", ownername);
        params.put("ownerno", ownerno);
        params.put("pricing",pricing );
        params.put("description",businessdescription );
        params.put("whatsappno",whatsappno );
        params.put("email",value);
        new Post_to_Server(this, params).getJson(URL_PROFILE, new HTTP_Post_Callback() {
            @Override
            public void onSuccess(String string) {
                try {
                    JSONObject job = new JSONObject(string);
                    int SUCCESS = job.getInt("SUCCESS");
                    if (SUCCESS == 1) {
                        Toast.makeText(ProfileComplete.this, "Profile completed !", Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
                    }
                }

                catch (JSONException ex){
                    Toast.makeText(ProfileComplete.this, "Error", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(VolleyError error) {

            }
        });


    }




}

1 个答案:

答案 0 :(得分:0)

您进行了这些更改

替换:

(SELECT userid FROM users where email=$email) 

(SELECT userid FROM users where email=?)

并将此行添加到$ STH行的末尾

$STH->bindParam(7, $email);