Foros aprenderaprogramar.com

Aprender a programar => C, C++, C#, Java, Visual Basic, HTML, PHP, CSS, Javascript, Ajax, Joomla, MySql y más => Mensaje iniciado por: Ojkita en 23 de Febrero 2016, 12:36

Título: PHP Notice: Undefined index: Matricula in... recuperar por POST, GET o REQUEST
Publicado por: Ojkita en 23 de Febrero 2016, 12:36
Buenas a todos!!

Estoy empezando con esto de PHP, bases de datos y estoy haciendo como una especie de Web para una empresa de taxis.

Me da un Warning como si no se enviara un $_POST y no encuentro la solución.
Os dejo el código por si podéis orientar un poco.

Código: [Seleccionar]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Modificar Datos</title>
<link rel="stylesheet" href="tabla.css" />
</head>

<body>
<h1>Selecciona la Matr&iacute;cula para modificar los datos</h1>
<?php$conexion = mysqli_connect("localhost:3306", "root", "", "taxicorp");if (!isset($_POST["Matricula"])) {echo ("<form action='cambio.php' method='post'>");$sel = "SELECT Matricula FROM taxis";$exec = mysqli_query($conexion, $sel);$row = mysqli_fetch_array($exec);echo ('<select name="Matricula" id="Matricula">');while ($registro = mysqli_fetch_array($exec)) {echo ('<option>' . $registro["Matricula"] . "</option>");}?>

</select>

<input type="submit" id="cambia" name="cambia" value="enviar" />

<?php} else {$sel = "SELECT * FROM taxis WHERE Matricula='" . $_POST["Matricula"] . "'";$exec = mysqli_query($conexion, $sel);if (!$exec) echo("Error al conectar con la base"  . mysqli_error($conexion));$row = @mysqli_fetch_array($exec); echo("<form action='cambia2.php' method='post'>");?>

<table border="1">
<tr><th colspan="2">Matr&iacute;cula:&nbsp;&nbsp;<font color="red"><strong>
<?php echo($_POST["Matricula"]); ?>
</strong></font></th></tr>
<tr>
  <td>Modelo:</td>
  <td colspan="1"><input type="text" name="Modelo" required value='
<?php echo($row["Modelo"]); ?>
' /></td>
</tr>
<tr>
  <td>Nombre:</td>
  <td colspan="1">
  <input name="Nombre" type="text" id="Nombre" size="20" required maxlength="20" value='
<?php echo($row["Nombre"]); ?>
'/></td></tr>
  <tr><td>Apellidos:</td><td><input name="Apellidos" type="text" id="Apellidos" size="40" maxlength="40" required value='
<?php echo($row["Apellidos"]); ?>
'/></td>
</TR>
<TR>
  <td><font color="green">Libre:</font></td>
  <td colspan="-1"><input name="libre" type="checkbox" id="libre" value="Libre"
<?php if ($row["Ocupado"]) echo("checked"); ?>
  />
  </td>
</TR>
<TR>
  <td colspan="2" align="center"><input name="enviar" type="submit" id="enviar" value="Modificar Datos" /></td>
  </TR>
</TABLE>
</form>
<?php}?>

</body>
</html>


Estos datos de arriba se envían a la siguiente página, que es donde me aparece el Warning.

Código: [Seleccionar]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Cambia2</title>
<link rel="stylesheet" href="tabla.css" />
<style type="text/css">
a {
text-decoration:none;
color:#99CC66;
font-weight:600;
}
a:hover {
text-decoration: underline;
color:#99CC66;
font-weight:600;
}
</style>
</head>

<body>
<?php$conexion = mysqli_connect("localhost:3306", "root", "", "taxicorp");//Creamos la sentencia SQL y la ejecutamos$cambia="UPDATE taxis SET Modelo='" .$_POST["Modelo"] . "'," . "Nombre='" . $_POST["Nombre"] . "',";		$cambia.="Apellidos='" . $_POST["Apellidos"] . "',Ocupado=";		if (isset($_POST["Ocupado"]))			$cambia.="true";		else			$cambia.="false";		    $cambia.=" WHERE Matricula='" . $_POST["Matricula"] . "'"; //Aquí me da el error$exec=mysqli_query($conexion, $cambia);if ($exec)	echo("<H1>Cambio realizado</H1>");else	echo("<H1>Se ha producido un error</H1>");?>

<div align="center"><a href="listado.php">Visualizar el contenido de la base</a></div>
</body>
</html>

Gracias de antemano, saludos.
Título: Re:PHP Notice: Undefined index: Matricula in... recuperar por POST, GET o REQUEST
Publicado por: César Krall en 25 de Febrero 2016, 09:00
Hola!

No he hecho pruebas pero antes de hacer la consulta update intenta mostrar por pantalla los valores que recibes del formulario por ejemplo:

echo "Dato recibido de modelo es ".$_POST["Modelo"];
echo "Dato recibido de nombre es ". $_POST["Nombre"];
echo "Dato recibido de apellidos es ".$_POST["Apellidos"];
echo "Dato recibido de ocupado es ".$_POST["Ocupado"];
echo "Dato recibido de matricula es". $_POST["Matricula"];

