Checkbox with data from one table, write to another

3

I have a form with a checkbox that pays data from the mao_obra table and needs to write to detail_orcamento , but I can only get and write the id from mao_obra but I also need the price and it is not writing, it follows the tables and the code I am using:

mao_obra:

'id' int(11) NOT NULL AUTO_INCREMENT,
'mao_obra' text NOT NULL,
'preco' decimal(10,2) DEFAULT NULL,
PRIMARY KEY ('id')

detail_orcamento:

'id' int(11) NOT NULL AUTO_INCREMENT,
'orcamento_id' int(11) NOT NULL,
'mao_obra_id' varchar(100) NOT NULL,
'preco' text NOT NULL,
PRIMARY KEY ('id'),
KEY 'mao_obra_id' ('mao_obra_id'),
KEY 'orcamento_id' ('orcamento_id')

Code:

<?php include("includes/config.php");?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
if (isset($_POST['enviar'])){
    $mao_obra_id        = $_POST['mao_obra_id'];
    $orcamento_id       = $_POST['orcamento_id'];
    $preco              = $_POST['preco'];

    foreach($_POST['mao_obra_id'] as $indice => $valor){
        $inserir = "INSERT INTO detalhe_orcamento1 (mao_obra_id, orcamento_id, preco) VALUE ('".$valor."', '".$orcamento_id."', '".$preco."')" or die(mysql_error());
        $ex = mysql_query($inserir) or die(mysql_error());
    }
}
?>
<?php

// consulta do select de Serviços
$selec = "SELECT * FROM mao_obra";
$exec = mysql_query($selec) or die(mysql_error());

// Lista dados do checkbox 
while($dados=mysql_fetch_assoc($exec)) {

$valor_id       = $dados['id'];
$valor_mao_obra  = $dados['mao_obra'];
$valor_preco     = $dados['preco'];

?>

<form id="form1" action=""enctype=" multipart/form-data" method="post">
    <input style="margin-left:30px" name="mao_obra_id[]" type="checkbox" value="<?php echo $valor_id ?>"/>&nbsp;&nbsp;<?php echo $valor_mao_obra ?>
    <input type="hidden" name="preco[]" value="<?php echo $valor_preco ?>" /><?php echo $valor_preco ?>
 <?php  }?>
 <input type="text" name="orcamento_id" />
    <input type="submit" name="enviar" value="Adicionar Orçamento" />
</form>

</body>
</html>

The id is recorded right by the checkbox selected, but the price only writes the first one.

    
asked by anonymous 07.06.2014 / 18:28

1 answer

1

Example how to recover this data:

The variable $checkbox is an array with id and value to display the values. The key point is that id is easy to retrieve, while value has to use another nome + id technique, and for id value 1 has input hidden valores_1 .

Like this:

<input type="checkbox" name="id[]"      value="1">
<input type="hidden"   name="valores_1" value="1.5">

At the time of recovery it is easy, because if the id value of 1 is recovered you only write $_POST['valores_'.id] ie $_POST['valores_'.1] to recover the value referring to id = 1 .

Complete example:

<?php
    $checkbox = array(array("id" => 1, "value" => 1.50),
                      array("id" => 2, "value" => 2.50),
                      array("id" => 3, "value" => 3.50));
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>checkbox</title>
</head>
<body>
    <form action="roto.php" method="post" enctype="multipart/form-data" name="form1">
    <?php foreach($checkbox as $value): ?>
        <input type="checkbox" name="id[]" value="<?php echo $value['id'];?>">
        <input type="hidden"   name="valores_<?php echo $value['id'];?>" value="<?php echo $value['value'];?>">
    <?php endforeach; ?>
        <button>Enviar</button>
    </form>
    <?php
        if (isset($_POST['id'])):
            foreach($_POST['id'] as $key => $value):
                echo $value . ' - ' . $_POST['valores_'.$value];
                echo '<br>';
            endforeach;
        endif;
    ?>
</body>
</html>

If you need to retrieve more values it is the same logic used as nome + id where id would be the key recovery point of the other values.

Html generated:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>checkbox</title>
</head>
<body>
    <form action="roto.php" method="post" enctype="multipart/form-data" name="form1">
        <input type="checkbox" name="id[]"      value="1">
        <input type="hidden"   name="valores_1" value="1.5">

        <input type="checkbox" name="id[]"      value="2">
        <input type="hidden"   name="valores_2" value="2.5">

        <input type="checkbox" name="id[]"      value="3">
        <input type="hidden"   name="valores_3" value="3.50">

        <button>Enviar</button>
    </form>
    </body>
</html>

Choosing values 1 and 3 will output the result:

Followinginyourcodewouldlooksomethinglikethis:

<?phpinclude("includes/config.php");?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
    if (isset($_POST['enviar'])){       
        $orcamento_id = $_POST['orcamento_id'];
        foreach($_POST['mao_obra_id'] as $indice => $valor){
            $preco   = $_POST['preco'.$valor];
            $inserir = "INSERT INTO detalhe_orcamento1 
            (mao_obra_id, orcamento_id, preco) VALUE ('".$valor."', '".$orcamento_id."', '".$preco."')" 
            or die(mysql_error());
            mysql_query($inserir) or die(mysql_error());
        }
    }   
    // consulta do select de Serviços
    $selec = "SELECT * FROM mao_obra";
    $exec = mysql_query($selec) or die(mysql_error());
    // Lista dados do checkbox 
?>

<form id="form1" action="" enctype=" multipart/form-data" method="post">
<?php
    while($dados = mysql_fetch_assoc($exec)){
        $valor_id        =  $dados['id'];
        $valor_mao_obra  =  $dados['mao_obra'];
        $valor_preco     =  $dados['preco'];

?>
    <input style="margin-left:30px" name="mao_obra_id[]" type="checkbox" value="<?php echo $valor_id ?>"/>
    &nbsp;&nbsp;<?php echo $valor_mao_obra ?>
    <input type="hidden" name="preco<?php echo $valor_id ?>" value="<?php echo $valor_preco ?>" />
    &nbsp;&nbsp;<?php echo $valor_preco ?>
<?php 
    } 
?>
    <input type="text" name="orcamento_id" />
    <input type="submit" name="enviar" value="Adicionar Orçamento" />
</form>
</body>
</html>

Note: I can not debug your code, so I do not know if it's perfect. My example can copy and paste into another file that works perfectly and can follow as a tutorial.

    
08.06.2014 / 02:32