Good, I'm doing an online course that is somewhat outdated, and I'm having some problems. I'm trying to edit the data on a fictional bus website using a form, but I have a problem. I attached the code:
From one page you can see all the buses, and this works perfectly. Each bus has an "edit" button, which leads to a form to change the data. This is the form in PHP:
<?php
include("funciones.php");
if (isset($_GET["id"]))
{
$id = $_GET["id"];
}
$resultado = cargarAutobusEditar($id);
$nombre = $resultado[0];
$color = $resultado[1];
$capacidad =$resultado[2];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Editar Autobús</title>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<link href="estilos.css" rel="stylesheet" type="text/css" />
</head>
<body>
<header>
<h1>
<img src="images/logo.png" alt="Logo" />
</h1>
<h2>Editar Autobús</h2>
</header>
<form action="funciones.php" method="post">
<input type="hidden" name="id" value="<?php $id ?>" />
<label for="Nombre">Nombre</label>
<input type="text" name="Nombre" value="<?php echo $nombre ?>" id="Nombre" />
<label for="Color">Color</label>
<input type="text" name="Color" value="<?php echo $color ?>" id="Color" />
<label for="Capacidad">Capacidad</label>
<input type="text" name="Capacidad" value="<?php echo $capacidad ?>" id="Capacidad" />
<input type="submit" name="editar" value="Guardar" class="boton_editar"/>
<a href="funciones.php?borrar=<?php echo $id ?>" class="borrar">Eliminar</a>
<div class="clearfix"></div>
</form>
<article id="ver_autobuses">
<img src="images/autobus.png" alt="Imagen autobús" />
</article>
</body>
</html>
In this form the data of each bus is collected thanks to the function cargaAutobusEditar ($ id), which is the following:
function cargarAutobusEditar($id){
$consulta = "SELECT * FROM autobuses WHERE ID = '" . $id . "'";
$valor = conexionBD($consulta);
while($row = mysqli_fetch_assoc($valor))
{
$nombre = $row["Nombre"];
$color = $row["Color"];
$capacidad = $row["Capacidad"];
}
$resultado = Array($nombre, $color, $capacidad);
return $resultado;
}
This works correctly, the data of the corresponding bus are shown on the screen, in each input of the form. The problem occurs when I'm going to edit, because I change the data and when I click on the edit button, this function is called:
<?php
if (isset($_POST["editar"]))
{
editarAutobus();
}
function editarAutobus(){
$id = $_POST['ID'];
$nombre = $_POST['Nombre'];
$color = $_POST['Color'];
$capacidad = $_POST['Capacidad'];
$consulta = "UPDATE autobuses SET Nombre='" . $nombre . "', Color='" . $color. "', Capacidad='" . $capacidad . "' WHERE ID='" .$id . "'";
conexionBD($consulta);
header('Location:editar_autobuses.php?id='.$id);
}
?>
By clicking on the button to save the changes and call this function, the form fields are empty ...
I have another script to register buses, passing the MySQL query through the same process, and it works correctly, so I understand that the connection to the database works correctly. Anyway, I leave the functions:
function conexionBD($consulta){
$dbLocal = new DBMySql("localhost", "root", "", "bus", 3306);
$valor = $dbLocal->setQuery($consulta);
return $valor;
}
<?php
class DBMySql extends BaseDeDatos
{
protected $conexion;
public function __construct($servidor, $usuario, $password, $db, $puerto = 3306){
parent::__construct($servidor, $usuario, $password, $db, $puerto, "mysql");
$this->conexion = mysqli_connect($this->servidor . ":" . $this->puerto, $this->usuario, $this->password);
mysqli_select_db($this->conexion, $this->db);
}
public function setQuery($query){
//$query = mysqli_real_escape_string($this->conexion, $query);
return $this->idConsulta = mysqli_query($this->conexion, $query);
}
?>
Any ideas that you may be failing?
Thank you and greetings.
EDITED:
Ok, thank you very much for your help, trying to adapt the script that I already had written, I used the links that you attach to me and I have edited this part of the code:
function conexionBD($consulta){
//$dbLocal = new DBMySql("localhost", "root", "", "bus", 3306);
//$valor = $dbLocal->setQuery($consulta);
$mipdo=new DbPDO();
$valor = $mipdo->query($consulta);
return $valor;
}
With this I access the created object that you have passed me, and I get any query passed through that method. For now I will not use the bind_params because I want it to simply work, then I will add it as indicated. The problem is that some functions have stopped working, I guess because it used the "style of procedures" and not the "object-oriented". The error that is shown to me when using this function:
function verAutobuses(){
$consulta = "SELECT * FROM autobuses";
$valor = conexionBD($consulta);
$resultado ="";
while ($row = mysqli_fetch_assoc($valor))
{
$resultado .= "<h3>Nombre: <span>" . $row["Nombre"] . "<span><a href='editar_autobuses.php?id=" . $row["ID"] . "' class='editar'><img src='images/editar.png'></a></h3>";
$resultado .= "<h4>Color: <span>" . $row["Color"] . "</span></h4>";
$resultado .= "<h4>Capacidad: <span>" . $row["Capacidad"] . "</span></h4>";
}
return $resultado;
}
And the error that returns me is the following:
Warning: mysqli_fetch_assoc () expects parameter 1 to be mysqli_result, array given in C: \ xampp \ htdocs \ course \ functions.php on line 72
I've been looking at the documentation, and I thought that by switching to the object-oriented style I should write something like this:
function verAutobuses(){
$consulta = "SELECT * FROM autobuses";
$valor = conexionBD($consulta);
$resultado ="";
while ($row = $valor->fetch_assoc())
{
$resultado .= "<h3>Nombre: <span>" . $row["Nombre"] . "<span><a href='editar_autobuses.php?id=" . $row["ID"] . "' class='editar'><img src='images/editar.png'></a></h3>";
$resultado .= "<h4>Color: <span>" . $row["Color"] . "</span></h4>";
$resultado .= "<h4>Capacidad: <span>" . $row["Capacidad"] . "</span></h4>";
}
return $resultado;
}
But he returns this error to me, that I do not understand it the truth:
Fatal error: Uncaught Error: Call to a member function fetch_assoc () on array in C: \ xampp \ htdocs \ course \ functions.php: 70 Stack trace: # 0 C: \ xampp \ htdocs \ course \ ver_autobuses.php (4): viewBuses () # 1 {main} thrown in C: \ xampp \ htdocs \ course \ functions.php on line 70
Thank you very much for your answers, I value them very much considering that I am starting and this seems to me an impossible world.