I have a PHP class to insert POST data in a db. The db has 2 tables: "empleado" and "puesto", where puesto has 2 rows. I want to insert data from a form, but the table "empleado" has a foreign key, so I'm doing a INSERT INTO SELECT as a prepared statement. But when I check the db the table "empleado" is empty...
This is the 1st time I use prepared statements so I don't know if I'm doing it right. Can you tell me what's the problem?
These are the functions of class "Empleado":
public function __construct($host, $usuario, $pass, $db) {
$this->conexion = new mysqli($host, $usuario, $pass, $db);
}
function nuevoEmpleado($nombre, $puesto, $correo, $pass) {
$inserta1 = 'INSERT INTO empleado (nombre, idPuesto, correo, password)
SELECT ?, idPuesto, ?, ? FROM puesto
WHERE puesto = ?';
if($stmt = $this->conexion->prepare($inserta1)) {
$stmt->bind_param('ssss', $nombre, $correo, $pass, $puesto);
$stmt->execute();
$stmt->close();
}
}
This file is called "main.php" and here I create an instance:
include('Empleado.class.php');
$empleado = new Empleado('localhost', 'root', 'mysql', 'prueba');
$empleado->listaPuestos();
if(isset($_POST['enviar'])) {
$empleado->nuevoEmpleado($_POST['nombre'], $_POST['puesto'],, $_POST['correo'], $_POST['pass']);
}
And here's my form:
<form action="main.php" method="post">
<input type="text" placeholder="Nombre" name="nombre"/>
<label>Puesto</label>
<select name="puesto" id="jobs">
</select>
<input type="email" placeholder="aaaa@aaa.aaa" name="correo"/>
<input type="password" placeholder="contraseña" name="pass"/>
<input type="submit" name="enviar"/>
</form>
The select is populated by an ajax.
Thanks in advance.