I can not enter data in the mysql database using a php form

2

I have a user registration form, and when I execute it, it tells me that it has been connected correctly, and that the user has registered, but when entering mysqladmin , there is no record in the database.

I have the following form code:

<?php
include 'conexiongeografs.php';
$username =$_POST["Nom"];
$apellidos =$_POST["Cognoms"];
$municipi =$_POST["Municipi"];
$comarca =$_POST["Comarca"];
$email =$_POST["Email"];
$ambit =$_POST["ambit professional"];
$especialitzacio =$_POST["especialitzacio"];
$comment =$_POST["comment"];
$insertar = "INSERT INTO jos_users1(Nom,Cognoms,Municipi,Comarca,Email,ambit professional,especialitzacio,comment) VALUES ('$username','$apellidos','$municipi','$comarca','$email','$ambit','$especialitzacio','$comment')";

$resultado = mysqli_query($conexion,$insertar);
if(!$resultado) {
        echo 'usuari registrat correctament';
    }
    else
    {
        echo 'error al registrarse ';   
    }


mysqli_close($conexion);
?>

East of the connection:

<?php
$conexion =mysqli_connect("localhost","geografs_geo","geo020908");
if (!$conexion) {
echo 'erroral conectar a la base de datos';
}
else {
echo 'conectado a la base de datos';
}

... and finally, the one in the form:

<form action="registreobert.php" method="post" role="form">
<div class="form-group">
<label for="username">Nom</label>
    <input type="text" class="form-control" id="username"
           placeholder="Introdueix el teu nom">
</div>
<div class="form-group">
<label for="apellidos">Cognoms</label>
    <input type="text" class="form-control" id="apellidos"
           placeholder="Introdueix el teu cognom">
</div>
<div class="form-group">
<label for="municipi">Municipi</label>
    <input type="text" class="form-control" id="municipi"
           placeholder="Introdueix el teu municipi">
</div>
<div class="form-group">
<label for="comarca">Comarca</label>
    <input type="text" class="form-control" id="comarca"
           placeholder="Introdueix la teva comarca">
</div>
<div class="form-group">
<label for="email">Email</label>
    <input type="email" class="form-control" id="email"
           placeholder="Introdueix el teu email">
</div><br>
 <div class="form-group">
<select class="form-control" name="ambit professional" id="ambit">
<option>ÀMBIT PROFESSIONAL</option>
<option>ADMINISTRACIÓ</option>
<option>CONSULTORIA</option>
<option>RECERCA</option>
<option>EDUCACIÓ</option>
<option>ALTRES</option>
</select></div><br>
<div class="form-group">
<select class="form-control" name="especialitzacio" id="especialitzacio">
  <option>ESPECIALITZACIÓ</option>
  <option>PLANIFICACIÓ TERRITORIAL I URBANÍSTICA</option>
  <option>MEDI AMBIENT, PAISATGE, PATRIMONI NATURAL</option>
  <option>TECNOLOGIES DE LA INFORMACIÓ GEOGRÀFICA</option>
  <option>DESENVOLUPAMENT SOCIOECONÒMIC TERRITORIAL, TURISME</option>
  <option>EDUCACIÓ, RECERCA, SOCIETAT DEL CONEIXEMENT</option>
  <option>PREVENCIÓ DE RISCOS</option>
  <option>MOBILITAT I TRANSPORT</option>
</select></div>
<br>
 <div class="form-group">
  <label for="comment">Altres especialitzacions (especificar):</label>
  <textarea class="form-control" rows="3" id="comment"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Enviar</button>
</div>
</form>

I paste the form codes again:

Nom      Cognoms      Municipi      Region      E-mail     
  ÀMBIT PROFESSIONAL ADMINISTRATION CONSULTANCY RECEIVE EDUCACIÓ ALTRES
  SPECIALIZATION   PLANIFICACIÓ TERRITORIAL I URBANÍSTICA   MEDI AMBIENT, PAISATGE, PATRIMONI NATURAL   TECHNOLOGIES OF GEOGRAPHIC INFORMATION   DESENVOLUPAMENT SOCIOECONÒMIC TERRITORIAL, TURISME   EDUCACIÓ, RECERCA, SOCIETAT DEL CONEIXEMENT   PREVENTED RISKS   MOBILITAT I TRANSPORT
    Altres specialitzacions (specify):    Submit

the code

still bad I leave you the structure of the database in case this was the problem

id  int(11)         No  Ninguna AUTO_INCREMENT  Cambiar Cambiar Eliminar Eliminar



Primaria Primaria
Único Único
Índice Índice
Más

2   username    varchar(20) utf8_spanish_ci     No  Ninguna     Cambiar Cambiar Eliminar Eliminar   

Primaria Primaria
Único Único
Índice Índice
Más

3   apellidos   varchar(20) utf8_spanish_ci     No  Ninguna     Cambiar Cambiar Eliminar Eliminar   

Primaria Primaria
Único Único
Índice Índice
Más

4   municipi    varchar(30) utf8_spanish_ci     No  Ninguna     Cambiar Cambiar Eliminar Eliminar   

Primaria Primaria
Único Único
Índice Índice
Más

5   comarca varchar(30) utf8_spanish_ci     No  Ninguna     Cambiar Cambiar Eliminar Eliminar   

Primaria Primaria
Único Único
Índice Índice
Más

6   email   varchar(20) utf8_spanish_ci     No  Ninguna     Cambiar Cambiar Eliminar Eliminar   

