How to insert data into a MySQL B.D with PHP

0

This is the form for entering data

 <html>
<head>
    <title></title>
    <meta charset="utf-8">
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <form action="cadastrando.php" method="POST">
        <input type="text" name="nome"><br><br>
        <input type="text" name="idade"><br><br><br>
        <input type="radio" name="sex" value="male"> Masculino
        <br>
        <input type="radio" name="sex" value="female"> Feminino<br>
        <input type="text" name="email">
        <input type="password" name="senha"><br><br>
        <input type="text" name="peso"><br><br>
        <input type="text" name="altura"><br>
        <input type="text" name="qntd_exe">
        <input type="submit" value="Cadastrar">
    </form>
</body>
</html>

And here is the data reader with the command to insert the data

<?php

$link = mysqli_connect("localhost", "root", "", "tcc");

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
?>

<html>
<head>
    <title></title>
    <meta charset="utf-8">

</head>
<body>
<?php

$nome = $_POST['nome'];
$idade = $_POST['idade'];
$email = $_POST['email'];
$senha = $_POST['senha'];
$peso = $_POST['peso'];
$altura = $_POST['altura'];
$imc = ($peso/($altura*$altura));
$qntd_exe = $_POST['qntd_exe'];


$sql = "INSERT INTO usuario VALUES ";
$sql .= "($nome', '$idade', '$email', '$senha', '$peso', '$altura', '$imc', '$qntd_exe', '$sexo')"; 
mysqli_query($link,$sql) or die("Erro ao tentar cadastrar registro");
mysqli_close($link);
echo "Cliente cadastrado com sucesso!";

?>
</body>
</html>

As much as I search and change several codes it does not appear error, but also does not insert the data in B.D

    
asked by anonymous 08.06.2017 / 01:10

1 answer

0

Considering this part of your code:

$nome = $_POST['nome'];
$idade = $_POST['idade'];
$email = $_POST['email'];
$senha = $_POST['senha'];
$peso = $_POST['peso'];
$altura = $_POST['altura'];
$imc = ($peso/($altura*$altura));
$qntd_exe = $_POST['qntd_exe'];


$sql = "INSERT INTO usuario VALUES ";
$sql .= "($nome', '$idade', '$email', '$senha', '$peso', '$altura', '$imc', '$qntd_exe', '$sexo')"; 
mysqli_query($link,$sql) or die("Erro ao tentar cadastrar registro");
mysqli_close($link);

If you do not specify the column names that will be entered values, you must provide the value of all of them. As you have not defined, you must provide the value of the id column, considering that it is the only one missing. Assuming it is auto increment , just set the value of it to DEFAULT :

INSERT INTO usuario VALUES (DEFAULT, ...);

Another error is the lack of a single quote in the name value:

    $sql .= "($nome', '$idade', ...
    //        ^---- Aqui deveria ter uma aspa simples

Finally, the last value that should be entered is sex:

$sql .= "(..., '$imc', '$qntd_exe', '$sexo')";
//                                   ^--- Valor de sexo

However, the variable $sexo is not defined in the program. Judging by your form, I believe that something like:

$sexo = $_POST["sex"];
    
08.06.2017 / 02:33