Problem sending PHP to Mysql

Asked

Viewed 46 times

0

Good night. I have a big problem and I can’t fix it. I created a form to register and when I click send, instead of sending, appears the PHP code. I already installed XAMPP and then WAMPP, but it doesn’t work at all.

HTML:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Faça o seu cadastro</title>
    <style>
            td{border:1px solid;}
            body{text-align: center}
            table{al}
    </style>
</head>
<body>
    <form action="inserir.php" method="POST">
        <input type="hidden" name="tipo" value="cadastro">
        <h1>CADASTRO</h1>
        <table align="center">
            <tr><td align="left">Nome: <input type="text" name="nome"></td></tr>
            <tr><td align="left">Rua: <input type="text" name="rua"></td></tr>
            <tr><td align="left">Cidade: <input type="text" name="cidade"></td></tr>
            <tr><td align="left">Estado: <input type="text" name="estado"></td></tr>
            <tr><td align="left">Idade: <input type="number" name="idade" min="0"></td></tr>
            <tr><td align="left">Peso: <input type="number" name="peso" step="any"></td></tr>
            <tr><td align="left">Tamanho: <input type="number" name="tamanho" step="any"></td></tr>
            <tr><td align="left">Nacionalidade: <input type="text" name="nacionalidade"></td></tr>
            <tr>
                <td align="left">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
                </td>
            </tr>
            <tr><td align="left">Sexo:<br>
                    <input type="radio" name="sexo" value="M">Masculino
                    <input type="radio" name="sexo" value="F">Feminino
                </td>
            </tr>
        </table>
        <table align="center" style="margin-top: 5px">
            <tr>
                <td align="right">
                    <button type="reset">Apagar</button>
                    <button type="submit">Enviar</button>
                </td>
            </tr>
        </table>
    </form> 
</body>
</html>

Filing cabinet 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 (mysqul_error());
mysql_select_db($banco) or die (mysqul_error());

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

    if (mysql_query($inserir)) {
        echo "Dados inseridos com sucesso!";
    }
}
elseif($_POST['tipo'] == 'buscar')
{
    $peso   = get_peso($_POST['peso']);
    $altura = get_altura($_POST['altura']);
    $cabelo = "cabelo = " . $_POST['corCabelo'];
    $sexo   = "sexo = " . $_POST['sexo'];

    $select = mysql_query("select * from cadastro where $peso and $altura and $cabelo and $sexo");

    $array_busca = array();

    while($ln = mysql_fetch_assoc($select))
    {
        $array_busca[] = $ln;
    }

    if(!empty($array_busca))
    {
        foreach($array_busca as $pessoa)
        {
            ?>
            <table>
                <tr>
                    <td><?php echo $pessoa['nome']?></td>
                    <td><?php echo $pessoa['idade']?></td>
                    <td><?php echo $pessoa['cidade'] . "/" . $pessoa['estado']?></td>
                </tr>
            </table>
            <?php
        }
    }
}

function get_peso($peso)
{
    switch ($peso)
    {
        case 'menor_40':    return 'peso <= 40';
        case 'entre_40_50': return 'peso >=40 and peso <= 50';
    }
}

function get_altura($altura)
{
    switch ($altura)
    {
        case 'menor_14':    return 'tamanho <= 1.4';
        case 'entre_14_15': return 'tamanho >=1.4 and tamanho <= 1.5';
    }
}
?>
  • are two different files, right ?

  • Yes yes. It’s just that when I posted, I put it together. One is the register and the other is Insert

  • Did you make sure the Apache server was started correctly? You are running these files in the server root directory or in any subdirectory of this?

  • By the way, what PHP code would that be? Is the file itself or is it something like array ( [nome] => ..., [rua] => ... )?

  • I put it in the root folder of Xampp and Wampp and it didn’t work. And in localhost the folder appears.

  • Can you send me your e-mail so I can send you the code? Because theoretically, apache is working on Wampp and/or Xampp

  • When you send the POST it returns with the raw PHP code of "insert.php"? This?

  • On the connection to the database, there is the function mysqul_error? The right thing would be mysql_error. By the way, the correct thing was for you not even to be using these functions mysql_*, they are obsolete in PHP 5 and have been removed from PHP 7; ideally use mysqli_* or PDO.

  • And in the insert you just specified the columns "name, street, city, state", but passed the values of "name, street, city, state, age, weight, size, nationality, corCabelo, sex". Why is that?

  • 'Cause first I’d test, if it worked I’d do the others.

  • This must be generating in your query. Use the function correctly mysql_error to check, including within a else when performing mysql_query.

  • boy, as well as many colleagues, I’m perplexed by your code, you should remove the print_r($post) as well as change a lot for example, Pdo or sqli, I have a registration function that uses both Pdo and sqli and aiming over everything security, in case you want to check to see if you like, send me message

  • I am mega beginner, so several mistakes =(

  • so if you want I can help you with many improvements in this script

  • Of course I would. I’m a beginner. I’m in my third period of Computer Science. The comic book teacher passed a paper, so we just presented a problem and created some tables and inserted.

  • However, I ran behind to learn some HTML, CSS and PHP. And I made a form to show in practice. My email: [email protected]

Show 11 more comments

1 answer

0

1-Then change the if($_POST['tipo'] == 'cadastro.html') for if($_POST['tipo'] == 'cadastro') because in the value of the type field, it is only 'register'

2-As for the possibility of being problem on the web server, try installing apache or vertrigo to do tests

3-Your INSERT is wrong... you are giving Insert in more values than columns:

-> Only name, street, city and state.... and in the values you give weight, age, size, nationality, which are not in charge of the INSERT

insert into cadastro(nome,rua,cidade,estado) values ('$_POST[nome]','$_POST[rua]','$_POST[cidade]',"
        . "'$_POST[estado]','$_POST[idade]','$_POST[peso]','$_POST[tamanho]','$_POST[nacionalidade]',"
        . "'$_POST[corCabelo]','$_POST[sexo]')";
  • So, this part was commented, I took it just because I wasn’t picking it up.

  • I took out the . html and instead of appearing the php code, there was a blank page. However I went to the database and added nothing.

  • another error found, check the SQL command

  • Then I would first test, then add the rest. In case, if I give more Insert than columns, it will not take?

  • no... you have to put which columns and the respective values

  • @Thunanfreitaschiu managed to solve ?

  • I did it! It worked. Thank you. Now I do not know how to return. The search code is not working.

  • SELECT SQL syntax is also wrong... Where has to be: Select * from [Table] Where [column name_name]=[desired value_value]

Show 3 more comments

Browser other questions tagged

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