Checkbox with data from one table, write to another

Asked

Viewed 3,379 times

3

I have a form with a checkbox paying for table data manpower and I need to record in detail, but all I can do is take and record the id of manpower but also need the price and not recording, follow the tables and the code I’m using:

manpower:

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

detail:

`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 checkbox selected, but the price only records the first.

  • You can post the form?

  • If you have several fields with the same price name, I think you should use [], that is to say name="preco[]". But it is not very clear which is your HTML... only has this form with 3 inputs`

1 answer

1


Example of how to recover this data:

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

Thus:

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

At the time of recovery it becomes easy, because if the id worthwhile 1 is recovered only you write $_POST['valores_'.id] that is to say, $_POST['valores_'.1] to recover the id = 1.

Full 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 recover more values is the same logic employed nome + id where id would be the key point of recovery of the other values.

Generated html:

<!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 the values 1 and 3 will output the result:

inserir a descrição da imagem aqui

Following your code would look something like this:

<?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'])){       
        $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>

Obs: I can’t debug your code, so I don’t know if it’s perfect. My example can copy and paste into another file that works perfectly and can follow as tutorial.

  • 1

    Harry I do not know how to thank you very much, for months I am trying to solve this problem and I had not been able to understand, now it became clear how it works, I tested here and this working properly, again thank you very much.

  • Hello Henrry, about the code you helped me, this perfect, but I came across a problem in the update form, could help me how to rescue the inserted checkbox, I have to create another question? Another question is about Stackoverflow, I have to register to appear my name same as yours, I just logged in with the gmail account, Thanks

  • @user3715576 type if you want the form at the time of loading to show you which one is selected ? that’s it ?

  • This is even for an eventual editing of items

  • would be such a thing: <?php $value['id'] == 1 ?' checked="checked"':'';?> if you don’t think it best to open another question detailing and putting example code like this?

Browser other questions tagged

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