I want to enter each element of an array as a record of each mysql column

0

What I want is to enter each element of an array in a field of my database this is my structure example table record has four fields value1, value2 value3, value4 I want to enter the following records that are generated from the following code

arraynew = array("5878-1","8978-12","2523-1");
$dato1 = "00320555555";
$dato2 = "22/02/2018";
$dato3 = "Maria Mercedes del Barrio";
$dato4 = $arraynew;

$data = array($dato1,$dato2,$dato3,$dato4);

$nuevo = array();
for($i= 0; $i < count($dato4); $i++ ){
    array_push($nuevo, array(
        $data[0],
        $data[1],
        $data[2],
        $data[3][$i]
    ));
}
echo print_r($nuevo);
echo var_dump($nuevo);

this is the structure of the array

Array ( [0] => Array ( [0] => 00320555555 [1] => 22/02/2018 [2] => Maria Mercedes del Barrio [3] => 5878-1 ) [1] => Array ( [0] => 00320555555 [1] => 22/02/2018 [2] => Maria Mercedes del Barrio [3] => 8978-12 ) [2] => Array ( [0] => 00320555555 [1] => 22/02/2018 [2] => Maria Mercedes del Barrio [3] => 2523-1 ) ) 1
array (size=3)
  0 => 
    array (size=4)
      0 => string '00320555555' (length=11)
      1 => string '22/02/2018' (length=10)
      2 => string 'Maria Mercedes del Barrio' (length=25)
      3 => string '5878-1' (length=6)
  1 => 
    array (size=4)
      0 => string '00320555555' (length=11)
      1 => string '22/02/2018' (length=10)
      2 => string 'Maria Mercedes del Barrio' (length=25)
      3 => string '8978-12' (length=7)
  2 => 
    array (size=4)
      0 => string '00320555555' (length=11)
      1 => string '22/02/2018' (length=10)
      2 => string 'Maria Mercedes del Barrio' (length=25)
      3 => string '2523-1' (length=6)

What I need is to enter each array enter it in a row with its corresponding field

    
asked by Carlos Rene Montano Arias 22.02.2018 в 22:20
source

1 answer

2

The general logic of database management in php is

  • Connect to the Database
  • Prepare the query we will do
  • Execute the query with the parameters we want
  • Do fetch in the cases we need
  • Let's see the examples, in which I'll skip handling exceptions to simplify but you have to be aware that you have to handle them

    Using PDO would be the following

    $conn = new pdo("mysql:dbname=basededatos;host=localhost", "usuario", "contraseña");
    $query = $conn->prepare(
    "INSERT INTO registro(valor1, valor2, valor3, valor4) 
    VALUES(:valor1, :valor2, :valor3, :valor4);");
    foreach($arraydearrays as $filas){
        foreach($filas as $insertar){
            $params = array(
                ':valor1' => $insertar[0]
                , ':valor2' => $insertar[1]
                , ':valor3' =>$insertar[2]
                , ':valor4' => $insertar[3]
            );
            $query->execute($params);
            // no es necesario un fetch porque los insert no retornan filas
        }
    }
    

    Using mysqli would be very similar But the binding is positional, so the first bind that you do is joined with the first '?' which is in your sql and so it goes.

    you can do the -> bind_param more elegantly

    $conn = new mysqli('localhost', 'usuario', 'contraseña', 'basededatos');
    
    $query = mysqli->prepare("INSERT INTO registro(valor1, valor2, valor3, valor4) 
        VALUES(?, ?, ?, ?);");
    
    $valor=array();
    $query->bind_param('tipodedatodevalor1', $valor[0]);
    $query->bind_param('tipodedatodevalor2', $valor[1]);
    $query->bind_param('tipodedatodevalor2', $valor[2]);
    $query->bind_param('tipodedatodevalor2', $valor[3]); 
    
    foreach($arraydearrays as $filas){
            foreach($filas as $insertar){
    //Asignas los valores que quieres insertar a las variables que bindeaste
                $valor = $insertar;
                $query->execute();
                //si quisieras valores de retorno harías $query->bind_result()
                // no es necesario un ->fetch() porque los insert no retornan filas
            }
        }
    
        
    answered by 23.02.2018 в 13:33