douyanxing6054 2015-08-28 06:13 采纳率: 0%
浏览 32

不想将值写入数据库

There is a script which pings all the ip addresses in the column "ip_address", I need to ping after reports he wrote in the column "status". I.e. If the computer is online, in the column "status" write the value 1, if the offline database set to 0. Example:

http://i.stack.imgur.com/BTiAT.jpg

He does not want to write this value. Here's my script:

<?php
include('db.php');
foreach($test as $value){
    exec("ping -c 4 " . $value->ip_address, $output, $result);
    if ($result == 0) {
        $insert_sql = "INSERT INTO test ('status')VALUES('1');";
     }else{ 
    $insert_sql = "INSERT INTO test ('status')VALUES('0');";
}
    echo "<br/>" . $insert_sql;
    }
?>

This is what happens :

http://i.stack.imgur.com/6vfyu.png

UPD:

<?php
include('db.php');
foreach($test as $value){
    exec("ping -c 4 " . $value->ip_address, $output, $result);
    if ($result == 0) {
        $insert_sql = "update test set status='1' where ip_addres = '".$value->ip_address."'";
        mysqli_query($insert_sql);
     }else{ 
        $insert_sql = "update test set status='0' where ip_addres = '".$value->ip_address."'";
        mysqli_query($insert_sql);
}
    }
?>

That's right?

  • 写回答

3条回答 默认 最新

  • drkjzk3359 2015-08-28 06:18
    关注

    You have to use update syntax:

    <?php
    include('db.php');
    foreach($test as $value){
        exec("ping -c 4 " . $value->ip_address, $output, $result);
        if ($result == 0) {
            $insert_sql = "update test set status='1' where ip_addres = '".$value->ip_address."'";
         }else{ 
            $insert_sql = "update test set status='0' where ip_addres = '".$value->ip_address."'";
    }
        echo "<br/>" . $insert_sql;
        }
    ?>
    

    And do not use single qoutes around column names. single quotes are only for character values. if you have to escape column names you have to use backticks.

    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算