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 ?>"/> <?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?
– rray
If you have several fields with the same price name, I think you should use
[]
, that is to sayname="preco[]"
. But it is not very clear which is your HTML... only has this form with 3 inputs`– Sergio