How to get data from different tables and insert into a new [closed]

1

Good evening, I'm doing a work for a course material and in it I have to work with three tables, in my case I use a customer registration table, a second car registration table and the third would be a junction of the two rental records from of two combobox, one that catches all registered customers and another all registered models. My question is: How can I get these two data from the combobox and play on a third table? There is table 1 - customers, table 2 - cars, table 3 - rents.

I'll leave the two parts I did referring to the third page, ver_carros.php is the page and aluga.php was where I was trying to play for the bank. I know some parts of the aluga.php is wrong but I left it because it was the last edition I did

ver_carros.php

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Aluguel de Carros</title>
<link href="css/bootstrap.min.css" rel="stylesheet">

<script src="js/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>

</head>

<body>



<!-- COMEÇO DA NAVBAR -->
<nav class="navbar navbar-default">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="index.php">Aluguel de Carros</a>
    </div>

    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li><a href="cadastro.html">Cadastrar usuário</a></li>
        <li><a href="cadastrocar.html">Cadastrar carro</a></li>
        <li><a href="ver_carros.php">Alugar carro</a></li>
        <li><a href="alugueis.php">Histórico</a></li>
      </ul>
    </div>
  </div>
</nav>
<!-- FIM DA NAVBAR -->

<h1 align="center">Aluguel de Carros</h1>

<hr>
  <form class="form-horizontal" id="alugarcarro" name="alugarcarro" method="post" action="aluga.php" onSubmit="return validaCampo(); return false;>
    <fieldset>
<div class="form-group">
        <label for="select" class="col-lg-2 control-label">Nome</label>
        <div class="col-lg-10">
          <select class="form-control" id="nome" name="nome">
          <?php
            $conexao = mysqli_connect("localhost","root", "", "clientes" ); 
        if (mysqli_connect_errno())
        die ("Erro de conexão com localhost, o seguinte erro ocorreu -> ".mysqli_error());
        $banco = mysqli_select_db($conexao, "clientes"); 
        if (!$banco)
        die ("Erro de conexão com banco de dados, o seguinte erro ocorreu -> ".mysqli_error($conexao));
        $sql = "select id,nome from clientes order by nome;";
        $resultado = mysqli_query($conexao,$sql);
        while ($row = mysqli_fetch_array($resultado)){
        echo "<option value='" . $row['id'] . "'>" . $row['nome'] . "</option>"; }
        mysqli_close($conexao);
        ?>



        </select>
        </div>
      </div>

<div class="form-group">
        <label for="select" class="col-lg-2 control-label">Carros</label>
        <div class="col-lg-10">
          <select class="form-control" id="preco" name="preco">
          <?php
            $conexao = mysqli_connect("localhost","root", "", "clientes" ); 
        if (mysqli_connect_errno())
        die ("Erro de conexão com localhost, o seguinte erro ocorreu -> ".mysqli_error());
        $banco = mysqli_select_db($conexao, "clientes"); 
        if (!$banco)
        die ("Erro de conexão com banco de dados, o seguinte erro ocorreu -> ".mysqli_error($conexao));
        $sql = "select id,modelo,preco from carros order by preco;";
        $resultado = mysqli_query($conexao,$sql);
        while ($row = mysqli_fetch_array($resultado)){
        echo "<option value='" . $row['id'] . "'>" . $row['modelo'] . " - R$" . $row['preco'] . "</option>"; }
        mysqli_close($conexao);
        ?>

        </select>
        </div>
      </div>


      <div style="float:right;">
          <input name="alugarcarro" type="submit" id="alugarcarro" class="btn btn-success" value="Alugar carro!" />
          <input name="limpar" type="reset" id="limpar" class="btn btn-danger" value="Limpar Campos preenchidos!" />
        </div>
        </div>
      </div>
    </fieldset>
  </form>
  </form>


</div>
</body>
</html>

aluga.php

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Carro alugado com sucesso!</title>
<link href="css/bootstrap.min.css" rel="stylesheet">

<script src="js/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>
</head>
<body>
<?php 
        $nomealuguel = $_POST ["nome"];
        $precoaluguel = $_POST ["preco"];

//Gravando no banco de dados ! conectando com o localhost - mysql
$conexao = mysqli_connect("localhost","root", "", "clientes" ); 
if (mysqli_connect_errno())
die ("Erro de conexão com localhost, o seguinte erro ocorreu -> ".mysqli_error());

//conectando com a tabela do banco de dados
$banco = mysqli_select_db($conexao, "clientes"); 
if (!$banco)
die ("Erro de conexão com banco de dados, o seguinte erro ocorreu -> ".mysqli_error($conexao));


//Query que realiza a inserção dos dados no banco de dados na tabela indicada acima
$query = "INSERT INTO 'aluga' ( 'nome' , 'preco' )
VALUES ('$nomealuguel', '$precoaluguel')";
mysqli_query($conexao,$query);
//echo "Seu cadastro foi realizado com sucesso!Agradecemos a atenção.";
//mensagem que é escrita quando os dados são inseridos normalmente.
?>

<nav class="navbar navbar-default">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="index.php">Aluguel de Carros</a>
    </div>

    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li><a href="cadastro.html">Cadastrar usuário</a></li>
        <li><a href="cadastrocar.html">Cadastrar carro</a></li>
        <li><a href="ver_carros.php">Alugar carro</a></li>
        <li><a href="alugueis.php">Histórico</a></li>
      </ul>
    </div>
  </div>
</nav>

<div class="alert alert-success col-xs-6 col-xs-offset-3" align="center">
  <h1>Alugado com sucesso</h1>
  <?php echo $nomealuguel; ?> , você alugou um carro com sucesso!
</div>

</body>
</html>

Sorry if you're confused, my first post here.

    
asked by anonymous 24.11.2015 / 03:45

0 answers