我的PHP代码发送短信错误

时间:2012-06-11 07:51:38

标签: php mysql kannel

我是新手,我在我的代码的第26行收到错误我无法理解导致错误的原因,因为它使我无法发送短信任何帮助将不胜感激

        <?php
        $gw_host="10.0.0.9";
     $value=$_POST['value'];
     urlencode($message)=$_POST['message'];
     $con = mysql_connect("localhost","db_host","xxxxxx");

         if (!$con)
         {
         die('Could not connect: ' . mysql_error());
         }

         mysql_select_db("aic_sms", $con);

     $result = mysql_query("SELECT phn_number FROM users WHERE message=$value");
     if($result){
     while($row = mysql_fetch_array($result)) {
         function sendSmsMessage($phn_number, $message)
        {
            $ch= curl_init();
        curl_setopt($ch, "http://10.0.0.15/process_sms/sendsms.php?recipient=$phn_number&msg=" . urlencode($message));
        curl_exec($ch);
        curl_close($ch);

           }
       }
    else {
       echo mysql_error();
   }
   }

   echo 'Message sent successfully';    



      mysql_close($con);
      ?>

1 个答案:

答案 0 :(得分:0)

好的,首先你在一个循环中有一个函数,这将导致函数定义两次然后终止你的脚本,用你定义它们的函数然后使用函数名来执行函数内的代码,也就是实际的卷曲您正在使用的功能不能正常工作,因为您没有设置正确的选项。

另外,您应该使用准备好的查询来执行SQL语句,因为在当前脚本中您很容易被SQL注入,恶意的人可能会发布someval OR 1=1这会导致mysql将所有内容返回为1 = 1,这是真的。

继承人你应该怎么做,然后建立它。希望能帮助到你     

if($_SERVER['REQUEST_METHOD']=='POST' && !empty($_POST['value']) && !empty($_POST['message'])){

    $value  = $_POST['value'];
    $message= $_POST['message'];

    try {
        $dbh = new PDO("mysql:host=localhost;dbname=aic_sms", 'username', 'password');

        $stmt = $dbh->prepare("SELECT phn_number FROM users WHERE message=:message");

        /*** bind the paramaters ***/
        $stmt->bindParam(':message', $value, PDO::PARAM_STR);

        /*** execute the prepared statement ***/
        $stmt->execute();

        /*** fetch the results ***/
        while($row = $stmt->fetch())
        {
            $result[$row['phn_number']] = sendSmsMessage($row['phn_number'], $message);
        }
        //perhaps do somthing with the $result array
        echo 'Messages sent successfully';   
    }
    catch(PDOException $e)
    {
        die($e->getMessage());
    }
}else{
    //Show form or whatever
}


function sendSmsMessage($phn_number, $message){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, "http://10.0.0.15/process_sms/sendsms.php?recipient=$phn_number&msg=" . urlencode($message));
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 30);

    $html = curl_exec($curl);
    curl_close($curl);

    return $html;
}
?>