简体   繁体   English

从数据库mysql中的函数php插入值

[英]Insert values from function php in database mysql

im testing insert al ip's from an array created with one function in php, in one database.. 即时测试从在一个数据库中用php中的一个函数创建的数组中插入al ip。

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.. 我需要从该数组中插入每个值,但我不知道如何。问题是在“ if”中插入它们的“ foreach”。

How can i solve this? 我该如何解决?

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;
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM