2016-10-08 9 views
0

私は、アンドロイドからphpに接続するのにvolleyを使用していますが、エラーが表示されています。ここに私の助けをしてください。& phpコード.phpはデータベースに接続していますphp show - 1 '成功' =>偽の結果。 「問題」によるとタイプjava.lang.stringはjsonarrayに変換できません

<?php 

$servername = "localhost"; 
$username = "****"; 
$password = "*****"; 
$dbname = "*****"; 


$em = $_POST['ya']; 
$one = $_POST['is']; 
$to = $_POST['to']; 


$d = date('Y-m-d'); 
$y = date('y'); 
$m = date('m'); 
$d = date('d'); 
$conn = mysqli_connect($servername, $username, $password, $dbname); 

if (!$conn) { 
    die("Connection failed: " . mysqli_connect_error()); 
} 
$sqll = "SELECT * FROM jio"; 
$res = mysqli_query($conn,$sqll); 
$rowC = mysqli_num_rows($res); 

$rowC = $rowC%999 + 1; 
if($rowC < 10){ 
    $i = $year.$mon.$day.'00'.$rowC; 
}elseif (rowC < 100) { 
    $i = $year.$mon.$day.'0'.$rowC; 
}else { 
    $i = $year.$mon.$day.$rowC; 
} 
$sql = "INSERT INTO jio(iu,i, d, ya, is, qs,to,ra,wto,wi,wk,)VALUES('0',".$i."','".$d."','".$em."','".$one."', '-1','".$to."','0','0','0','0')"; 

$r = mysqli_query($conn, $sql); 
$rows=mysqli_affected_rows($conn); 
$result = array(); 

if($rows>0) { 
    array_push($result,array(
      'success'=>true, 
      'i' => $i, 
      'd' =>$d 
    )); 
} 
else 
array_push($result,array(
     'success'=>false 
)); 

echo json_encode($result); 

mysqli_close($conn); 
?>       
+1

これは '$ rowC%999 + 1'とか誰も、PHPでarray_pushを使っている人は誰もいません....' $ result = [...]; } else {$ result = [...]; ' – ArtisticPhoenix

+0

構文エラーは5個以上必要です。 Java/Androidとphpの両方 そして、 'VALUES(' 0 '、 "。$ i。"'、 ' – Jeff

答えて

0

public class main extends Activity{ 

    String i = ""; 
    String d = ""; 
    String ya = ""; 
    String is = ""; 
    String to=""; 

    final Response.Listener<String> responseListener = new Response.Listener<String>() { 

    @Override 
    public void onResponse(String response) 
    { 
     JSONObject jsonResponse = null; 

     try { 
      JSONArray array = new JSONArray(response); 
      jsonResponse = array.getJSONObject(0); 
      Log.w(TAG, "onResponse: jsonresponse" + response.toString()); 
      boolean success = jsonResponse.getBoolean("success"); 
      if (success) { 
       i = jsonResponse.getString("i"); 
       d = jsonResponse.getString("d"); 
      } else { 
      } 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 

    Inp insQ = new Inp(ya,is ,to ,responseListener); 
    RequestQueue queue = Volley.newRequestQueue(main.this); 
    queue.add(insQ);} 


// next ins class - commented at edit by Jeff 
public class Ins extends StringRequest 
{ 
    public static final String REGISTER_REQUEST_URL = "edu.php"; 

    private static final String TAG = "Ins"; 
    private Map<String,String> params; 
    public Ins(String ya, String is, String to, Response.Listener listener) 
    { 
     super(Request.Method.POST, REGISTER_REQUEST_URL, listener, null); 
     Log.w(TAG, "Ins: " + ya + is + to); 
     params = new HashMap<>(); 
     params.put("ya", ya); 
     params.put("is",is); 
     params.put("to", to + ""); 
     Log.w(TAG, "Ins working well " + ya + is +to); 
    } 

    @Override 
    public Map<String,String> getParams() { 
     return params; 
    } 
} 

PHPコードの開始は、私はあなたのコード内のすべての構文エラーは、ここにコードを移植から来たと思います説明しました。

ようです - 私が正しくあなたを理解している場合 - あなたの唯一の問題は、あなたのSQLの簡単な行方不明'です:

                // here 
$sql = "INSERT INTO jio(iu,i, d, ya, is, qs,to,ra,wto,wi,wk,)VALUES('0',".$i."','".$d."','".$em."','".$one."', '-1','".$to."','0','0','0','0')"; 

これは、(理由はmysqliのエラーの)$rowsが偽であることを原因となりますので、あなたの成功した場合はfalseに設定します。

修正SQLはsql-injectionに開放しているので、あなたがより良い、prepared statementsに切り替える必要があります

$sql = "INSERT INTO jio(iu,i, d, ya, is, qs,to,ra,wto,wi,wk,)VALUES('0','".$i."','".$d."','".$em."','".$one."', '-1','".$to."','0','0','0','0')"; 
// this is the critical part: 
// ...,'".$i."',... 

NOTES
だろう。 また、クエリが成功したか、エラーがあったかどうかを最初に確認してください。

$r = mysqli_query($conn, $sql); 
if($r) { 
    // work with the result 
} else { 
    // an error occurred. Show it, handle it, whatever. 
    echo mysqli_error($conn); 
} 

また、array_pushはphpでは必要ありません。これは、その構文を使用する方がはるかに簡単です:

$result = array(); 
if($rows>0) { 
    $result[] = array(
     'success'=>true, 
     'i' => $i, 
     'd' =>$d 
     ); 
} 
else { // don't forget these brackets here! 
    $result[] = array(
     'success'=>false 
     ); 
} // and here 

そして最後に:あなたはそれがとにかく最後に終了するよう、スクリプトの最後にmysqliの接続をクローズする必要はありません。

関連する問題