Save result of Array in Mysql

0

with help I have used the following code that allows me to enter data from an input and that it be separated into 3 different values that I need, which are value, code and serial

<?php
$datos = "3,5 1401 4145 7854 8454 7458 5152556555 5 1401 4145 7854 8454 7458 5152556555 3,5 1401 4145 7854 8454 7458 5152556555";
// divides por espacios y cada 7 elementos, los elementos de cada fila
$temp = array_chunk(explode(' ', $datos), 7);
$ar = array();

foreach($temp as $key => $v) {
    // optienes el 1º elemento valor
    $ar[$key]['valor'] = array_shift($v);
    // optienes el ultimo elemento, serial
    $ar[$key]['serial'] = array_pop($v);
    // lo que queda es el codigo, lo unes con espacios
    $ar[$key]['codigo'] = implode(' ', $v);
}


print_r($ar); // esto me da como resultado el Array

In the example offered very gently by @Xerif I have now found a new challenge and that is that I must generate the INSERT of this data generated in a database using MySQLi the table is called tarjetas with the following structure: id, valor, codigo, serial the id is autoincrement I have tried to do it in several ways I thought that I would only achieve it but I can not find a way to do it, can you please give me guidance on how I should do it?

I know that $ar[0][valor] would give me the value of the first array but I can not visualize how I should do the INSERT in my Database when 50 values plus their respective 50 codes and 50 serials are examples ..!

$sql = "INSERT INTO tarjetas (id, valor, codigo, serial)
VALUES null, '$valor', '$codigo', '$serial'";

Implementing the suggestion I have done the following:

 function entregar_pedido(){
    global $db;

    $id_pedido = ($_GET['id']);
    $user = ($_GET['user']);
    $lote_pedido = $_REQUEST['lote'];

    $datos = "3,5 1401 4145 7854 8454 7458 5152556555 5 1401 4145 7854 8454 7458 5152556555 3,5 1401 4145 7854 8454 7458 5152556555";
// divides por espacios y cada 7 elementos, los elementos de cada fila
$temp = array_chunk(explode(' ', $datos), 7);
$ar = array();

foreach($temp as $key => $v) {
    // optienes el 1º elemento monto
    $ar[$key]['monto'] = array_shift($v);
    // optienes el ultimo elemento, serial
    $ar[$key]['serial'] = array_pop($v);
    // lo que queda es el codigo, lo unes con espacios
    $ar[$key]['codigo'] = implode(' ', $v);

    $monto =   $ar[$key]['monto'];
    $codigo =  $ar[$key]['codigo'];
    $serial =  $ar[$key]['serial'];

    $sql = "INSERT INTO tarjetas (id, monto, codigo, serial, usuario, id_pedido)
        VALUES(null, '$monto', ' $codigo', '$serial', '$user', '$id_pedido')";    

}
mysqli_query($db, $sql);
$resultado_ingreso = mysqli_query($db, $sql) or mysqli_error($db);

if (mysqli_query($db, $sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $db->error;
}

}

But only saves the first data 3 times, something like that

3,5 1401 4145 7854 8454 7458 5152556555
3,5 1401 4145 7854 8454 7458 5152556555
3,5 1401 4145 7854 8454 7458 5152556555

I can not make you save the required data completely.

Possibly it is not the best practice but I managed to solve by making a modification in the structure of my database by placing a single key in the serial column and I left the code as follows:

function entregar_pedido(){
    global $db;

    $id_pedido = ($_GET['id']);
    $user = ($_GET['user']);
    $lote_pedido = $_REQUEST['lote'];

    $datos = "1 1111 1111 1111 1111 1111 1111122222 2 2222 2222 2222 2222 2222 22222233333 3 3333 3333 3333 3333 3333 333344444 4 4444 4444 4444 4444 4444 4444555555";
// divides por espacios y cada 7 elementos, los elementos de cada fila
$temp = array_chunk(explode(' ', $datos), 7);
$ar = array();

foreach($temp as $key => $v) {
    // optienes el 1º elemento monto
    $ar[$key]['monto'] = array_shift($v);
    // optienes el ultimo elemento, serial
    $ar[$key]['serial'] = array_pop($v);
    // lo que queda es el codigo, lo unes con espacios
    $ar[$key]['codigo'] = implode(' ', $v);

    $monto =   $ar[$key]['monto'];
    $codigo =  $ar[$key]['codigo'];
    $serial =  $ar[$key]['serial'];

    $sql = "INSERT INTO tarjetas (id, monto, codigo, serial, usuario, id_pedido)
        VALUES(null, '$monto', ' $codigo', '$serial', '$user', '$id_pedido')";   

        mysqli_query($db, $sql);
        $resultado_ingreso = mysqli_query($db, $sql) or mysqli_error($db);

}


if (mysqli_query($db, $sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $db->error;
}



    $_SESSION['msn_pedidos_entrega']  = "Se ha registrado un nuevo usuario de manera Exitosa.<br>";

    echo $user;
    echo $lote_pedido;



}

Logically I remain attentive to improvements that I can make to my code.

    
asked by Jose M Herrera V 21.09.2018 в 14:18
source

1 answer

1

For an insert query of several values, you can create it directly by adding each record separated by commas, as follows:

$sql = "INSERT INTO tarjetas (id, valor, codigo, serial)
VALUES (null, '$valor1', '$codigo1', '$serial1') ,
       (null, '$valor2', '$codigo2', '$serial2'),
       (null, '$valor3', '$codigo3', '$serial3'),
       (null, '$valor4', '$codigo4', '$serial4'),
       (null, '$valor5', '$codigo5', '$serial5'),
";

Therefore, you could build the instruction with a loop, something like this:

$sql = "INSERT INTO tarjetas (id, valor, codigo, serial) VALUES ";

foreach($registros as $registro) {
   $sql.= "(null, '".$registro['valor']."', '".$registro['codigo']." ', '".$registro['serial']."')";
};

$sql.=";";
    
answered by 21.09.2018 / 14:29
source