PHP - Undefined index

 
Vista:
sin imagen de perfil

Undefined index

Publicado por jesus (5 intervenciones) el 05/08/2015 19:21:24
Hola,

Tengo un problema con mi código me aparece el siguiente error:

Notice: Undefined index: orange in C:\xampp\htdocs\molina\prueba_enc\prueba2.php on line 23

Notice: Undefined index: grapefruit in C:\xampp\htdocs\molina\prueba_enc\prueba2.php on line 23

Notice: Undefined index: banana in C:\xampp\htdocs\molina\prueba_enc\prueba2.php on line 23

Notice: Undefined index: orange in C:\xampp\htdocs\molina\prueba_enc\prueba2.php on line 23

Notice: Undefined index: grapefruit in C:\xampp\htdocs\molina\prueba_enc\prueba2.php on line 23

Notice: Undefined index: banana in C:\xampp\htdocs\molina\prueba_enc\prueba2.php on line 23

Warning: mysqli_query(): Couldn't fetch mysqli in C:\xampp\htdocs\molina\prueba_enc\prueba2.php on line 28

Warning: mysqli_error(): Couldn't fetch mysqli in C:\xampp\htdocs\molina\prueba_enc\prueba2.php on line 28

Error reading database:

Pero si me guarda los datos en mi Base me pueden apoyar se los agradeceria mucho

Codigo:


<?php
include("conexion.php");

$chkbox = array('orange', 'apple', 'grapefruit', 'banana', 'watermelon');
if(isset($_POST['btnsave']))
{ $fruit = $_POST['fruit'];

$values = array();

foreach($fruit as $selection )
{ if(in_array($selection, $fruit))
{ $values[ $selection ] = 1; }
else
{ $values[ $selection ] = 0; }
}
// var_dump($selection); Ver el arreglo de forma rapida
// MySQL statement.

for ($i=0; $i <sizeof($values) ; $i++) {


$insert = "INSERT INTO table_location2 (orange, apple, grapefruit, banana, watermelon)
VALUES ('".$values['orange']."', '".$values['apple']."','".$values['grapefruit']."', '".$values['banana']."','".$values['watermelon']."')";



// MySQL statement to execute the INSERT statement above.
mysqli_query($dbconnect, $insert) or die('<br/>Error reading database: '.mysqli_error($dbconnect));
mysqli_close($dbconnect);
}
} // End of, if statement from the button check
?>

<html>
<head>
<title>Checkbox</title>
<head>
<title>HTML Checkbox</title>
</head>
<body>
<h2> Pick your most favorite fruits: </h2>
<form name="fruitcheckbox" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="checkbox" name="fruit[]" value="orange"> Orange
<input type="checkbox" name="fruit[]" value="apple"> Apple
<input type="checkbox" name="fruit[]" value="grapefruit"> Grapefruit
<input type="checkbox" name="fruit[]" value="banana"> Banana
<input type="checkbox" name="fruit[]" value="watermelon"> Watermelon
<br>
<input type="submit" value="Save" name="btnsave">
</form>
</body>
</html>

Muchas gracias!!!!!!!!!!!!! :)
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de xve
Val: 3.943
Oro
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

Undefined index

Publicado por xve (6935 intervenciones) el 05/08/2015 20:28:06
Hola Jesus, fijate que no es un error, es un Notice... te esta informando de que estas utilizando unos indices que no existen, pero solo como nota informativa...
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar
sin imagen de perfil

Undefined index

Publicado por jesus (5 intervenciones) el 05/08/2015 21:32:29
Hola xve, tienes razón es solo una notificación muchas gracias por lo que mi pregunta seria ¿cómo hacer que no salieran esas notificaciones?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Undefined index

Publicado por unoMasDelMonton (32 intervenciones) el 06/08/2015 01:52:57
Pues partiendo del hecho que si haces un var_dump($values) después del foreach() verás que el contenido de ese array es solo de los valores que has seleccionado anteriormente en el formulario, por lo que el resto de valores que pides en el
VALUES ('".$values['orange']."', '".$values['apple']."','".$values['grapefruit']."', '".$values['banana']."','".$values['watermelon']."')";

No están definidos y por lo tanto salta el warning. Una idea es comprobar cada valor con un isset() y solo entonces agregarlo a la BBDD.

Por otra parte se pueden hacer ediciones a tu código para evitar hacer tantas comprobaciones.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
include("conexion.php");
 
$chkbox = array('orange', 'apple', 'grapefruit', 'banana', 'watermelon');
 
if(isset($_POST['btnsave']))
{
	$fruit = $_POST['fruit'];
	$values = array();
 
	foreach($fruit as $selection)
	{
		if(in_array($selection, $chkbox))
		{
			$values[] = $selection;
		}
	}
 
	if($values!=array()){
		$campos =  implode(", ",$values);
		$valores = "'".implode("', '", array_fill ( 0 , count($values) , 1 ))."'";
 
		//var_dump($values);// Ver el arreglo de forma rapida
		// MySQL statement.
		$insert = "INSERT INTO table_location2 ($campos) VALUES ($valores)";
 
		echo $insert;
 
		// MySQL statement to execute the INSERT statement above.
		mysqli_query($dbconnect, $insert) or die('<br/>Error reading database: '.mysqli_error($dbconnect));
		mysqli_close($dbconnect);
	}
} // End of, if statement from the button check
?>
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

Undefined index

Publicado por jesus (5 intervenciones) el 06/08/2015 18:11:16
Gracias lo voy a probar
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar