PHP / MySQL Add Category to Subject

3

Good people have never had much interest in the back-end area, but for greater reasons I'm having to learn.

So far the basics I can already do, 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 story in a particular category.

I tried to do an INSERT followed by a SELECT but I did not succeed, because the matter does not change to id_cat, it gets to value "0" and not to the value "1" p>

If someone can give me an example of how INSERT + SELECT would look, I would be grateful.

Thank you in advance.

(Note: I'm not using OO ..., I'm doing the old form with mysql_, which is the easiest way to learn.)

<?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 ;
    
asked by anonymous 29.04.2015 / 14:25

2 answers

1

Look at your table structure where it is:

'id_cat' bigint(20) NOT NULL, // aqui você diz que não pode ser null

Look at your php where you have the insert:

$sql = mysql_query("INSERT INTO lancamentos (id, id_cat, titulo, original, capa, diretor, lancamento, genero, midia, temporada, episodios, sinopse)  VALUES('', '', '$titulo', '$original', '$capa', '$diretor', '$lancamento', '$genero', '$midia', '', '', '$sinopse')
  

You are passing null in id_cat

Another thing, you made the structure for a FOREIGN KEY , but did not relate the tables, ie this FK (id_cat) is not guaranteeing data integrity, would have to make the relationship like this:

ALTER TABLE 'lancamentos' 
ADD CONSTRAINT 'fk_lancamentos'
FOREIGN KEY ('id_cat')
REFERENCES 'cat_lancamentos' ('id')
ON DELETE NO ACTION
ON UPDATE NO ACTION;

EDIT: In order for the id_cat to be correctly registered, you must follow this logic:

  • Have at least one record in cat_channels
  • On the screen the user will choose this release, so the id should be saved to the next step
  • Record in the releases table the id of the cat_lacings in the cat_id and its other attributes
  • 29.04.2015 / 20:44
    0

    I did not understand very well what I wanted, so I understood, first you must do the insert and then the select, and the select together you wanted would be this?

    select l.id, ct_l.nome, l.titulo, l.original, l.capa, l.diretor, l.lancamento, l.genero, l.midia, l.temporada, l.episodios, l.sinopse from lancamentos l
    inner join cat_lancamentos ct_l on ct_l.id = l.id_cat;
    
        
    29.04.2015 / 20:50