2016-02-04 33 views
0

我試圖讓一個簡單的下拉選擇表單來更改reg_id查詢來發送gcm消息。查詢選擇REG_ID從所有的頻道!=「0」,但不是「AND $和。額外的條件。似乎不夠好,但不是簡單的正常工作。$ count的回聲總是一樣的爲什麼我的表單選擇不會改變mysql查詢邏輯?

<html> 
<head> 
<title>Notifications</title> 
</head> 

<body> 
<form method="post"> 
<textarea type="text" name="msg" id="msg" rows="25" cols="80"></textarea> 
<input type="submit" value="send"/> 

<div class="col-md-2"> 
<select name="cat" class="form-control"> 
<option value="All">All</option> 
<option value="ranked">ranked</option> 
<option value="new">new</option> 
</select> 
</div> 
</form> 
</body> 
</html> 


<?php 
switch($_GET['cat']){ 
    case 'All': 
    $and='user_id!="2095"'; 
    break; 

    case 'ranked': 
    $and='salutes>"10"'; 
    break; 

    case 'new': 
    $and='created=CURDATE()'; 
    break; 

    default: 
    $and='user_id!="2095"'; 
} 

if(isset($_POST['msg'])){ 
$pdo=new PDO('mysql://hostname=localhost;dbname=database', 'user', 'password'); 

$user_ids=$pdo->query('select reg_id from accounts where channel!="0" AND '.$and.' AND user_id!="2095"'); 
$user_ids=$user_ids->fetchAll(PDO::FETCH_ASSOC); 
$ids=array(); 
foreach($user_ids as $row){ 
    $ids[]=$row['reg_id']; 
    $count++; 
} 
    // API access key from Google API's Console 
define('API_ACCESS_KEY',"***************"); 
$msg = array 
(
    'privateMessage' => "{$_POST['msg']}", 
    'messageText' => "{$_POST['msg']}", 
    'user_id'  => "2095", 
    'handle' => "Administration", 
    'rank' => "z" 
); 
$fields = array 
(
    'registration_ids' => $ids, 
    'data'   => $msg, 
    "time_to_live" => 3, 
    "priority" => "high", 
); 

$headers = array 
(
    'Authorization: key=' . API_ACCESS_KEY, 
    'Content-Type: application/json' 
); 

$ch = curl_init(); 
curl_setopt($ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send'); 
curl_setopt($ch,CURLOPT_POST, true); 
curl_setopt($ch,CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($fields)); 
$result = curl_exec($ch); 
curl_close($ch); 
echo $count; 
} 

?> 

回答

4

形式方法是POST,但是你的$ _GET檢查(即$_GET['cat']