I have a form where I fill in some user data, then when accepting it sends me to the controller where I have to create a candidate object with the received data and then send that object to a function in the model so that it does an insert. This is the form:
<!DOCTYPE html >
<head>
<meta charset="utf-8">
<link href=\"miestilo.css\" rel=\"stylesheet\" type=\"text/css\" />
<title>Formulario para añadir datos </title>
</head>
<body>
<center><h2>Formulario de altas<h2></center>
<form name="altas" method="POST" action="grabar.php">
<table bgcolor="#E9FFFF" align=center border=2>
<td align="right">Escribe tu D.N.I.: </td>
<td align="left"> <input type="text" name="dni" value="" size=8></td><tr>
<td align="right">Nombre....: </td>
<td align="left"> <input type="text" name="nombre" value="" size=20></td><tr>
<td align="right">Apellidos...: </td>
<td align="left"> <input type="text" name="apellidos" value="" size=15></td></tr>
<tr>
<td align="right">Sexo...:</td>
<td align="left">
<input type="radio" name="sexo" value="m" > Masculino
<input type="radio" name="sexo" value="f" > Femenino
</td>
</select> </tr><tr>
<td align="right">Habla:<br>
(<i>Si habla varios seleccionarlos<br>
pulsando con el mouse encima de <br>
cada uno de ellos con la tecla<br>
<b>Ctrl</b> presionada</i>)</td>
<td align="left"> <SELECT MULTIPLE name="idiomas[]" SIZE=9>
<?php
foreach($arrayIdiomasPersonas as $idioma)
{
echo "<option value='".$idioma["id"]."'>".$idioma["nombre"]."</option>";
}
?>
</select>
</td><tr>
<!--colocamos los botones de enviar y borrar //-->
<td align=center><input type=submit name="accion" value="Enviar"></td>
<td align=center><input type=submit name="accion" value="Cancelar"></td>
</table> </form>
</body>
</html>
And this is the function I have in the model:
public static function insertDatos($candidatoNuevo)
{
//recibe un objeto candidato e inserta los datos en la tabla
}
And this is the Candidate class:
<?php
class Candidato {
protected $dni;
protected $nombre;
protected $apellidos;
protected $sexo;
protected $idiomas;//array de idiomas
public function getDni() {return $this->dni; }
public function getNombre() {return $this->nombre;}
public function getApellidos() {return $this->apellidos;}
public function getSexo() {return $this->sexo;}
public function getIdiomas() {return $this->idiomas;}
public function __construct($pdni,$pnombre,$papellidos,$psexo,$pidiomas)
{
$this->dni = $pdni;
$this->nombre = $pnombre;
$this->apellidos=$papellidos;
$this->sexo=$psexo;
$this->idiomas=$pidiomas;
}
}
?>
I have tried several things in the controller, from creating some variables there for each of the attributes through $_POST and sending them to the function through an sql string and even creating a function that receives the parameters, but as the function asks me for a candidate object , I understand that you have to create a new Candidate in the controller. If the function asked me for the 5 attributes, I would understand how to do it, but asking me for an already created object is what shocks me. Let's see if you can help me and I can get to understand how it works, thank you very much.
Edit: I have gotten the function to insert, but it inserts empty data:
insert function:
public static function insertDatos($candidatoNuevo)
{
$sql5="INSERT INTO candidatos (dni, nombre, apellidos, sexo)
VALUES ('".$pdni."','".$pnombre."','".$papellidos."','".$psexo."')";
return ($resultado=self::ejecutaConsultaAccion($sql5));
}
Save.php in the controller:
<?php
require_once("../model/base.php");
//si inserta
//header ("Location:index.php");
$pdni= $_POST["dni"];
$pnombre = $_POST["nombre"];
$papellidos = $_POST["apellidos"];
$psexo = $_POST["sexo"];
$pidiomas = $_POST["idiomas"];
$existe=ClaseBase::compruebaCandidato($pdni);
if (!$existe)
{
$nuevoUser = new Candidato($pdni,$pnombre,$papellidos,$psexo,$pidiomas);
//print_r($nuevoUser);
ClaseBase::insertDatos($nuevoUser);
echo "<p>El candidato ha sido insertado
correctamente</p>";
}
else
echo "<p>El dni .$pdni ya estaba registrado, inserta
otro.</p>";
//si no
//require_once("../View/resultado.php")
?>
Doing a print_r of $newUser:
Candidato Object ( [dni:protected] => 1111 [nombre:protected] => juan [apellidos:protected] => sanchez [sexo:protected] => m [idiomas:protected] => Array ( [0] => 1 ) )
And these are the errors:
Notice: Undefined variable: pdni in C:\xampp\htdocs\recuPHP\MVC\Final\Model\base.php on line 104
Notice: Undefined variable: pnombre in C:\xampp\htdocs\recuPHP\MVC\Final\Model\base.php on line 105
Notice: Undefined variable: papellidos in C:\xampp\htdocs\recuPHP\MVC\Final\Model\base.php on line 105
Notice: Undefined variable: psexo in C:\xampp\htdocs\recuPHP\MVC\Final\Model\base.php on line 105
For some unknown reason, the variables do not reach the model and I get an error in the insert statement.
'If the function asked me for the 5 attributes, I would understand how to do it, but asking me for an already created object is what shocks me.'
The 5 attributes are being asked for when creating the candidate object. The __construct() function is in charge of that. It is executed when you do the "new Candidate". So if you then pass the object that you have created through "new Candidate" to the insertData function, the parameters that you have passed to the __construct are already properties of the object