PHP/MySQL Add Category to Matter

Question:

Well folks, I've never had much interest in the back-end area, but for bigger 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 couldn't do using the handouts, which is to register an article in a certain category.

I tried to do an INSERT followed by a SELECT but I was not successful, as the matter does not change the id_cat, it has the value "0" and not the value "1" which would refer to the category.

If anyone can give me an example of how the INSERT + SELECT would be I would be grateful.

Thank you very much in advance.

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

Complete code, I added the id_cat more continues generating id 0, to check if he was at least identifying the SELECT I changed the id_cat from int 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 ;

Answer:

Look in 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 you didn't list the tables, that is, this FK (id_cat) is not guaranteeing data integrity, you 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: For the id_cat to be registered correctly, this logic must be followed:

  1. Have at least one record in cat_lancamentos
  2. on Screen the user will choose this release, then its id should be saved for the next step
  3. Register the id of cat_lancamentos in the id_cat and its other attributes in the launches table
Scroll to Top