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

Asked

Viewed 8,422 times

0

This is the form to enter 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 this here is the data reader with the command to enter 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

  • If you do not define the column names in the clause insert, you need to define the value of all of them, including the id.

  • Here is a simple example: https://wiki.locaweb.com.br/pt-br/Formul%C3%A1rio_php_%2B_Mysql .

  • I’ll take a look, thank you very much!

  • Ah, but if the id is auto increament as I leave it at the time of inserting? empty?

  • If you do not specify column names, you do. In this case, simply put the value as default.

  • do not need to put the id referring to the query I suggested INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);

Show 1 more comment

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 names of the columns that will be entered values, you must provide the value of all of them. As you have not set, you must provide the column value id, whereas she is the only one absent. Supposing that she is auto increment, just set the value of the same as 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 inserted is sex:

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

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

$sexo = $_POST["sex"];

Browser other questions tagged

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