Fórum List/Menu - PHP #432152
08/01/2013
0
<?php
include ("conexao.php");
$acao = @$_GET["acao"];
$id = @$_GET["id"];
if ($acao != "")
{
$sql = "SELECT c.*, s.* FROM categoria c, subcategoria s WHERE c.id_categoria = s.id_categoria and id_subcategoria = '$id' ";
$qry = mysql_query($sql);
$linha = mysql_fetch_array($qry);
$subcategoria = $linha[@subcategoria];
$id_categoria = $linha[@id_categoria];
}
?>
<!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=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><div align="center">Cadastro de Categorias </div></td>
</tr>
<tr>
<td><form id="form1" name="form1" method="post" action="op_subcategoria.php">
<table width="314" border="1" align="center" cellpadding="0" cellspacing="0">
<tr>
<td>Categoria</td>
<td><label>
<select name="id_categoria" id="id_categoria">
<option>Selecione uma categoria</option>
<?php
$sql = "SELECT * FROM categoria ORDER BY categoria";
$qry = mysql_query($sql);
while (@$linha = mysql_fetch_array($qry)){
$valor = $linha[@id_categoria];
if ($id_categoria = $valor)
{
$selecionado = "selected";
}
else
{
$selecionado ="";
}
echo "<option value = \"$valor\" $selecionado > $linha[categoria] </option>";
}
?>
</select>
</label></td>
</tr>
<tr>
<td>SubCategoria</td>
<td width="211"><input name="txt_subcategoria" type="text" id="txt_subcategoria" value="<?php echo @$subcategoria; ?>" size="35" /></td>
</tr>
<tr>
<td colspan="2"><label>
<div align="center">
<input type="submit" name="Submit" value="<?php if($acao != ""){echo $acao;}else {echo "Inserir";}?>" />
<input name="acao" type="hidden" id="acao" value="<?php if($acao != ""){echo $acao;}else {echo "Inserir";}?>" />
<input name="id" type="hidden" id="id" value="<?php echo $id; ?>" />
</div>
</label></td>
</tr>
</table>
</form>
</td>
</tr>
<tr>
<td> </td>
</tr>
</table>
</body>
</html>
a operação de atualizar é assim:
if ($acao == Alterar)
{
$sql = "UPDATE subcategoria SET
id_categoria = '$id_categoria',
subcategoria = '$txt_subcategoria'
WHERE id_subcategoria = '$id' ";
mysql_query($sql) or die ("Não foi possivel alterar os dados");
echo "<script type = 'text/javascript'> location.href='index.php?link=4'</script>";
}
Marcio Araujo
Curtir tópico
+ 0Posts
08/01/2013
Marcio Araujo
if ($acao == "Inserir")
{
$sql = "INSERT INTO subcategoria (id_categoria, subcategoria) VALUES ('$id_categoria','$txt_subcategoria')";
mysql_query($sql) or die ("Não foi possivel inserir os dados");
echo "<script type = 'text/javascript'> location.href='index.php?link=4'</script>";
}
Gostei + 0
09/01/2013
Marcio Araujo
Gostei + 0
09/01/2013
William
Gostei + 0
09/01/2013
Marcio Araujo
Gostei + 0
10/01/2013
Marcio Araujo
Gostei + 0
10/01/2013
William
1 - Alterei um pouco sua instrução SQL;
2 - Sua condição if estava com apenas 1 sinal de "=";
<?php
include ("conexao.php");
$acao = @$_GET["acao"];
$id = @$_GET["id"];
if ($acao != "")
{
$sql = "SELECT c.id_categoria, s.subcategoria FROM categoria c INNER JOIN subcategoria s ON c.id_categoria = s.id_categoria and id = '$id' ";
$qry = mysqli_query($conexao, $sql);
$linha = mysqli_fetch_array($qry);
$subcategoria = $linha['subcategoria'];
$id_categoria = $linha['id_categoria'];
}
?>
<!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><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><div align="center">Cadastro de Categorias </div></td>
</tr>
<tr>
<td><form id="form1" name="form1" method="post" action="op_subcategoria.php">
<table width="314" border="1" align="center" cellpadding="0" cellspacing="0">
<tr>
<td>Categoria</td>
<td><label>
<select name="id_categoria" id="id_categoria">
<option>Selecione uma categoria</option>
<?php
$sql = "SELECT * FROM categoria ORDER BY categoria";
$qry = mysqli_query($conexao, $sql);
while ($linha = mysqli_fetch_array($qry)){
$valor = $linha['id_categoria'];
if ($id_categoria == $valor)
{
$selecionado = "selected";
}
else
{
$selecionado ="";
}
echo "<option value = \"$valor\" $selecionado>".$linha['categoria'] ."</option>";
}
?>
</select>
</label></td>
</tr>
<tr>
<td>SubCategoria</td>
<td width="211"><input name="txt_subcategoria" type="text" id="txt_subcategoria" value="<?php echo @$subcategoria; ?>" size="35" /></td>
</tr>
<tr>
<td colspan="2"><label>
<div align="center">
<input type="submit" name="Submit" value="<?php if($acao != ""){echo $acao;}else {echo "Inserir";}?>" />
<input name="acao" type="hidden" id="acao" value="<?php if($acao != ""){echo $acao;}else {echo "Inserir";}?>" />
<input name="id" type="hidden" id="id" value="<?php echo $id; ?>" />
</div>
</label></td>
</tr>
</table>
</form>
</td>
</tr>
<tr>
<td> </td>
</tr>
</table>
</body>
</html>
Qualquer coisa posta aí ...
Gostei + 0
10/01/2013
William
Gostei + 0
11/01/2013
Marcio Araujo
Gostei + 0
11/01/2013
Marcio Araujo
não entendi, essa parte.
Gostei + 0
11/01/2013
William
Você executa sua querys assim:
$qry = mysql_query($sql);
Eu executo com outro tipo de função:
$qry = mysqli_query($conexao, $sql);
Note a diferença entre "mysql" e "mysqli"!
As funções "mysqli" foram implementadas mais recentemente pelo PHP, e trázem algumas funcionalidades a mais que o "mysql", blz?
Gostei + 0
11/01/2013
Marcio Araujo
assim que tiver algum resultado, te aviso
obrigado mais uma vez...
Gostei + 0
11/01/2013
Marcio Araujo
eu iria morrer doido e não saberia...kkkkkkkkkkkk
era só o "==" tinha só um...
MUITO OBRIGADO!!!
Gostei + 0
30/07/2013
José
Estou dando este tópico por concluído.
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)