PHP several Insert along with an update

Asked

Viewed 127 times

-1

I need to do several insert along with a update.

On my application, I gave a print_r($_POST); to see all variables being sent.

Array
(
    [matricula] => 10-03793383-19
    [id_aluno] => 11
    [id_curso] => 1
    [valorCurso] => 1000.00
    [valorPago] => 0.00
    [parcelas] => 2
    [formaPagamento] => 6
    [formaTaxas] => 0,00
    [id_polo] => 1
    [poloComissao] => 0,00
    [aMatricula] => Array
        (
            [0] => 10-03793383-19
            [1] => 10-03793383-19
        )

    [aParcelas] => Array
        (
            [0] => 1 de 2
            [1] => 2 de 2
        )

    [aParcelasForma] => Array
        (
            [0] => 6
            [1] => 6
        )

    [aParcelasVencimento] => Array
        (
            [0] => 2019-08-02
            [1] => 2019-08-09
        )

    [aParcelasValor] => Array
        (
            [0] => 500.00
            [1] => 500.00
        )

    [button] => 
    [form] => form1
    [dataMatricula] => 2019-08-02
    [editadorPor] => Administrador
    [editadoData] => 2019-08-02 07:08:27
    [status] => A
    [ID_Matricula] => 1
)

From the above value, those going to the insert is that data:

    [aMatricula] => Array
        (
            [0] => 10-03793383-19
            [1] => 10-03793383-19
        )

    [aParcelas] => Array
        (
            [0] => 1 de 2
            [1] => 2 de 2
        )

    [aParcelasForma] => Array
        (
            [0] => 6
            [1] => 6
        )

    [aParcelasVencimento] => Array
        (
            [0] => 2019-08-02
            [1] => 2019-08-09
        )

    [aParcelasValor] => Array
        (
            [0] => 500.00
            [1] => 500.00
        )

How should I do for the insert enter the records according to the amount of PLOTS?

PHP code:

if ((isset($_POST["form"])) && ($_POST["form"] == "form1")) {

    echo '<pre>';print_r($_POST);echo '</pre>';

    $rs1 = $mysqli->prepare("UPDATE matriculas SET
            matricula=?,
            id_aluno=?,
            id_curso=?,
        valorCurso=?,
            parcelas=?,
            valorPago=?,
        formaPagamento=?,
            formaTaxas=?,
            id_polo=?,
        poloComissao=?,
            dataMatricula=?,
            editadorPor=?,
        editadoData=?,
        status=?
        WHERE
            ID_Matricula=? ");

    $rs1->bind_param('ssssssssssssssi',
      $_POST['matricula'],
      $_POST['id_aluno'],
      $_POST['id_curso'],
      $_POST['valorCurso'],
      $_POST['parcelas'],
      $_POST['valorPago'],
      $_POST['formaPagamento'],
      $_POST['formaTaxas'],
      $_POST['id_polo'],
      $_POST['poloComissao'],
      $_POST['dataMatricula'],
      $_POST['editadorPor'],
      $_POST['editadoData'],
      $_POST['status'],
        $_POST['ID_Matricula']
        );

    $rs1->execute();


    $rs2 = $mysqli->prepare("INSERT INTO financ_receita (matricula, parcela, valor, dataLanc, dataVenci, formaEntrada) VALUES (?, ?, ?, ?, ?, ?) ");

    $rs2->bind_param('ssssss',
      $_POST['aMatricula'],
      $_POST['aParcelas'],
      $_POST['aParcelasValor'],
      date("Y-m-d"),
      $_POST['aParcelasVencimento'],
      $_POST['aParcelasForma']
    );

    $rs2->execute();


    if (($rs1->errno) or ($rs2->errno)) {
        echo 'Erro rs1: ', $rs1->error;
      echo 'Erro rs1: ', $rs2->error;
    } else {
        echo "<script>window.location='sucesso.php'</script>";
    }
}

2 answers

-1

For those who have the same question, follow the answer.

for ($i = 0; $i < $_POST['parcelas']; $i++) {
      $rs2 = $mysqli->query("INSERT INTO financ_receita (id_cadastro, matricula, parcela, valor, dataLanc, dataVenci, formaEntrada) VALUES (
         '".$_POST['id_cadastro'][$i]."',
         '".$_POST['aMatricula'][$i]."',
         '".$_POST['aParcelas'][$i]."',
         '".$_POST['aParcelasValor'][$i]."',
         '".date('Y-m-d')."',
         '".$_POST['aParcelasVencimento'][$i]."',
         '".$_POST['aParcelasForma'][$i]."'
      )");

-2

If I understand your question, you must ask INSERT according to the number of plots, then you must iterate parcelas and make the INSERT. I advise you to use the pdo as object of the seat, mysqli is already out of the most current versions of PHP.

  • 2

    Content is more for a comment than a response.

  • 3

    That information is incorrect. mysqli will not be discontinued (unless some improved version emerges one day, as happened with mysql lib that was discontinued by the improved mysqli version that is the current one) and PDO is lower than mysqli in the OP scenario. Understand that PDO is an abstraction library, and Mysqli is a pure wrapper, therefore more effective and more comprehensive in terms of functionality.

  • 1

    Truth @Bacco confused with mysql functions_*

Browser other questions tagged

You are not signed in. Login or sign up in order to post.