【问题标题】:Check MySQL column contents inserting into based on if statement PHP根据if语句PHP检查插入的MySQL列内容
【发布时间】:2015-11-20 17:02:29
【问题描述】:
        $con = mysqli_connect($host,$uname,$opw,$odb);  //database login details
        // Check connection
        if (mysqli_connect_errno())                                             //if you cannot access the database
          {
          echo "Failed to connect to MySQL: " . mysqli_connect_error();         //useful error message
          }

        $result = mysqli_query($con,"SELECT * FROM PetLoc");

        if (!$result) { // add this check.
            die('Invalid query: ' . mysqli_error());
        }

        while($row = mysqli_fetch_assoc($result))
        {
             if($row['Location'] == 'inside')
             {
               $sql = ("INSERT INTO PetLoc (Location) VALUES ('outside')");
               sendText("outside");
             }
             else
             {
                $sql =("INSERT INTO PetLoc (Location) VALUES ('inside')");
               sendText("inside");
             }
        }

    function sendText($petLoc) {  
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "smsserver");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, true);
        $data = array(
             'mphone' => 'phoneno',
             'smstext' => 'Your pet walked through the door flap, it was last seen '.$petLoc.'.',
             'username' => 'uname'
        );
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $output = curl_exec($ch);
        curl_close($ch);
        echo "Output".$output ;

    }
mysqli_close($con);

此代码正在检查我的 sql 服务器中“位置”列的内容,如果内容是“内部”,则应该将其更改为“外部”,反之亦然。然后它将字符串“外部/内部”发送到 sendText 方法,该方法将利用文本消息中的响应。

问题是服务器内没有数据被更改,我没有收到连接错误,所以连接正常,但无法插入数据库。

在运行这个 PHP 文件时,除了 OutputReturnCode 1 之外,我没有得到任何反馈 - 但这是来自 curl 实现告诉我文本没有发送。

任何帮助表示赞赏, 干杯。

【问题讨论】:

    标签: php mysql curl


    【解决方案1】:

    问题是服务器内没有数据被更改,我没有收到连接错误,所以连接正常,但无法插入数据库。

    那是因为您的代码中没有 mysqli_query()

    解决方案

    while($row = mysqli_fetch_assoc($result))
    {
         if($row['Location'] == 'inside')
         {
           $sql = "INSERT INTO PetLoc (Location) VALUES ('outside')";
           mysqli_query($con, $sql);
           sendText("outside");
         }
         else
         {
            $sql =("INSERT INTO PetLoc (Location) VALUES ('inside')");
            mysqli_query($con, $sql);
           sendText("inside");
         }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-05
      • 1970-01-01
      • 2012-09-17
      • 2023-03-18
      • 2016-03-13
      相关资源
      最近更新 更多