Insert data in MySQL mediating form and PHP

0

Good afternoon, I am trying to do an exercise that consists of a bus manager, trying to register new buses through a form. I attached the code and my doubts to see if you can help me:

The form where the data is entered:

<form action="funciones.php" method="post">

        <label for="Nombre">Nombre</label>
        <input type="text" name="Nombre" value="" id="Nombre" />

        <label for="Color">Color</label>
        <input type="text" name="Color" value="" id="Color" />

        <label for="Capacidad">Capacidad</label>
        <input type="text" name="Capacidad" value="" id="Capacidad" />

        <input type="submit" name="alta" value="Dar de alta"/>

        <div class="clearfix"></div>

</form> 

The functions.php file and the function to insert a new bus:

function altaAutobuses(){ 
    $nombre = $_POST["Nombre"]; 
    $color = $_POST["Color"]; 
    $capacidad = $_POST["Capacidad"]; 
    $autobus = new Autobuses($nombre, $color, $capacidad); 
    conexionBD($autobus->consulta()); 
    header('Location:ver_autobuses.php'); 
}  

Here the class bus, to see the query function () that is the one that passes the query to MySQL:

class Autobuses{ 

private $nombre; 
private $color; 
private $capacidad; 

    function __construct($nombre, $color, $capacidad){ 
        $this->nombre->$nombre; 
        $this->color->$color; 
        $this->capacidad->$capacidad; 
    } 

    public function consulta(){ 
        $consulta = "INSERT INTO autobuses (Nombre,Color,Capacidad) VALUES ('".$this->nombre."','".$this->color."','".$this->capacidad."')"; 
        return $consulta; 
    } 
}  

The point is that I have another file to see the buses that are in the database, and if that shows the buses on the screen, so the connection to the database works, and if I add a new bus in the database, PHPMyAdmin also shows it, so there is no doubt that the connection to the database goes well. I do not understand what is failing, I have tried to change the variables of the query () function of the bus and put words directly, but it does not connect to the database, instead, I put the request "SELECT * FROM buses" and if that shows me the contents of the database ....

Also, I try to do an echo with the variables that happened through the form, and write them on the screen, so you are receiving them. I understand that the problem is in the writing of new data in the database of PHPMyAdmin ...

Thank you and greetings.

    
asked by Viturbiko 09.11.2017 в 19:49
source

2 answers

1

In your constructor you should assign a value to the variables, making use of the operator =

  function __construct($nombre, $color, $capacidad){ 
        $this->nombre = $nombre; 
        $this->color =$color; 
        $this->capacidad = $capacidad; 
    } 
    
answered by 09.11.2017 в 20:00
0

I noticed that operator error =, but it was not the problem. As A.Cedano said (I do not quite know how to quote him) I was reviewing conexionBD($autobus->consulta()); and I realized the following:

public function setQuery($query){
        //$query = mysqli_real_escape_string($this->conexion, $query);
        return $this->idConsulta = mysqli_query($this->conexion, $query);
    }

The error was in the line that I left commented, I used the mysqli_real_escape_string function as indicated in the course I was doing PHP, and you see that the comment was solved the error. I do not quite understand the effect of this function despite looking at the documentation (I do not quite understand the term "escape"), but I understood that it was used to avoid malicious code when doing MySQL queries. Is there an alternative? Do you know why I could be giving the error?

Thank you very much to both of you for answering so fast.

    
answered by 09.11.2017 в 20:17