Search data from a database table

Asked

Viewed 133 times

0

I ask for your help in my code. I’m creating a php code that can make reservations for certain cars, the basic thing is to book the car for user x I can already do. The problem, which will actually only appear in the future, is at the time of selecting the Car, while there are few cars it is easy to find your favorite but with the passage of time the table will increase and much and will get very tiring looking for your favorite car. I was wondering if it’s possible to create a button "Search Car" where the user would put the name of the car in input and when you click on the button the table where all the cars are is updated and all the cars with that Name appear. In case you don’t have a car with that name alert saying that there is no car with that name.

I’m new to programming and so I don’t know if it’s possible to do this in Php, If it is, you could help me ?

That’s the code I’m creating

Reserva.php

<body>
	<?php
	include_once "conexao.php";
	?>

   <div class="col-lg-3">
        <div class="form-group">
            <label for="NOME">Nome do Carro: </label>
            <input class="form-control"  placeholder="Nome do Carro" name="NOME"> 
<button type="submit" class="btn btn-primary" style="margin-top: 24;">Buscar</button>
        </div>
    </div>
   <br>
  
<table id="tab" border="" width="430px">
  <form method="post">
 <thead>
<tr>
  <th>ID</th>
  <th>Carro</th>
  <th>Marca</th>
  <th>Modelo</th>
  <th>Cor</th>
  <th>Ano</th>
</tr>
</thead>
 </style>
<center>

  <?php
            include_once "conexao.php";
            $sql = "select c.id_adm, c.id_carro, c.Nome_carro, c.Marca, c.Modelo, c.Cor, c.Ano_carro, a.id_adm, a.login from carros as c join adm as a on
            a.id_adm = c.id_adm ";
            $result = mysql_query($sql,$con);
            if($result){
            while($linha = mysql_fetch_array($result)){
?>
<tbody>
       <tr id="<?php echo $linha['id_carro']; ?>">
           <td data-target="id_carro"> <?php  echo $linha['id_carro'];?></td>
           <td data-target="Nome_carro"> <?php echo $linha['Nome_carro'];?></td>
           <td data-target="Marca"> <?php echo $linha['Marca'];?></td>
           <td data-target="Modelo"> <?php echo $linha['Modelo'];?></td>  
           <td data-target="Cor"> <?php echo $linha['Cor'];?></td>  
           <td data-target="Ano_carro"> <?php echo $linha['Ano_carro'];?></td>       
           
       </tr>
       <script type="text/javascript">
    $('tbody tr').click(function(){
        var id = $(this).attr('id');
        $.each($('#'+id+" td"),function(){
            var target = $(this).data('target');
            $("input[name='"+target+"']").val($(this).html());
        });
    });
</script>
</tbody>
<?php
          }
      
          }
          mysql_close($con);
?>
</form>
</table>




	 <p><h1>Marque um Horario</h1></p>

	<form method="post" action="evento.php?reserva=true">
		<div class="form-group your-id">
    <input class="wpcf7-form-control wpcf7-text  wpcf7-validates-as-required form-control form-control" name="id_carro" style="display: none">
  </div>
   
       <table>
 <tr>
  <td> Nome Carro:</td>
  <td><input class="wpcf7-form-control wpcf7-text  wpcf7-validates-as-required form-control form-control" name="Nome_carro" disabled required></td>
</tr>
 
  
    
 <tr>
  <td align="right">Marca:</td>
  <td><input class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required  form-control form-control" name="Marca" disabled required></td>
</tr>
 
    
    
        
 <tr>
  <td align="right">Cor:</td>
  <td> <input class="wpcf7-form-control wpcf7-text  wpcf7-validates-as-required form-control form-control" name="Cor" disabled required></td>
</tr>



   
 <tr>
  <td align="right">Ano:</td>
  <td> <input class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required  form-control form-control" name="Ano_carro" disabled required></td>
</tr>   
       

   </table>
		<table>
			<tr>
		<td><strong> Data:</strong></td>
		<td><input type="date" name="date" required><br></td>
		</tr>
		<tr><td><strong>Horario:</strong></td>
			<td><select name="horario" required>
				<option selected></option>
				<option value="09:00">09:00</option>
				<option value="10:00">10:00</option>
				<option value="11:00">11:00</option>
				<option value="12:00">12:00</option>
				<option value="13:00">13:00</option>
        <option value="14:00">14:00</option>
        <option value="15:00">15:00</option>
        <option value="16:00">16:00</option>
        <option value="17:00">17:00</option>
        <option value="18:00">18:00</option>
			</select></td></tr>

			<tr><td><strong>Consessionaria:</strong></td>
			<td><select name="consessionariaa" required>
				<option selected></option>
				<option value="Pontes Vieira">Pontes Vieira</option>
				<option value="Antonio Bezerra">Antonio Bezerra</option>
				<option value="João Feliciano">João Feliciano</option>
				<option value="Cezar Sales">Cezar Sales</option>
				<option value="Pedro Algusto Junior">Pedro Algusto Junior</option>
        <option value="Paris">Paris</option>
			</select></td></tr>
				<tr>
					<td colspan="2" align="center"><input type="submit" value="Fazer Reserva"></td>
				</tr>
				
		</table>
	</form>

</body>

Connexion.php

<HTML>
<HEAD>
 <TITLE>New Document</TITLE>
</HEAD>
<BODY>
<?php
  $host="localhost";
  $login="root";
  $senha="";
  $banco="concessionaria";

  $con=mysql_connect($host,$login,$senha)or die("Erro ao conecta!".mysql_error());
  $sql="create database if not exists ".$banco;
  mysql_query($sql,$con);
  mysql_select_db($banco,$con);
  $sql = "create table if not exists adm(
  id_adm int(8) auto_increment,
  login varchar(100) not null,
  senha varchar(50) not null,
  primary key(id_adm))";
  mysql_query($sql,$con);
    $sql = "create table if not exists usuario(
  id_usuario int(8) auto_increment,
  login varchar(100) not null,
  senha varchar(50) not null,
  cpf varchar(20) not null,
  rg varchar(20) not null,
  nome varchar(50) not null,
  telefone varchar(20) not null,
  cidade varchar(50) not null,
  primary key(id_usuario))";
  mysql_query($sql,$con);
  $sql="create table if not exists reserva(
    id_reserva int(8) auto_increment,
    data varchar(20) not null,
    horario varchar(50) not null,
    consessionaria varchar(50) not null,
    id_usuario int(8) not null,
    id_carro int(8) not null,
    foreign key(id_carro) references carros(id_carro),
    foreign key(id_usuario) references usuario(id_usuario),
    primary key(id_reserva))";
  mysql_query($sql,$con);
    $sql="create table if not exists carros(
    id_carro int(8) auto_increment,
    Nome_carro varchar(50) not null,
    Marca varchar(50) not null,
    Modelo varchar(50) not null,
    Cor varchar(50) not null,
    Ano_carro int(8) not null,
    id_adm int(8) not null,
    foreign key(id_adm) references adm(id_adm),
    primary key(id_carro))";
  mysql_query($sql,$con);
?>
</BODY>
</HTML>

1 answer

0


Good first I see that you are still using the mysql functions and you better update your whole script to mysqli or to Pdo which is what I recommend from a search later to know why, because the mysql functions no longer exist in PHP or have been removed, now answering your YES question as you are using mysql from a look at this link right here you will find a search system done in mysql as you want

Browser other questions tagged

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