How to insert a number N number of times in the same row? [closed]

0

I must insert the cost per fee according to the amount of fees that the EXAMPLE course has: SAY HOW MANY FEES YOU HAVE THE COURSE: 5 VALUE PER QUOTA: 22.000BS. This value must be saved 5 times in 5 different fields c1, c2, c3, c4, c5. And so keep it any number of times according to the number of installments.

    
asked by gbianchi 07.08.2017 в 03:08
source

1 answer

2

Basically what you are trying to do is to generate a string for your insert that is created based on a counter. Following your example, a possible code would be:

//Num de cuotas  a insertar
$numeroCuotas = 5;

// Asumimos que la conexión a mysql ya está inicializada corréctamente y se almacena en la variable $db

//Variable que almacenará los campos sobre los q hacer le insert
$campos = "";

//Variable para los valores a insertar.
$valores = "";

for($i = 1; $i <= $numeroCuotas; $i++){
    $campos .= 'costo' . $i . ', ';
    $valores .= '\'22000\', ';
}

//Quitamos los carácteres ', ' de los strings
if($campos != ''){
    $campos = substr($campos, 0, -2);
    $valores = substr($valores, 0, -2);
}

//Generamos los inserts
$insert = 'INSERT INTO TABLA (' . $campos . ') VALUES (' . $valores . ');';

//Ejecutamos el insert
$db->query($insert);
    
answered by 07.08.2017 / 08:35
source