【问题标题】:Input data from android to database in mysql将数据从android输入到mysql中的数据库
【发布时间】:2014-04-22 16:51:17
【问题描述】:

我有这个代码:

<?php include('konek.php');

$entid=$_POST['entId'];
$entSender=$_POST['entSender'];
$entTitle=$_POST['entTitle'];
$entDate=$_POST['entDate'];
$entSAGrade=$_POST['entSAGrade'];
$entReason=$_POST['entReason'];
$entProblem=$_POST['entProblem'];
$entTime=$_POST['entTime'];

if($row_num != 0)
{
$ins=mysql_query("INSERT into tblentry(entId,entSender,entTitle,entTime,entSAGrade,entReason,entProblem) VALUES('$entId','$entSender','$entTitle',CURTIME(),'$entSAGrade','$entReason','$‌​entProblem')");
echo "Added"; }
else 
{   echo "Added."; }
?>

我的问题是,当我在我的 android 模拟器中输入数据时,它没有添加。我的代码有什么问题?

这是我在 android 中的代码:

public class AddEntry extends Activity {

Button buttonSave;
EditText ent1, ent2, ent3, ent4, ent5, ent6 ;
HttpPost httppost;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
ProgressDialog dialog = null;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addentry);

    buttonSave = (Button)findViewById(R.id.Save);  
    ent1 = (EditText)findViewById(R.id.entSender);
    ent2 = (EditText)findViewById(R.id.entDate);
    ent3 = (EditText)findViewById(R.id.entTitle);
    ent4 = (EditText)findViewById(R.id.entSAGrade);
    ent5 = (EditText)findViewById(R.id.entReason);
    ent6 = (EditText)findViewById(R.id.entProblem);

    buttonSave.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog = ProgressDialog.show(AddEntry.this, "", 
                    "Adding entry. . .", true);
             new Thread(new Runnable() {
                    public void run() {
                        insertEntry();

                    }

                  }).start();               
        }
    });

}

void insertEntry(){
    try{            

        httpclient=new DefaultHttpClient();
        httppost= new HttpPost("http://10.0.2.2/smcfi/insertentry.php"); // make sure the url is correct.
        //add your data
        nameValuePairs = new ArrayList<NameValuePair>(6);
        // Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar, 
        nameValuePairs.add(new BasicNameValuePair("enSender",ent1.getText().toString().trim()));  // $Edittext_value = $_POST['Edittext_value'];
        nameValuePairs.add(new BasicNameValuePair("entDate",ent2.getText().toString().trim()));
        nameValuePairs.add(new BasicNameValuePair("entTitle",ent3.getText().toString().trim()));
        nameValuePairs.add(new BasicNameValuePair("entSAGrade",ent4.getText().toString().trim()));
        nameValuePairs.add(new BasicNameValuePair("entReason",ent5.getText().toString().trim()));
        nameValuePairs.add(new BasicNameValuePair("entProblem",ent6.getText().toString().trim()));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        //Execute HTTP Post Request
        response=httpclient.execute(httppost);
        // edited by James from coderzheaven.. <span id="IL_AD10" class="IL_AD">from here</span>....
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        final String response = httpclient.execute(httppost, responseHandler);
        System.out.println("Response : " + response); 
        runOnUiThread(new Runnable() {
            public void run() {
               // tv.setText("Response from PHP : " + response);
                dialog.dismiss();
            }
        });

        if(response.equalsIgnoreCase("Added")){
            runOnUiThread(new Runnable() {
                public void run() {
                    Toast.makeText(AddEntry.this,"Successfully Added.", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(AddEntry.this, Entry.class);
                    AddEntry.this.startActivity(intent);
                    AddEntry.this.finish();
                }
            });

        }else{
            showAlert();                
        }

    }catch(Exception e){
        dialog.dismiss();
        System.out.println("Exception : " + e.getMessage());
    }
}
public void showAlert(){
    AddEntry.this.runOnUiThread(new Runnable() {
        public void run() {
            AlertDialog.Builder builder = new AlertDialog.Builder(AddEntry.this);
            builder.setTitle("Error");
            builder.setMessage("Not Added.")  
                   .setCancelable(false)
                   .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                       }
                   });                     
            AlertDialog alert = builder.create();
            alert.show();
        }
    });
}
@Override
public void onBackPressed() {
    Intent intent = new Intent(AddEntry.this, MainAct.class);
    AddEntry.this.startActivity(intent);
    AddEntry.this.finish();
}

}

