How to query in Mysql and PHP database

Asked

Viewed 7,521 times

0

I made a registration form that adds the information in the Mysql database. But I created a query form and would like to know how to return information from the database.

Insert.php

<?php
error_reporting (E_ALL & ~ E_NOTICE & ~ E_DEPRECATED);
$host  = "localhost";
$user  = "root";
$pass  = "";
$banco = "relacionamentos";

//echo "<pre>";
//print_r($_POST);
//echo "</pre>";
//exit;

$conexao = mysql_connect($host,$user,$pass) or die (mysql_error());
mysql_select_db($banco) or die (mysql_error());

if($_POST['tipo'] == 'cadastro')
{
    $inserir = "insert into cadastro(nome,rua,cidade,estado,idade,peso,tamanho,nacionalidade,cabelo,sexo) values ('$_POST[nome]','$_POST[rua]','$_POST[cidade]','$_POST[estado]','$_POST[idade]','$_POST[peso]'"
        . ",'$_POST[tamanho]','$_POST[nacionalidade]','$_POST[corCabelo]','$_POST[sexo]')";
?>

HTML:

<!DOCTYPE html>

<html>
    <head>
        <meta charset="utf-8">
        <title>Busca da Pessoa Ideal</title>
        <style>
            td{border:1px solid;}
        </style>
    </head>
    <body>
        <form>
            <h1 align="center">Caracteristicas do seu par ideal</h1>
                <table align="center">
                    <tr><td>Idade Minima: <input type="number" name="idadeMin" min="18"><br>
                    Idade Maxima: <input type="number" name="idadeMax" min ="18" max="100"><br>
                    <input type="checkbox" name="idadeQualquer">Qualquer Idade<br></td></tr>
                    <tr><td>Peso Minimo: <input type="number" name="pesoMin" step="any" min="0"><br>
                    Peso Maximo: <input type="number" name="pesoMax" step="any" min="0"><br>
                    <input type="checkbox" name="pesoQualquer">Qualquer Peso<br></td></tr>
                    <tr><td>Tamanho:
                    <select name="tamanho" id="tam">

                        <option value="1314">Entre 1.30 e 1.40</option>
                        <option value="1415">Entre 1.40 e 1.50</option>
                        <option value="1516">Entre 1.50 e 1.60</option>
                        <option value="1617">Entre 1.60 e 1.70</option>
                        <option value="1718">Entre 1.70 e 1.80</option>
                        <option value="18">Maior que 1.80</option>
                    </select>
                    <br>
                    <input type="checkbox" name="tamanhoQualquer">Qualquer Tamanho</td></tr>
                    <br>
                    <tr><td>Cor do cabelo: <br>
                    <input type="radio" name="corCabelo" value="Preto">Preto
                    <input type="radio" name="corCabelo" value="Loiro">Loiro
                    <input type="radio" name="corCabelo" value="Ruivo">Ruivo
                    <input type="radio" name="corCabelo" value="Outros">Outros<br>
                    <input type="checkbox" name="cabeloQualquer">Qualquer Cabelo</td></tr><br>
                    <tr><td>Sexo: <br>
                    <input type="radio" name="sexo" value="M">Masculino
                    <input type="radio" name="sexo" value="F">Feminino
                    <input type="radio" name="sexo" value="Y">Ambos
                    <br>
                    <button type="input" >Enviar</button>
                    <button type="reset">Apagar</button>
                </table><br>
        </form>
    </body>
</html>

1 answer

0

According to the above scenario to get data from the database you must execute a query query (SELECT).

Suppose you want to simply refer to the name and street of the table register you can use the following script (source):

<?php

// Conectando
$link = mysql_connect('localhost', 'root', 'admin')
        or die('Could not connect: ' . mysql_error());
echo 'Connected successfully' . "\n";
// Selecionando banco de dados
mysql_select_db('test') or die('Could not select database');

// Executando consulta SQL
$query = 'SELECT nome, rua FROM cadastro';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

// Imprimindo resultados em HTML
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
        echo "\t<tr>\n";
        foreach ($line as $col_value) {
                echo "\t\t<td>$col_value</td>\n";
        }
        echo "\t</tr>\n";
}
echo "</table>\n";

//  Liberando memória
mysql_free_result($result);

// Fechando conexão
mysql_close($link);
  • In this case, I wanted when the user typed "Submit", to return the name of the person with that certain characteristics that he passed in the form. You can show me in code how I do it?

  • You don’t make a canoe without wood. Have you considered researching your problem in a divided way? It seems you have a gigantic problem. Analyze more deeply and divide to conquer

Browser other questions tagged

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