【问题标题】:Why data is not inserted into the table(this table contains Foreign Key) from these PDO statements?为什么这些 PDO 语句没有将数据插入到表中(该表包含外键)?
【发布时间】:2018-04-14 19:39:40
【问题描述】:

我正在使用 PHP/MYSQL 在服务器中编写一个带有数据库的简单 android 应用程序。以前我的注册表单的数据被插入到表中。但是,当我从这个表问题开始与另一个表建立关系时,不再插入数据。我使用了一些定制的 volley 库来进行网络连接,因此这不是问题。我找不到确切的错误,但我确实知道 php 文件有问题。我正在使用 PDO,并且已使用 SELECT 方法将数据插入到外键列中。 Table 1 StructureTable 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);


?>

安卓代码在这里

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) {

            }
        });


    }




}

【问题讨论】:

    标签: php android mysql pdo foreign-keys


    【解决方案1】:

    你做出这些改变

    替换:

    (SELECT userid FROM users where email=$email) 
    

    (SELECT userid FROM users where email=?)
    

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

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

    【讨论】:

    • 没用,PDO 语句确实让我很困惑
    • 您可以在查询中使用限制 0,1 (SELECT userid FROM users where email=?)
    • $STH = $con->prepare("INSERT INTO Profile (Category_type, Full_name, Personal_no, Pricing, Description, Whatsapp_no, userid) 值 (?,?,?,?,?,?, (SELECT userid FROM users where email=$email)");
    • ")" 丢失..在上述查询中
    猜你喜欢
    • 2016-04-30
    • 1970-01-01
    • 2016-04-25
    • 2023-03-16
    • 2021-01-10
    • 2016-08-21
    • 1970-01-01
    • 1970-01-01
    • 2012-05-31
    相关资源
    最近更新 更多