How to take data from different tables and insert into a new

Asked

Viewed 825 times

1

Good evening, I’m doing a job for a course subject and in it I have to work with three tables, in my case I use a table customer registration, a second table car registration and the third would be a junction of the two rental registration from two combobox, one that picks up all the registered clients and another all the registered models. My question is: How can I take these two dice from the combobox and play in a third table? Table 1 - customers, table 2 - cars, table 3 - rent.

I will leave the two parts that I made regarding the third page, ver_carros.php is the page and rents.php was where I was trying to play for the bank. I know some parts of renti.php are wrong but I left it that way because it was the last edition I made

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>

rent.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.

  • Hello without having a data structure, I can only tell you that you need to save the Id (unique identifier ) of the person and the car, I suggest you have a boolean in the third table in order to know if the rental is active or not. In terms of php I can not help you much because I am not an expert but in terms of database design I hope to have helped you.

  • Giving error? Which error? This snippet is wrong: $query = "INSERT INTO aluga ( nome , 'price' ) VALUES ('$nominaluguel', '$precoaluguel')";

  • Can you show how the tables are? You want to make a record of car rentals, is that it? You can use a table of rental records that includes the user’s foreign key and other foreign car key. And maybe more dates, and everything else you need.

  • I don’t know much about PHP, but I can say that your question is confused because I don’t understand where your difficulty is. You posted the code you did, but it doesn’t explain what each page does, and mostly it doesn’t say whether it makes a mistake or what happens. Try to edit to make your difficulty clear, or someone will hardly be able to help you.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.