代码之家  ›  专栏  ›  技术社区  ›  Banka

为什么不将这些PDO语句中的数据插入表(此表包含外键)?

  •  0
  • Banka  · 技术社区  · 6 年前

    我正在使用PHP/MYSQL编写一个简单的android应用程序,服务器上有数据库。之前,我的注册表数据被插入到表中。然而,当我从这个表开始与另一个表建立关系时,数据不再插入。我使用了一些定制的截击库来连接网络,因此这不是一个问题。我找不到确切的错误,但我确实知道php文件有问题。我使用PDO,并使用SELECT方法将数据插入外键列。 Table 1 Structure Table 2 Strucutre PHP代码如下所示:

        <?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);
    
    
    ?>
    

    android代码在这里

    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 回复  |  直到 6 年前
        1
  •  0
  •   Murad Shukurlu    6 年前

    你做了那些改变

    替换:

    (SELECT userid FROM users where email=$email) 
    

    (SELECT userid FROM users where email=?)
    

    并将这一行添加到$STH行的末尾

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