3
Good staff never had much interest in the back-end area, but for greater reasons I’m having to learn.
So far I can do the basics, which are insert, delete, update, view the data using php/mysql.
Now I came across a problem that I could not do using the handouts, which is to register a subject in a certain category.
I tried to do an INSERT followed by a SELECT but I was not successful, because the material does not change the id_cat, it is with the value "0" and not with the value "1" that would reference the category.
If anyone could give me an example of what INSERT + SELECT would look like.
Thank you very much.
(NOTE: I’m not using OO..., I’m doing it the old way with mysql_, which is the easiest way to learn.)
Full code, I added id_cat but it keeps generating id 0, to check if it was at least identifying SELECT I changed the int id_cat to varchar and the same record the category name in id_cat normally.
<?php
if(isset($_POST['enviar'])) {
$id_cat = $_POST['cat_lancamentos'];
$titulo = $_POST['titulo'];
$original = $_POST['original'];
$capa = $_FILES["capa"] ["name"];
$lancamento = $_POST['lancamento'];
$genero = $_POST['genero'];
$diretor = $_POST['diretor'];
$sinopse = $_POST['sinopse'];
$midia = $_POST['video'];
$caminho_img = "../capas/";
if(move_uploaded_file($_FILES["capa"]["tmp_name"],$caminho_img."/".$capa)) {
$mysql_path = $caminho_img."/".$capa;
$sql=mysql_query("INSERT INTO lancamentos VALUES('', '$id_cat', '$titulo', '$original', '$capa', '$diretor', '$lancamento', '$genero', '$midia', '', '', '$sinopse')");
echo "INSERT INTO lancamentos VALUES('', '$id_cat', '$titulo', '$original', '$capa', '$diretor', '$lancamento', '$genero', '$midia', '', '', '$sinopse')";
if($sql)
{
header("location: lancamentos-gerenciar.php");
}
}
}
?>
<form method="post" action="" enctype="multipart/form-data">
<pedido>
<botaoexcluir><a href="#"><font color="red">Cancelar</font></a></botaoexcluir> <botaoadicionar><input type="submit" name="enviar" value="Cadastrar"></botaoadicionar>
<capa><img id="uploadPreview" width="152px" height="214px" /> </capa>
<campopedido><strong>Titulo:</strong> <input type="text" name="titulo" id="titulo" size="50"></campopedido>
<campopedido><strong>Titulo Original:</strong> <input type="text" name="original" id="original" size="40"></campopedido>
<campopedido><strong>Lançamento:</strong> <input type="text" name="lancamento" id="lancamento" size="40"></textarea></campopedido>
<campopedido><strong>Genero:</strong> <input type="text" name="genero" id="genero" size="40"></campopedido>
<campopedido><strong>Categoria:</strong>
<select name="cat_lancamentos" size="1" id="cat_lancamentos">
<option value="">Selecionar</option>
<?php
$sql = "SELECT * FROM cat_lancamentos";
$resultado = mysql_query($sql) or die('Erro ao selecionar a categoria: ' .mysql_error());
while($linhas = mysql_fetch_array($resultado))
{
$selected = ($linhas['nome'] == $_POST['cat_lancamentos'])?'selected':'';
?>
<option value="<?php echo $linhas['nome']; ?>" <?php echo $selected; ?>>
<?php echo $linhas['nome']; ?>
</option>
<?php
}
?>
</select>
</campopedido>
<campopedido><strong>Dirigido por:</strong> <input type="text" name="diretor" id="diretor" size="40"></campopedido>
<campopedido><strong>Selecionar capa:</strong> <input id="uploadImage" type="file" name="capa" onchange="PreviewImage();" /></campopedido>
<sinopse>
<strong>Sinopse</strong><br />
<textarea name="sinopse" id="sinopse" size="85" cols="40" rows="5" style="width:620px; height:80px; margin-top:10px;"></textarea>
</sinopse>
</pedido>
<titulo> Midia</titulo>
<pedido>
<codigo>
<p>Codigo de adicição de midia</p>
<textarea name="video" id="video" size="85" cols="40" rows="5" style="width:600px; height:120px; margin-top:-20px;margin-bottom:10px;">
</textarea>
</codigo>
</pedido>
</form>
My Mysql Tables
CREATE TABLE `cat_lancamentos` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`nome` varchar(255) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM CHARSET=utf8 AUTO_INCREMENT=1 ;
CREATE TABLE `lancamentos` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`id_cat` bigint(20) NOT NULL,
`titulo` varchar(255) NOT NULL,
`original` varchar(255) NOT NULL,
`capa` varchar(100) NOT NULL,
`diretor` varchar(50) NOT NULL,
`lancamento` varchar(50) NOT NULL,
`genero` varchar(255) NOT NULL,
`midia` varchar(255) NOT NULL,
`temporada` varchar(50) NOT NULL,
`episodios` varchar(20) NOT NULL,
`sinopse` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM CHARSET=utf8 AUTO_INCREMENT=1 ;
Post the structure of these tables you are using (name of the fields)
– Lucas
I added the last code I tried. And thank you for answering my question.
– Anderson Rodrigues