How to reference foreign keys in registration forms?

3

I am making a website from a library as a college work, which has the following table in the database:

MyproblemisthatIdonotknowhowtohandlethephpcodeandtheformoffieldsthatreceiveforeignkeys.

Forexample,intheloanfileIneedtoreferencethebookthatisbeingloanedandforthisIhavetheforeignkey'keepbook_digital_digital',butIdonotknowhowtomakethecodethatwouldenabletheofficialtoperformthisfunctioncorrectlyintheregistrationscreen.

Ifitisuseful,theregistrycodesthatIusedintheothertablesthatdonothaveforeignkeysfollowthisformat:

<?php require_once ("InscricaoClass.php"); $user = livros::getInstance(); if (isset($_GET['codigolivros'])) { $codigolivros=$_GET['codigolivros']; $user->__set('codigolivros', $codigolivros); $user->carregar(); } else { $codigolivros=0; } if ($_SERVER['REQUEST_METHOD']=='POST') { $user ->__set('titulo', $_POST['titulo']); $user ->__set('editora', $_POST['editora']); $user ->__set('autor', $_POST['autor']); $user ->__set('genero', $_POST['genero']); if ($_POST ['codigolivros']>0) { $user-> alterar(); } else { $user->gravar(); } } ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Cadastro Livros</title> </head> <body> <form action="" method="post"> <h2> Cadastro Livros</h2> <label>Código</label> Código: <input type="number" name="codigolivros" value="<?php echo $codigolivros;?>" > <br><br><br> Titulo: <br> <input type="text" name="titulo" value="<?php echo $user::$titulo;?>" placeholder=""> <br><br><br> Editora: <br> <input type="text" name="editora" value="<?php echo $user::$editora;?>" placeholder=""> <br><br><br> Autor: <br> <input type="text" name="autor" value="<?php echo $user::$autor;?>" placeholder=""> <br><br><br> Genero: <br> <input type="text" name="genero" value="<?php echo $user::$genero;?>" placeholder=""> <br><br> <input type="submit" name="" value="Gravar"> </form> </body> </html>

The above code requires this:

    <?php

class livros
{
    //inicio dos atributos
    public static $codigolivros;
    public static $titulo;
    public static $editora;
    public static $autor;
    public static $genero;  

    public static $instance;

    //inicio dos métodos
    public function __construct()
    {
        require_once("Conexao.php");
    }

    public static function getInstance()
    {
        self::$instance = new livros ();
        return self::$instance;
    }

    public function __set($var,$val)
    {
        $this->$var = $val;
    }

    public function __get($var)
    {
        $this->$var;
    }
    //Fim dos métodos padrões

    public function gravar()
    {
        try {
            $sql="insert into manterlivros (titulo, editora, autor, genero) values (:p1, :p2, :p3, :p4)";
            $con=Conexao::getInstance () ->prepare($sql);
            $con->bindValue (":p1", $this->titulo);
            $con->bindValue (":p2", $this->editora);
            $con->bindValue (":p3", $this->autor);
            $con->bindValue (":p4", $this->genero);
            $result=$con->execute();

            return $result;
        } catch (Exception $e) {
            echo "ERRO".$e->getMessage();
        }
    }

    public function consultar()
    {
        try {
            $sql ="select * from manterlivros";
            $con= conexao::getInstance()->prepare($sql);
            $con -> execute ();

            return $con;
        } catch (Expection $e) {
            echo "ERRO NO CONSULTAR";
        }
    }


    public function excluir($codigo)
    {
        try {
            $sql = "delete from manterlivros where codigolivros = '".$codigo."'" ;
            $con = Conexao::getInstance()->prepare($sql);

            return $con->execute();echo "excluindo";
        } catch (exception $e) {
            echo "ERRO NO EXCLUIR";
        }
    }

    public function alterar()
    {
        try {
            $sql="update manterlivros set titulo=:p1, editora=:p2, autor=:p3, genero=:p4 where codigolivros=:p0";

            $con=Conexao::getInstance()->prepare($sql);
            $con->bindValue (":p1", $this->titulo);
            $con->bindValue (":p2", $this->editora);
            $con->bindValue (":p3", $this->autor);
            $con->bindValue (":p4", $this->genero);
            $con->bindValue ("p0", $this->codigolivros);

            $result=$con->execute();

            return $result;
        } catch (Exception $e) {
            echo "Erro no Alterar";
        }
    }


    public function carregar()
    {
        try { 

            $sql ="select * from manterlivros where codigolivros=:p1";

            $con= Conexao::getInstance()->prepare($sql);
            $con->bindValue (":p1", $this->codigolivros);
            $con->execute ();

            foreach ($con as $linha) {
                $this::$titulo = $linha['titulo'];
                $this::$editora = $linha['editora'];
                $this::$autor = $linha['autor'];
                $this::$genero = $linha['genero'];
            }

            return $con;
        } catch (Expection $e) {
            echo "ERRO NO ALTERAR";
        }
    }
}
    
asked by anonymous 17.11.2015 / 01:41

2 answers

1

Fuller version:

Your form must be assembled with at least this information, a text box for the name, after typed the name is made aajax request for PHP fetch the enrollment based on the name, done the enrollment field is filled.

Now make a button that opens a modal to add the books to the loan. When clicking on record the main data received should be, matricula and livros (that an array contains id_livro and other data).

This information should be written to table emprestimos . The main fields are matricula , id_livro , data_emprestimo , data_devolucao .

Simple version (for example only)

Create a single form with the fields matricula , and for example 3 or limits of loans per student combobox the name of it must be enclosed in brackets, like this: <select name="livros[]"> , to record see the previous paragraph, you will need foreach to register all loans, also remember that sometimes a book can be borrowed.

    
17.11.2015 / 13:28
1

In your Books () class, create a method for querying the book ID entered in the name="codigolivros" field so you will get FK to write.

    
19.12.2016 / 01:20