0

私のアプリでは、ユーザーが記入するための特定のフォームがあり、これらのデータをオンラインDBに保存したいと思います。

これがJavaの私のコードです:

public void send_data_to_DB(){
         String result = "";
         InputStream is = null;
         StringBuilder sb=null;
         ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();//() before
         nameValuePairs.add(new BasicNameValuePair("table", table));
         nameValuePairs.add(new BasicNameValuePair("code", Integer.toString(code)));
         nameValuePairs.add(new BasicNameValuePair("name", name));
         nameValuePairs.add(new BasicNameValuePair("email", email));
            try{
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://myurl.php");
                HttpEntity entity = new UrlEncodedFormEntity(nameValuePairs);
                httppost.addHeader(entity.getContentType());
                httppost.setEntity(entity);
                HttpResponse response = httpclient.execute(httppost);
                //HttpEntity entity = response.getEntity();
                //is = entity.getContent();
        }catch(Exception e){
                Log.e("log_tag", "Error in http connection "+e.toString());
        }
     }

そしてこれが私のphpスクリプトです:

<?php

    mysql_connect("dserver","User","Code");

    mysql_select_db("DB_Name");

$table.=$_POST['table'];
$code.=$_POST['code'];
$name.=$_POST['name'];
$email.=$_POST['email'];

  $q=mysql_query(" INSERT INTO {$table} (code,name,email) VALUES ({$code},{$name},{$email}) ")or die(mysql_error());


mysql_close();
?>

私はそれが私のphpと私が変数を割り当てたり使用したりする方法にあるに違いないと思いますが、私はPHPの経験があまりありません。手伝って頂けますか?

4

2 に答える 2

1

PHP 側では、.=代わりに=..

編集:

SQL ステートメントに引用符がありません:

への変更

"INSERT INTO {$table} (code,name,email) VALUES ('{$code}','{$name}','{$email}')"
于 2012-09-02T18:51:47.033 に答える
0
$table=.$_POST['table'];
$code=.$_POST['code'];
$name=.$_POST['name'];
$email=.$_POST['email'];

すべての等号の後に余分なドットがあるため、おそらく構文エラーが発生します。

あなたの他の問題は、SQL インジェクションの脆弱性です。クエリで使用する前に、/escape の適切な値を常に確認してください。

于 2012-09-02T18:44:27.130 に答える