2012-08-23 36 views
0

我有一個腳本可以進行2-3次數據庫查詢,2次插入和1次更新。 我遇到的問題是,當我通過轉到像www.domain.com/handler.php?a=1&b=2&c=3這樣的Web服務器頁面手動測試腳本時,我完全獲得了3個更新。但是當我使用這個函數時,它不起作用......更具體地說,INSERT INTO不起作用。我在數據庫中獲得了更新,但是沒有新的條目。使用MySQL INSERT的腳本在直接運行時起作用,但不能作爲函數

while (($data = fgetcsv(STDIN)) !== false){ 
    $url='http://domain.com'; 
    $date = date('Y-m-d'); 
    $type = trim($data[0]); 
    if ($type != ''){ 
     $send_date = $data[2]; 
     $email = $data[4]; 
     $status = $data[7]; 
     $error = $data[8]; 
     $bounce = $data[10]; 
     $ip = $data[14]; 
     $job_id = $data[19]; 
     $params = 'pmta_token='.$pmta_token.'&type='.$type.'&send_date='.$send_date.'&email='.$email.'&status='.$status.'&error='.$error.'&bounce='.$bounce.'&ip='.$ip.'&job_id='.$job_id; 
     $params= array('server'=>$_SERVER['SERVER_NAME'],'type'=>$data[0],'time'=>$data[1],'message'=>$data[8],'bouncecat'=>$data[10],'jobid'=>$data[19],'domip'=>$data[21]); 
     $line = $type.','.$email.','.$ip.','.$status.','.$error.','.$bounce.','.$job_id.','.date('c')."\n"; 

     curl_post_async($url, $params); 
    } 
} 
    function curl_post_async($url, $params){ 
     foreach($params as $key => $val){ 
      if (is_array($val)) $val = implode(',', $val); 
     $post_params[] = $key.'='.urlencode($val); 
    } 
    $post_string = implode('&', $post_params); 

    $parts=parse_url($url); 

    $fp = fsockopen($parts['host'], 
     isset($parts['port'])?$parts['port']:80, 
     $errno, $errstr, 30); 

    $out = "POST ".$parts['path']." HTTP/1.1\r\n"; 
    $out.= "Host: ".$parts['host']."\r\n"; 
    $out.= "Content-Type: application/x-www-form-urlencoded\r\n"; 
    $out.= "Content-Length: ".strlen($post_string)."\r\n"; 
    $out.= "Connection: Close\r\n\r\n"; 
    if (isset($post_string)) $out.= $post_string; 

    fwrite($fp, $out); 
    fclose($fp); 
} 

我一定錯過了一些東西!我使用這個函數而不是curl的原因是因爲curl需要連接並等待響應時間......這個腳本每分鐘處理約2000個請求。

這裏是handler.php

這是失敗的,而不是任何輸入到數據庫的一個,沒有錯誤,當我做手工,它就像一個魅力,但使用套接字失敗,ONLY在INSERT上,UPDATE通過套接字連接工作。?。?。?

mysql_connect('localhost','root',''); 
mysql_select_db('analyzer'); 
date_default_timezone_set('America/Los_Angeles'); 

if(isset($_POST['type'])){ 
    $domip = @explode('/',$_POST['domip']); 
    $server = @strtolower($_POST['server']); 
    $type = @$_POST['type']; 
    $time = @$_POST['time']; 
    $message = @$_POST['message']; 
    $bouncecat = @$_POST['bouncecat']; 
    $jobid = @$_POST['jobid']; 
    $domain = @$domip[0]; 
    $md5 = @$_POST['md5']; 
    $ip = @$domip[1]; 
    if(isset($_POST['ip'])){$ip=$_POST['ip'];} 
}else{ 
    $domip = @explode('/',$_REQUEST['domip']); 
    $server = @strtolower($_REQUEST['server']); 
    $type = @$_REQUEST['type']; 
    $time = @$_REQUEST['time']; 
    $message = @$_REQUEST['message']; 
    $bouncecat = @$_REQUEST['bouncecat']; 
    $jobid = @$_REQUEST['jobid']; 
    $domain = @$domip[0]; 
    $md5 = @$_REQUEST['md5']; 
    $ip = @$domip[1]; 
} 
$now=date('Y-m-d H:i'); 
@mysql_query("INSERT INTO `log` (`type`,`time`,`message`,`cat`,`jobid`,`domain`,`ip`,`server`) VALUES('$type','$time','$message','$bouncecat','$jobid','$domain','$ip','$server')"); 
if(strpos($message,'ALERT'!==false)){ 
    @mysql_query("INSERT INTO `alerts` (`type`,`time`,`message`,`level`,`value`,`page`) VALUES('error','$now','$message','5','$ip','apps/loganalyzer/incl/datatracker.php')"); 
} 
if($type=='d'){ 
    @mysql_query("UPDATE `$server` SET `delivered`=`delivered`+1 WHERE `time`='$now'"); 
}elseif(($type=='b')&&($bouncecat!='bad')){ 
    @mysql_query("UPDATE `$server` SET `errored`=`errored`+1 WHERE `time`='$now'"); 
}elseif(($type=='b')&&($bouncecat=='bad')){ 
    @mysql_query("UPDATE `$server` SET `bounced`=`bounced`+1 WHERE `time`='$now'"); 
}elseif($type=='c'){ 
    @mysql_query("UPDATE `$server` SET `unsubscribed`=`unsubscribed`+1 WHERE `time`='$now'"); 
} 
+2

請不要使用'mysql_ *'函數,它已被棄用(請參閱[*紅框*](http://php.net/manual/en/function.mysql-query.php))和易受攻擊到SQL注入。使用[* PDO *](http://php.net/manual/en/book.pdo.php)或[* MySQLi *](http://php.net/manual/en/book.mysqli.php) 。 – alfasin

+3

順便說一下,使用'@來扼殺錯誤 - 是不好的! – alfasin

+0

並嘗試在phpMyAdmin等工具中查詢。另外,你可以使用mysql_error()來找出MySQL認爲你的INSERT查詢錯誤:http://php.net/manual/en/function.mysql-error.php –

回答

0

雖然我不知道到底是什麼問題,我有2項建議供您進行調試:

  1. 功能之前取出@符號 - 這是你隱藏你的錯誤需要修復。
  2. 製作一個小腳本來測試系統中捲曲的函數,以便您可以真正看到INSERT查詢出了什麼問題。
相關問題