Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

im testing insert al ip's from an array created with one function in php, in one database..

I have this code:

<?php

//Funcion IP
$range='192.168.3.0/24';

function rango($range){
    $parts = explode('/',$range);
    $exponent = 32-$parts[1].'-';
    $count = pow(2,$exponent);
    $start = ip2long($parts[0]);
    $end = $start+$count;
    return array_map('long2ip', range($start+1, $end-2) );
}



//Conexion a la Base de datos
$mysqli = new mysqli("localhost", "root", "", "ips");
if ($mysqli->connect_errno) {
    echo "Falló la conexión con MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

//Elimina la tabla si existe
if (!$mysqli->query("DROP TABLE IF EXISTS prueba3") ||
    //Crea la tabla
    !$mysqli->query("CREATE TABLE prueba3(ip VARCHAR(30), estado VARCHAR(30))") ||
    //Inserta valores en la tabla

    foreach((rango($range)) as $i)
    {
        !$mysqli->query("INSERT INTO prueba3 (ip, estado) VALUES (". $i .", 'Libre')");
    }

    /*!$mysqli->query("INSERT INTO prueba3(ip, estado) VALUES ('192.168.3.1', 'Libre')")) { */
    //Si falla la creacion de la tabla
    echo "Falló la creación de la tabla: (" . $mysqli->errno . ") " . $mysqli->error;
}
?>

I need insert each value from that array, and i dont know how.. The problem is the 'foreach' inside the 'if' to insert them..

How can i solve this?

share|improve this question
 
Im new programming..sorry :S –  Vyrtu Nov 13 '13 at 16:16
 
What are the conditions you want met for the script to do the insertion? From what I see in your script you're always dropping the table and creating it again I don't see what's the point to that! Plus you can't put foreach here, please explain further what do you want to check in this if statement –  AL-Kateb Nov 13 '13 at 16:20
add comment

1 Answer

up vote 0 down vote accepted

I still don't know why you're dropping the table each time but you can try this code, it does what you need BTW you were missing quotes around the value, it is a string so it does need to be enclosed within quotes

<?php

//Funcion IP
$range='192.168.3.0/24';

function rango($range){
    $parts = explode('/',$range);
    $exponent = 32-$parts[1].'-';
    $count = pow(2,$exponent);
    $start = ip2long($parts[0]);
    $end = $start+$count;
    return array_map('long2ip', range($start+1, $end-2) );
}



//Conexion a la Base de datos
$mysqli = new mysqli("localhost", "root", "MYSQL123", "ips");
if ($mysqli->connect_errno) {
    echo "Falló la conexión con MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

//Elimina la tabla si existe
if ($mysqli->query("DROP TABLE IF EXISTS prueba3") AND $mysqli->query("CREATE TABLE prueba3(ip VARCHAR(30), estado VARCHAR(30))")){
    //Inserta valores en la tabla

    foreach((rango($range)) as $i)
    {
      $query = $mysqli->query("INSERT INTO prueba3 (ip, estado) VALUES ('". $i ."', 'Libre')");
        if($query){
          echo "Inserted " . $i . "<br/>";
        }else{
          echo "error ...(" . $mysqli->errno . ") " . $mysqli->error . "<br />";
        }
    }

    /*!$mysqli->query("INSERT INTO prueba3(ip, estado) VALUES ('192.168.3.1', 'Libre')")) { */
    //Si falla la creacion de la tabla

}else{
  echo "Falló la creación de la tabla: (" . $mysqli->errno . ") " . $mysqli->error;
}
?>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.