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 ?>"/> <?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.