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:
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 :
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:
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.