2017-04-13 77 views
0

你好,我想檢查是否phoneNumber的在數據庫中存在,然後返回一個響應,但我越來越陣列蜇皈依警告PHP數組字符串皈依警告在循環中

Javascript代碼:

var c = [ 
    { 
     "displayName" : "Nozha", 
     "phoneNumbers": ["97925955"] 
    }, 
    { 
     "displayName": "Maher", 
     "phoneNumbers": ["97925955"] 
    }] 
    checkUser(c) 


    function checkUser(data){ 
     //hne 3ayet lel service php mte3ek w na7i return false 
     $.ajax({ 
     url : "https://nozha.000webhostapp.com/verifecontact.php", 
     type : "POST", 
     data : {"data":data},    
      success:function(data) {           
       console.log(data); 
      } 
     }); 
    } 

PHP代碼:

<?php 
$S = $_POST['data']; 
for($i=0; $i<COUNT($S);$i++){ 
    $result=mysqli_query($con,"SELECT * from user where tel='$S[$i]['phoneNumbers']'"); 
    if(mysqli_num_rows($result)>0){ 
    $S[$i]['success'] = true; 
    }else{ 
    $S[$i]['success'] = false; 
    } 
} 
echo json_encode($S); 
?> 

和這裏就是我得到的迴應現在它似乎我不是數組中訪問電話號碼:

<br /> 
<b>Notice</b>: Array to string conversion in 
<b>/storage/h2/007/664007/public_html/verifecontact.php</b> on line <b>9</b> 
<br /> 
<br /> 
<b>Warning</b>: mysqli_num_rows() expects parameter 1 to be mysqli_result, 
boolean given in <b>/storage/h2/007/664007/public_html/verifecontact.php</b> on line <b>10</b><br /> 
<br /> 
<b>Notice</b>: Array to string conversion in 
<b>/storage/h2/007/664007/public_html/verifecontact.php</b> on line <b>9</b> 
<br /> 
<br /> 
<b>Warning</b>: mysqli_num_rows() expects parameter 1 to be mysqli_result, 
boolean given in <b>/storage/h2/007/664007/public_html/verifecontact.php</b> 
on line <b>10</b><br /> 
[{"displayName":"Nozha","phoneNumbers":["97925955"],"success":false}, 
{"displayName":"Maher","phoneNumbers":["97925955"],"success":false}] 
+1

[小博](http://bobby-tables.com/)說***腳本是在對SQL注入攻擊的風險。( http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)***瞭解[製備](http://en.wikipedia.org/wiki/Prepared_statement )[MySQLi]的聲明(http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)。即使[轉義字符串](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string)是不安全的! –

+1

可能有用,知道在哪裏_並看到完整的錯誤消息_ – RiggsFolly

+0

你有什麼警告/錯誤? – borracciaBlu

回答

-1

你可以試試這個,

<?php 
$result = '[ 
    { 
     "displayName" : "Nozha", 
     "phoneNumbers": ["97925955"] 
    }, 
    { 
     "displayName": "Maher", 
     "phoneNumbers": ["97925955"] 
    }]'; 

$result = json_decode($result, true); 

for($i=0; $i < count($result); $i++){ 
    if(count($result) > 0){ 
     $result[$i]['success'] = true; 
    }else{ 
     $result[$i]['success'] = false; 
    } 
} 

$jsonResult = json_encode($result); 

print_r($jsonResult); 

?> 
+0

或不。沒有「嘗試」。一個好的答案***將總是解釋所做的事情以及爲什麼這樣做,不僅是爲了OP,還是爲了將來訪問SO。 –

+2

@JayBlanchard:'做或不做。沒有「嘗試」這個報價已被所有padawan批准!但是'900歲的時候,你會達到......'(對於這個毫不相干的評論感到抱歉,但是captain明顯的不能錯過這個... ^^)編輯:如果它困擾人們,我會刪除 – OldPadawan