How to write data from two columns of a table using select?

Asked

Viewed 398 times

2

Hello, I am using a Select to take data from the Marks table, and save the code and brand name when registering the product, but I am not able to save the data of both simultaneously, I can only save the code or brand name, if you change them in VALUE.

My SELECT is like this:

        <label>Selecione a Marca:</label><br />
        <select name="codmarca">
        <?php
        include '../conexao.php';
        $select = mysql_query("SELECT * FROM marca");
        while($res = mysql_fetch_array($select)){
        ?>
        <option value="<?php echo $marca = $res['codigo'];?>"><?php echo $marca = $res['nome_marca'];?></option>
        <?php } ?>
        </select>

And that way I can only record the code, because it is he who is in VALUE at the moment.

How do I save code and tag at the same time using select, or not?

Thank you in advance for your attention to my problem.

2 answers

1

You can concatenate the code and brand name value and place them in VALUE. When redeeming the value of the VALUE attribute, split and save them in the desired fields.

<label>Selecione a Marca:</label><br />
    <select name="codmarca">
    <?php
    include '../conexao.php';
    $select = mysql_query("SELECT * FROM marca");
    while($res = mysql_fetch_array($select)){
    ?>
    <option value="<?php echo $marca = $res['codigo'] . '#' . $res['nome_marca'];?>"><?php echo $marca = $res['nome_marca'];?></option>
    <?php } ?>
    </select>

For the above code the value of the VALUE attribute will be: CODE#NOME_MARCA. When you redeem this value, split the string CODE#NOME_MARCA:

list($codigo, $nome) = explode("#", $data);
echo $codigo; // CÓDIGO DA MARCA
echo $nome; // NOME DA MARCA
  • I need to recover at least the name_tag on another page. Sorry E.Thomas, but I’m a layman in this part of explode, that part of the list I enter where? On the page where I will recover the data, or on the same page of SELECT?

  • Murilo, the explode is from PHP, according to your example. Imagine a string '3#PORSCHE'. When it is done: explodes('#', '3#PORSCHE') the return is an array with two elements. The list command puts the value of the first index (3) in the $code variable and the value of the second index (PORSCHE) in the $name variable. You must send this data and treat it in the . php file that inserts the information in the database, through a POST or GET.

1

E.Thomas' solution is valid.

But I think you can use Mysql itself for this.

Use only the codigoas value:

<option value="<?php echo $marca = $res['codigo'];?>"><?php echo $marca = $res['nome_marca'];?></option>

To record (INSERT) use something similar to:

INSERT INTO TABELA(nome) VALUES (SELECT nome_marca FROM marca WHERE codigo = ?)

The ? must be the value that got the option.

In short, it would be:

mysql_query("INSERT INTO TABELA(nome) VALUES (SELECT nome marca FROM marca WHERE codigo = $_POST['codmarca'])");

mysql_query is obsolete, I just used it as a quick and practical example to understand!

  • From what I understand he could not use the option tag to store more than one value for later recovery. But if his idea is to only record two data at the same time in the database, his answer is correct.

Browser other questions tagged

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