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

1
Hello, I'm using a Select to get data from the Tags table, and save the code and the brand name when I register the product, but I'm not able to save the data of the two simultaneously, ie I can only write the code or the brand name, if you change them in VALUE.

My SELECT looks 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 so I can only write the code, because it's what's in VALUE right now.

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

Thank you in advance for everyone's attention to my problem.

    
asked by anonymous 07.04.2016 / 00:45

2 answers

0

You can concatenate the code and tag name value and put them in VALUE. When you redeem the value of the VALUE attribute, split it and save it 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 attribute value will be: CODE # MARK_NAME. When you are going to redeem this value, split the string CODE # MARK_NAME:

list($codigo, $nome) = explode("#", $data);
echo $codigo; // CÓDIGO DA MARCA
echo $nome; // NOME DA MARCA
    
07.04.2016 / 01:06
0

The E.Thomas solution is valid.

But I think you can use MySQL itself for that.

Only use codigo as value:

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

To write ( INSERT ) use something similar to:

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

The ? should be the value that got the option.

Summarizing 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!

    
07.04.2016 / 01:16