I've done a CRUD
of people with queries prepared in PDO
that worked and now they have sent me to do it in a way that I do not understand, which is passing the values to use in array
in execute()
.
For example, to create a person I put this and it worked for me:
$stmt=conectar()->prepare("INSERT INTO persona (codigo,nombre) VALUES (:codigo,:nombre)");
$stmt->bindParam(':codigo',$codigo);
$stmt->bindParam(':nombre',$nombre);
$codigo=$_GET['codigo'];
$nombre=$_GET['nombre'];
$stmt->execute();
$texto="Persona creada!";
Now I have searched the internet and I have written this:
$stmt=conectar()->prepare("INSERT INTO persona (codigo,nombre) VALUES (:codigo,:nombre)");
$stmt->execute(array(':codigo'->$codigo,':nombre'=>$nombre));
And he says:
Notice: Trying to get property of non-object in C: \ xampp \ htdocs \ xampp \ ad \ people \ new.php on line 59 SQLSTATE [HY093]: Invalid parameter number: parameter was not defined
Since it did not work for me, I wrote this:
$stmt=conectar()->prepare("INSERT INTO persona (codigo,nombre) VALUES (:codigo,:nombre)");
$persona=new Persona($codigo,$nombre);
$stmt->execute((array)$persona);
And I get the following error:
SQLSTATE [23000]: Integrity constraint violation: 1048 Column 'code' can not be null My Person class is this:
class Persona{
private $codigo;
private $nombre;
function __construct($codigo,$nombre){
$this->codigo=$codigo;
$this->nombre=$nombre;
}
function getCodigo(){
return $this->$codigo;
}
function setCodigo($codigo){
$this->codigo=$codigo;
}
function getNombre(){
return $this->$nombre;
}
function setNombre($nombre){
$this->nombre=$nombre;
}
}