-2
I am developing an HTML site with the primary function of registering patients, employees, diseases among others.
All HTML pages are done, as is the databank that has already been modeled in phpMyAdmin. However, I am having trouble sending the form of the pages to the database.
Basically what I already have:
- All pages in HTML
- the file to make the connection to my database, the "connectionBD.php"
- the files to receive the forms in HTML and insert in the database, I will use as an example the file "receive.php" that will receive the form of the page "cadDoenca.html.
connectionBD.php:
<?php
$servidor = "localhost";
$dbname = "mydb";
$dbusuario = "root";
$dbsenha = "";
$conn = mysqli_connect($servidor, $dbusuario, $dbsenha, $dbname);
if (!$conn){
die ("Conexao falhou : ". mysqli_connect_error());
}
?>;
receivedDoenca.php:
<?php include_once ("connectionBD.php");?>
<html>
<body>
<?php
$gravidade = $_POST['gravidade'];
$idDoenca = $_POST['idDoenca'];
$sintomas = $_POST['sintomas'];
$nCientifico = $_POST['nCientifico'];
$conn = mysqli_connect($servidor, $dbusuario, $dbsenha, $dbname);
mysqli_select_db($conn, '$dbname');
$sql = "INSERT INTO doença (gravidade ,idDoenca, sintomas ,nCientifico) VALUES ('$gravidade','$idDoenca','$sintomas' ,'$nCientifico')";
if(mysqli_query($conn, $sql)){
echo "<script>alert('Dados salvos'); window.location = 'cadDoenca.html';</script>";
}
else{
echo "Deu erro: " .$sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>;
</body>
cadDoenca.html (as the code is rather large due to some parts of bootstrap, I will only quote the parts I imagine are influencing, such as the insertion of the form action = "receive.php" and the button"
<form input = "text" action="recebeDoenca.php" method="POST">
<div class="row">
<div class=" col-sm-6 mb-3">
<label for="exampleInputEmail1" class="form-label">ID Doença</label>
<input type="text" name = "idDoenca" class="form-control" placeholder="Informe o ID da doença">
</div>
</div>
Send button of the form:
</form>
<div id="abaixo" class="row justify-content-center">
<div class="col-sm-2" style="margin-left: 50px;">
<a href ="cadDoenca.html" value="1" type = "submit" class="btn btn-primary">Cadastrar</a>
</div>
Apparently no error appears to me, the data just is not entered in the BD.
I made a change to the button section, replacing with
<input type="submit" value="Cadastrar" class="btn btn-primary">
and this returned me the page "receive.php", but the information has not yet gone to my database, I can assume that is an error in my php files? :
------------- SOLVED---------------------
- I ended up discovering that my files were not on the correct server, so it was not working, I put the folders inside the xampp folder (htdocs) and ran perfectly. Doubt solved.
Puts the Submit button inside the "form":
<button>Cadastrar</button>
. Allbutton
within the tagform
is already a button to send the form where it is.– Sam
The same thing happened, when clicking on the button, returns the page of the script in php, and does not send to the database, I will review his modeling and scripts, but I believe everything is ok, I do not understand why this problem
– Kirk
Are you running your project on an apache server or embedded php server? Because if you are just opening your HTML file in the browser without using a php server, the php script will not be interpreted (run).
– Victor Carnaval
That’s right Victor, the files were not being opened on the correct server, after tidying it up, it started working perfectly. Thank you
– Kirk