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 ?
– Rovann Linhalis
Yes yes. It’s just that when I posted, I put it together. One is the register and the other is Insert
– Thun An Freitas Chiu
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?
– Woss
By the way, what PHP code would that be? Is the file itself or is it something like
array ( [nome] => ..., [rua] => ... )
?– Woss
I put it in the root folder of Xampp and Wampp and it didn’t work. And in localhost the folder appears.
– Thun An Freitas Chiu
Can you send me your e-mail so I can send you the code? Because theoretically, apache is working on Wampp and/or Xampp
– Thun An Freitas Chiu
When you send the POST it returns with the raw PHP code of "insert.php"? This?
– Thiago
On the connection to the database, there is the function
mysqul_error
? The right thing would bemysql_error
. By the way, the correct thing was for you not even to be using these functionsmysql_*
, they are obsolete in PHP 5 and have been removed from PHP 7; ideally usemysqli_*
or PDO.– Woss
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?– Woss
'Cause first I’d test, if it worked I’d do the others.
– Thun An Freitas Chiu
This must be generating in your query. Use the function correctly
mysql_error
to check, including within aelse
when performingmysql_query
.– Woss
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
– flourigh
I am mega beginner, so several mistakes =(
– Thun An Freitas Chiu
so if you want I can help you with many improvements in this script
– flourigh
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.
– Thun An Freitas Chiu
However, I ran behind to learn some HTML, CSS and PHP. And I made a form to show in practice. My email: [email protected]
– Thun An Freitas Chiu