我不知道是什么错误。但它没有添加。我输入的数据没有添加到我的数据库中。

【问题讨论】:

  • 您的查询中有语法错误。应该是:$ins=mysql_query("INSERT into tblentry(entId,entSender,entTitle,entTime,entSAGrade,entReason,entProblem) VALUES('$entId','$entSender','$entTitle',CURTIME(),'$entSAGrade','$entReason','$entProblem')"); 另外,不推荐使用 mysql_*,请改用 mysqli :)。此外,在您的查询之后,您可以添加or die (mysql_error());,即使上面的错误应该归类为语法错误。

标签: php android mysql


【解决方案1】:

发现一个错字-

 $ins=mysql_query("INSERT into tblentry
     (entId,entSender,entTitle,entTime,entSAGrade,entReason,entProblem)
     VALUES
     ('$entId','$entSender','$entTitle',CURTIME(),'$entSAGrade','$entReason','$entProblem'));"
                                                                                    ^=== This should be inside round bracket


改成这个 -

$ins=mysql_query("INSERT into tblentry
                 (entId,entSender,entTitle,entTime,entSAGrade,entReason,entProblem)
                 VALUES
                 ('$entId','$entSender','$entTitle',CURTIME(),'$entSAGrade','$entReason','$entProblem')");

【讨论】:

    【解决方案2】:

    替换

    $ins=mysql_query("INSERT into tblentry(entId,entSender,entTitle,entTime,entSAGrade,entReason,entProblem)
    VALUES('$entId','$entSender','$entTitle',CURTIME(),'$entSAGrade','$entReason','$entProblem'));"
    

    $ins=mysql_query("INSERT into tblentry(entId,entSender,entTitle,entTime,entSAGrade,entReason,entProblem)
    VALUES('$entId','$entSender','$entTitle',CURTIME(),'$entSAGrade','$entReason','$entProblem')");
    

    PS:您使用的是已弃用的 mysql 扩展;此外,您可以接受 sql 注入。更好的代码是

    <?php include('konek.php');
    $connect = mysqli_connect("localhost","username","password","yourdb");
    $entid=addslashes($_POST['entId']);
    $entSender=addslashes($_POST['entSender']);
    $entTitle=addslashes($_POST['entTitle']);
    $entDate=addslashes($_POST['entDate']);
    $entSAGrade=addslashes($_POST['entSAGrade']);
    $entReason=addslashes($_POST['entReason']);
    $entProblem=addslashes($_POST['entProblem']);
    $entTime=addslashes($_POST['entTime']);
    
    if($row_num != 0)
    {
    $ins=mysqli_query($connect,"INSERT into tblentry(entId,entSender,entTitle,entTime,entSAGrade,entReason,entProblem)
    VALUES('$entId','$entSender','$entTitle',CURTIME(),'$entSAGrade','$entReason','$entProblem')");
    echo "Added"; }
    else 
    {   echo "Added."; }
    ?>
    

    【讨论】:

    • 解析错误:语法错误,第 16 行 C:\xampp\htdocs\smcfi\insertentry.php 中出现意外 T_ECHO
    • 我也打错了,现在用新代码试试 :)
    【解决方案3】:

    首先你应该测试你的 PHP 服务隔离,例如使用 Chrome 的 PostMan 扩展。 当您确保 PHP 正常工作后,下一步是将 Android 的代码连接到您的 Web 服务。

    请检查 PostMan 以获得良好的调试;)

    https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm?hl=en

    【讨论】:

      猜你喜欢
      • 2014-07-04
      • 1970-01-01
      • 1970-01-01
      • 2013-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-19
      • 1970-01-01
      相关资源
      最近更新 更多