Para que no haya más confusión, pego de nuevo el código final:
Código PHP:
<?php
session_start();
include('includes/conexion.php');
if(isset($_SESSION['email'])){
$_POST["enviar"] = "";
if(isset($_POST["enviar"])){
$resultado = $conexion->prepare ( "SELECT nombre, apellidos FROM usuarios WHERE email=:email" );
$resultado->bindparam ( ':email', $_SESSION ['email'], PDO::PARAM_STR);
$resultado->execute();
if($row = $resultado ->fetch()){
$nombre = $row[0];
$apellidos = $row[1];
$nombrecompleto = $nombre . ' ' . $apellidos;
echo'
<form action = "" method = "post">
<table border="1">
<tr>
<td >Usuario</td>
<td> Fecha </td>
<td>ID Proyecto y Tarea</td>
<td>Horas</td>
<td>Comentarios</td>
</tr>
<td>
<strong><div>'.$nombrecompleto.' </div></strong>
</td>
<td>
<input type=date name="fecha">
</td>';
$res = $conexion->prepare("SELECT PID, ID, 'SUB-ID' from owner WHERE NOMBRE LIKE '%$nombrecompleto%' ORDER BY PID ASC");
$res->execute();
$aDatos = array();
while($n = $res->fetch()){
$aDatos[] = $n;
}
echo'<td><select name = "ids" id = "ides>';
foreach($aDatos as $row){
$pid = $row[0];
$id = $row[1];
$subid = $row[2];
if($subid = 0){
$subid=0;
}
echo '<option value="'.$pid."_".$id."_".$subid.'">';
echo "PID: ".$pid." - ID: ".$id." - Sub-Id: ".$subid;
echo '</option>';
}
echo '</select></td>';
if(isset($_POST['fecha'])){
$fecha = $_POST['fecha'];
}
if(isset($_POST['horas'])){
$horas = $_POST['horas'];
}
if(isset($_POST['comentarios'])){
$comentarios = $_POST['comentarios'];
}
echo '<td><input type = "text" name = "horas"/> </td>
<td><input type = "text" name = "comentarios"/> </td>
</table>
<input type="submit" name="enviar" value="Guardar valores">
</form>';
$value = $_REQUEST['ids'];
$explode = explode("_",$value,3);
$pid = $explode[0];
$id = $explode[1];
$subid = $explode[3];
$r =$conexion-> prepare("INSERT INTO 'timesheet' (nombre, fecha, pid, id, sub_id, Horas, Comentarios) VALUES (:nombre, :fecha, :pid, :id, :subid, :horas, :comentarios)");
$r ->bindParam(':nombre', $nombrecompleto);
$r ->bindParam(':fecha', $fecha);
$r ->bindParam(':pid', $pid);
$r ->bindParam(':id', $id);
$r ->bindParam(':subid', $subid);
$r ->bindParam(':horas', $horas);
$r ->bindParam(':comentarios', $comentarios);
$r ->execute();
}
}
}
?>
Mi problema es que el insert no almacena el valor que se selecciona en el formulario, si no el que por defecto le llega del foreach. Y al ejecutar éste código me aparecen estos errores:
Notice: Undefined index: ids in C:\xampp\htdocs\prueba\timesheet4.php on line 71
Notice: Undefined offset: 1 in C:\xampp\htdocs\prueba\timesheet4.php on line 74
Notice: Undefined offset: 3 in C:\xampp\htdocs\prueba\timesheet4.php on line 75
Las líneas son:
Código PHP:
$value = $_REQUEST['ids'];
$id = $explode[1];
$subid = $explode[3];