不想将值写入数据库

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?

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.

You need to:

  • Initalize a connection to your mysql database, using and address, username and password (in your case to netmap.inp.uz). Just as you do in HeidiSQL. Maybe this is done in your db.php file. If it is not, see http://php.net/manual/es/function.mysqli-connect.php
  • Instead of (or apart from) echoing your query, you must msqli_query($insert_sql), that is, have it executed by the database.

Right now, you are only sending the statement to the output.

This is a sample working code of your issue try this :

$test = array('www.google.com','msn.com','yahoo.com','stackoverflow.com','bhokat.com');

var_dump($test);
foreach ($test as $key => $value) {
    exec("ping -n 1 $value",$output[],$retval);
    if (strlen($output[$key][0]) > 0) {
        $sql = "UPDATE test SET status='1' WHERE ip_address=$value";
    } else {
        $sql = "UPDATE test SET status='1' WHERE ip_address=$value";
    }

    echo "<br/>" ;
}

Change update query as per your need. You need to update the field if already exist else insert, you can add extra check inside if and else part.