Primaria Primaria
Único Único
Índice Índice
Más

7   ambit   varchar(60) utf8_spanish_ci     No  Ninguna     Cambiar Cambiar Eliminar Eliminar   

Primaria Primaria
Único Único
Índice Índice
Más

8   especialitzacio varchar(60) utf8_spanish_ci     No  Ninguna     Cambiar Cambiar Eliminar Eliminar   

Primaria Primaria
Único Único
Índice Índice
Más

9   comment text
    
asked by gvaquer 27.10.2016 в 18:59
source

4 answers

1

Here I send you the corrected form and the insert : "New: As this code is, you should have the whole form."

 <form action="aqui va el nombre de la ruta donde esta el codigo del insert" method="POST">
<div class="rows">
    <label for="nom">nom</label>
    <input type="text" class="form-control" name="nom" placeholder="Introdueix la teva nom">
</div>
<div class="rows">
    <label for="Cognoms">Cognoms</label>
    <input type="text" class="form-control" name="Cognoms" placeholder="Introdueix la teva Cognoms">
</div>
<div class="rows">
    <label for="Municipi">Municipi</label>
    <input type="text" class="form-control" name="Municipi" placeholder="Introdueix la teva Municipi">
</div>
<div class="rows">
    <label for="comarca">Comarca</label>
    <input type="text" class="form-control" name="comarca" placeholder="Introdueix la teva comarca">
</div>
<div class="rows">
    <label for="email">Email</label>
    <input type="email" class="form-control" name="email"  placeholder="Introdueix el teu email">
</div>
<br>
<div class="rows">
  <select class="form-control" name="ambit_professional" id="ambit">
  <option>ÀMBIT PROFESSIONAL</option>
  <option>ADMINISTRACIÓ</option>
  <option>CONSULTORIA</option>
  <option>RECERCA</option>
  <option>EDUCACIÓ</option>
  <option>ALTRES</option>
  </select>
</div>
<br>
<div class="rows">
<select class="form-control" name="especialitzacio" id="especialitzacio">
  <option>ESPECIALITZACIÓ</option>
  <option>PLANIFICACIÓ TERRITORIAL I URBANÍSTICA</option>
  <option>MEDI AMBIENT, PAISATGE, PATRIMONI NATURAL</option>
  <option>TECNOLOGIES DE LA INFORMACIÓ GEOGRÀFICA</option>
  <option>DESENVOLUPAMENT SOCIOECONÒMIC TERRITORIAL, TURISME</option>
  <option>EDUCACIÓ, RECERCA, SOCIETAT DEL CONEIXEMENT</option>
  <option>PREVENCIÓ DE RISCOS</option>
  <option>MOBILITAT I TRANSPORT</option>
</select></div>
<br>
 <div class="rows">
  <label for="comment">Altres especialitzacions (especificar):</label>
  <textarea class="form-control" rows="10" cols="30" id="comment" name="comment"></textarea>
</div>
<div class="rows">
<button type="submit" class="btn btn-primary">Enviar</button>
</div>
</form>

When using a form without javascript, you must have the name attribute in all the input that you want to save.

The execution file would be like this:

<?php
include 'conexiongeografs.php';
$username =$_POST["nom"];
$apellidos =$_POST["cognoms"];
$municipi =$_POST["municipi"];
$comarca =$_POST["comarca"];
$email =$_POST["email"];
$ambit =$_POST["ambit"];
$especialitzacio =$_POST["especialitzacio"];
$comment =$_POST["comment"];
$insertar = "INSERT INTO jos_users1(nom,cognoms,municipi,comarca,email,ambit,especialitzacio,comment) VALUES ('$username','$apellidos','$municipi','$comarca','$email','$ambit','$especialitzacio','$comment')";

$resultado = mysqli_query($conexion,$insertar);
if(!$resultado) {
        echo 'usuari registrat correctament';
    }
    else
    {
        echo 'error al registrarse ';   
    }


mysqli_close($conexion);
?>

If you look at the data that you shared of the form, there are some data that you try to save in the table, I do not know if you omitted it in the intended post or really need to add them to the form,.

The other thing is that in programming variable names must be used according to the rules camelCase or underscore .

Connection data:

$conexion =mysqli_connect("localhost","usuario","contraseña",'base_de_datos');
if (!$conexion) {
echo 'erroral conectar a la base de datos';
}
else {
echo 'conectado a la base de datos';
}
    
answered by 27.10.2016 / 19:36
source
0

Use back-links (') when you come across a field that includes spaces (in your case the ambit professional field)

INSERT INTO jos_users1(Nom,Cognoms,Municipi,Comarca,Email,'ambit professional',especialitzacio,comment) VALUES ('$username','$apellidos','$municipi','$comarca','$email','$ambit','$especialitzacio','$comment')
    
answered by 27.10.2016 в 19:18
0

The way to send data from a FORM (html tag) is through the name, each input must have a NAME ASI:

<input type="text"  id="username" name="username">

So you send the 'username'.

    
answered by 27.10.2016 в 19:34
0

Closer to the response of @kira is that all fields in the form lack the name attribute and the value of that attribute must be the same one that you are using in the post variable. As an example:

Form field:

<input type="text" class="form-control" name=""Nom" id="username"
           placeholder="Introdueix el teu nom">

Value obtained in PHP:

$username = $_POST["Nom"];

This way you get the value of the Nom

field     
answered by 27.10.2016 в 19:40