Get SQL data for Select

Asked

Viewed 35 times

-2

a forum recommended using this schema to take the data from an SQL column and bring it to select. However, when I submit the form, what is being sent is the id_project and not the project name_name. I am using the $_POST method. How to resolve? (I put the related parties only)

$nomeproj = $_POST['tipoProjeto'];

$insert="INSERT INTO despesas(
      nomeProjeto)
    VALUES(
      '$nomeproj')";

<p> <select name="tipoProjeto" id="projeto" required>
        <option value="" disabled selected>Projeto</option>
        <?php while($prod = mysqli_fetch_array($query)) { ?>
        <option value="<?php echo $prod['id_projeto'] ?>"><?php echo $prod['nome_projeto'] ?></option>
        <?php } ?>
    
        </select></p>

1 answer

0

What is sent to the backend is the content of the options value attribute. If you want to pass the project name, simply swap:

<option value="<?php echo $prod['id_projeto'] ?>">

for

<option value="<?php echo $prod['nome_projeto'] ?>">

However, some things are confusing in your code. Why relate the project name to the expense instead of the id it? Still, prefer to use a name for select that translates what it sends from data. Perhaps, instead of "projecttype", "project".

  • This is because it will be something shown on the screen on another page for an Adm. It’s already created, but what appears there is the project id and not the name.

  • I get it. Making this switch, the name will appear there.

Browser other questions tagged

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