El error parece estar en que estás tratando de recuperar un valor por post, pero dicho valor no está llegando. Tienes que comprobar qué valores no te llegan y por qué.

Si te sirve de referencia tenemos un curso de php en http://aprenderaprogramar.com/index.php?option=com_content&view=category&id=70&Itemid=193

Saludos!
Título: Re:PHP Notice: Undefined index: Matricula in... recuperar por POST, GET o REQUEST
Publicado por: Ojkita en 26 de Febrero 2016, 13:06
Hola @César Krall, gracias por contestar.
Ya he resuelto el problema, he pasado los datos a través de $_COOKIE. Lo he dejado así, por si a alguien le interesa.

Código: [Seleccionar]
<?php@setcookie("Matricula",$_POST["Matricula"],time()+243600); //Aquí el cambio?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Modificar Datos</title>
<link rel="stylesheet" href="tabla.css" />
</head>

<body>
<h1>Selecciona la Matr&iacute;cula para modificar los datos</h1>
<?php$conexion = mysqli_connect("localhost:3306", "root", "", "taxicorp");if (!isset($_POST["Matricula"])) {echo ("<form action='cambio.php' method='post'>");$sel = "SELECT Matricula FROM taxis";$exec = mysqli_query($conexion, $sel);$row = mysqli_fetch_array($exec);echo ('<select name="Matricula" id="Matricula">');while ($registro = mysqli_fetch_array($exec)) {echo ('<option>' . $registro["Matricula"] . "</option>");}?>

</select>

<input type="submit" id="cambia" name="cambia" value="enviar" />

<?php} else {$sel = "SELECT * FROM taxis WHERE Matricula='" . $_POST["Matricula"] . "'";$exec = mysqli_query($conexion, $sel);if (!$exec) echo("Error al conectar con la base"  . mysqli_error($conexion));$row = mysqli_fetch_array($exec); //No lo entiendo!!//echo(mysql_result($row,0)); echo("<form action='cambia2.php' method='post'>");?>



<table border="1">
<tr><th colspan="2">Matr&iacute;cula:&nbsp;&nbsp;<font color="red"><strong>
<?php if (isset($_POST["Matricula"])) echo($_POST["Matricula"]); ?>
</strong></font></th></tr>
<tr>
  <td>Modelo:</td>
  <td colspan="1"><input type="text" name="Modelo" required value='
<?php echo($row["Modelo"]); ?>
' /></td>
</tr>
<tr>
  <td>Nombre:</td>
  <td colspan="1">
  <input name="Nombre" type="text" id="Nombre" size="20" required maxlength="20" value='
<?php echo($row["Nombre"]); ?>
'/></td></tr>
  <tr><td>Apellidos:</td><td><input name="Apellidos" type="text" id="Apellidos" size="40" maxlength="40" required value='
<?php echo($row["Apellidos"]); ?>
'/></td>
</TR>
<TR>
  <td><font color="green">Libre:</font></td>
  <td colspan="-1"><input name="libre" type="checkbox" id="libre" value="Libre"
<?php if ($row["Ocupado"]) echo("checked"); ?>
  />
  </td>
</TR>
<TR>
  <td colspan="2" align="center"><input name="enviar" type="submit" id="enviar" value="Modificar Datos" /></td>
  </TR>
</TABLE>
</form>
<?php}?>

</body>
</html>

Aquí recibe los datos...

Código: [Seleccionar]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Cambia2</title>
<link rel="stylesheet" href="tabla.css" />
<style type="text/css">
a {
text-decoration:none;
color:#99CC66;
font-weight:600;
}
a:hover {
text-decoration: underline;
color:#99CC66;
font-weight:600;
}
</style>
</head>

<body>
<?php$conexion = mysqli_connect("localhost:3306", "root", "", "taxicorp");//Creamos la sentencia SQL y la ejecutamosecho "Dato recibido de modelo es ".$_POST["Modelo"];echo "Dato recibido de matricula es". $_POST["Matricula"];$sel="UPDATE taxis SET Modelo='" .$_POST["Modelo"] . "'," . "Nombre='" . $_POST["Nombre"] . "',";		$sel.="Apellidos='" . $_POST["Apellidos"] . "',Ocupado=";		if (isset($_POST["libre"]))			$sel.="false";		else			$sel.="true";					    $sel.=" WHERE Matricula='" . $_COOKIE["Matricula"] . "'"; //Aquí la recibe				    $exec=mysqli_query($conexion, $sel);if ($exec)	echo("<H1>Cambio realizado</H1>");else	echo("<H1>Se ha producido un error</H1>");?>

<div align="center"><a href="listado.php">Visualizar el contenido de la base</a></div>
</body>
</html>

Así me ha funcionado.
Gracias y saludos.
Título: Re:PHP Notice: Undefined index: Matricula in... recuperar por POST, GET o REQUEST
Publicado por: César Krall en 28 de Febrero 2016, 21:06
Gracias por poner la solución que has usado!