How to populate the database tables with the textbox data

0

In the external PHP file I have the following code:

    <!DOCTYPE html>
     <?php

    $servername = "localhost";
    $username = "isabelso_isabel";
    $password = "password";
    $dbname = "isabelso_db";

    $dbconn = mysql_connect($servername, $username, $password, $dbname);
    mysql_select_db($dbname);

    if (isset($_POST['botao_registo']))
    {
        $name = $_POST['nome'];
        $nif = $_POST['nif'];

         $sql = ("INSERT INTO Paciente (nome, id_paciente ) VALUES ('".$nome."', ' ".$nif."')");

    if(mysql_query($query))
 {
echo "<script>alert('INSERTED SUCCESSFULLY');</script>";
}
else
 {
 echo "<script>alert('FAILED TO INSERT ".mysql_error()."');</script>";
        }    
     }

The name of the table is Paciente and the attributes are: nome, id_paciente

The textbox names are nome, nif .

I already have the other one:

    <body>

    <?php
          include("user_insert.php");
    ?>    
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
        <p style="font-size: 15px"> <font face= "Verdana">
            &nbsp Nome:<INPUT type="text" name="nome" size=50>
                &nbsp &nbsp &nbsp Data de Nascimento: <input type="date" name="bday" > <br><br>
            &nbsp NIF:<INPUT type="number" name="nif" size=10> &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
            &nbsp Telem&oacute;vel:<INPUT type="number" name="telemovel" size=10> <br><br>
            &nbsp Email:<INPUT type="email" name="email" size=40><br><br>
            &nbsp Password:<INPUT type="password" name="senha" size=30><br><br>
            &nbsp Password:<INPUT type="password" name="senha" size=30><br><br>
            &nbsp Alergias/Doen&ccedil;as Conhecidas:<INPUT type="text" name="doenças" size=50> <br><br>
            &nbsp Observa&ccedil;&otilde;es:<br>&nbsp <textarea name="observacoes" rows="5" cols="90" maxlength="500"></textarea>
            </p></font>
    <br>
    <br>
    <br>
    <br>
    <br> 

   <div id="botao">
      <input type="submit" name="botao_registo" value="ENVIAR DADOS" class=botaoEnviar />

There will be something wrong with the PHP code because when I fill in the text box and press the button to send to the database it gives the error: "FAILED TO INSERT", query was empty.

    
asked by anonymous 06.06.2017 / 11:17

1 answer

0

The problem is that the $query variable does not exist. The variable you want to insert is $sql , which contains query . Also I believe that it is not necessary to put query in square brackets, this will generate error. Then it would look like this:

... codigo anterior ...

$sql = "INSERT INTO Paciente (nome, id_paciente ) VALUES ('".$nome."', ' ".$nif."')"; // retirei os colchetes

        if(mysql_query($sql)) // aqui esta o erro

... resto do código ...
    
06.06.2017 / 21:45