Take array data and update to table

Asked

Viewed 86 times

0

Good morning.

I’m making a INSERT in the bank via PHP, with data coming from a SELECT MULTIPLE. The data of this SELECT comes from a bank table:

HTML:

<select name="numerosdasorte[]" class="form-control" multiple="">

   <?php
      $consulta = $PDO->query("SELECT * FROM cota1 ORDER BY cota ASC");
      while ($linha = $consulta->fetch(PDO::FETCH_ASSOC)) {
   ?>

   <option value="<?php echo $linha['cota']; ?>"><?php echo $linha['cota']; ?></option>

   <?php } ?>

</select>

When do I perform the SUBMIT of FORM, take the variable and place the commas to separate the chosen numbers:

PHP:

$numerosdasorte = '';
    foreach($_POST['numerosdasorte'] as $numeros){
        $numerosdasorte .= ',' . $numeros;
    }

  $numerosdasorte = ltrim($numerosdasorte, ',');

The code works normally, saved in the bank exactly the way I want it. The question is this: I need to separate these numbers to do the UPDATE on the table cota1, so that, when entering again, they are unavailable. In the table, I set as BOOLEAN, so I need to do this UPDATE to make the number unavailable for the next registration.

Anyway, I can’t think of a way to do that. A friend suggested making a UPDATE before FORM be submitted, in the choice of number. But, if the person gives up the registration, the number will be unavailable, even if the FORM is not submitted.

I think if I can separate those numbers, I can do the UPDATE of each of them in the table. What do you think?

  • I would like the justification of "downvote", to know where I am going wrong. For me, it is a doubt. I think every downvote should have mandatory justification.

  • you want to separate these numbers into an array? being each number in 1 position?

  • Arthur, thank you for your time. I think it would be easier to update the table of numbers in the bank. Unless you have another way, I’d like your opinion, please,

1 answer

1


supposing your variable arrives like this

Ex:

Attribution:

$var = "1,2,3,4,5,6,7,8,9";

Dump:

string(17) "1,2,3,4,5,6,7,8,9"

you can give a explode.

Example:

$exploded = explode(",",$var);

the output of $exploded would be:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
)

Functional documentation explode can be found here!.

  • Great, thanks! I’ll try here in a few minutes, and I’ll be right back!

  • When I give the ECHO in the $value, only comes the last number chosen.

  • 1

    edited the answer, I think this will suit you best friend

  • 1

    Perfect. It worked fine, thank you! Now I just need to think about how not to do all the numbers, the code would be huge. Big hug, closed topic!

Browser other questions tagged

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