2016-10-08 4 views
0

나는 volley를 사용하여 PHP를 안드로이드에서 연결하고 있지만 오류가 있으면 여기 좀 도와주세요. 내 Java & PHP 코드 .php가 데이터베이스에 연결되어 있습니다 PHP show - 1 'success'=> false 결과. 은 "문제"에 따르면타입 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 구문 오류가 있어야합니다. 자바/안드로이드와 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이 거짓 원인이 될 것이다 if 'success'를 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."',... 

참고
될 것이다. 또한 쿼리가 성공했는지 또는 오류가 있는지 먼저 확인하는 것이 좋습니다.

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

또한 PHP에서는 array_push이 필요하지 않습니다.

$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 - 연결을 종료 할 필요가 없습니다.

관련 문제