Notice: Undefined index: id

2

I have a form with php and html , in which, I want to modify my Database. I'm just confused with this error message.

Form

<?php
	
    require('../admin/conexion.php');

  $id=$_GET['id'];
  $query="SELECT empresa, marca, modelo, n_serie, tipo, procesador, vel_procesador, memoria_ram, disco_duro1, disco_duro2, sistema_operativo, g_inicio, g_final FROM computadora WHERE id='$id'";
  $resultado=$mysqli->query($query);
  $row=$resultado->fetch_assoc();
	
?>


<!DOCTYPE html>
<html lang="en">
  <head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Modificar - Computadora</title>

    <!-- Bootstrap -->
    <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet">
    <style type="text/css">
    body {
	background-image: url(../imagenes/imagen_student.jpg);
    background-size:100% 100%;
    background-attachment:fixed;
}
    </style>

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->


    </head>
    <center>


    <table>
    <form method="POST" action="modificar_computadora.php"> <!-- Archivo PHP de llamada -->
 
 
    <div class="container">
    <p><br/></p>
    <div class="row">
    <div class="col-md-8"></div>
    <div class="col-md-4">


    <div class="panel panel-default">
    <div class="panel-body">
    <div class="page-header">


    <h3>Modificar Computadora</h3>





            

    <!-- Formulario de llamada -->

            <form role="form">
            <div class="form-group">
            <label for="exampleInputEmail1">Empresa</label>
            <div class="form-group">
            <span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
            <input type="text" class="form-control" name = "empresa"  value="<?php echo $row['empresa'];?>" <!--id="exampleInputEmail-->"
            </div>



            <form role="form">
            <div class="form group">
            <label for="exampleInputPassword1">Marca</label>
            <div class="form-group">
            <span class="input-group-addon"><span class="glyphicon glyphicon-file"></span></span>
            <input type="text" class="form-control" name = "marca" value="<?php echo $row['marca']; ?>" <!--id="exampleInputEmail2-->"
            </div>


            <form role="form">
            <div class="form group">
            <label for="exampleInputPassword1">Modelo</label>
            <div class="form-group">
            <span class="input-group-addon"><span class="glyphicon glyphicon-road"></span></span>
            <input type="text" class="form-control" name = "modelo" value="<?php echo $row['modelo']; ?>" <!--id="exampleInputEmail2-->"
            </div>


            <form role="form">
            <div class="form group">
            <label for="exampleInputPassword1">N° Serie</label>
            <div class="form-group">
            <span class="input-group-addon"><span class="glyphicon glyphicon-list-alt"></span></span>
            <input type="text" class="form-control" name = "n_serie" value="<?php echo $row['n_serie']; ?>" <!--id="exampleInputEmail2-->"
            </div>


            <form role="form">
            <div class="form group">
            <label for="exampleInputPassword1">Tipo</label>
            <div class="form-group">
            <span class="input-group-addon"><span class="glyphicon glyphicon-heart"></span></span>
            <input type="text" class="form-control" name = "tipo" value="<?php echo $row['tipo']; ?>" <!--id="exampleInputEmail2-->"
            </div>


            <form role="form">
            <div class="form group">
            <label for="exampleInputPassword1">Procesador</label>
            <div class="form-group">
            <span class="input-group-addon"><span class="glyphicon glyphicon-heart"></span></span>
            <input type="text" class="form-control" name = "procesador" value="<?php echo $row['procesador']; ?>" <!--id="exampleInputEmail2-->"
            </div>


            <form role="form">
            <div class="form group">
            <label for="exampleInputPassword1">Velocidad del Procesador</label>
            <div class="form-group">
            <span class="input-group-addon"><span class="glyphicon glyphicon-heart"></span></span>
            <input type="text" class="form-control" name = "vel_procesador" value="<?php echo $row['vel_procesador']; ?>" <!--id="exampleInputEmail2-->"
            </div>


            <form role="form">
            <div class="form group">
            <label for="exampleInputPassword1">Memoria RAM</label>
            <div class="form-group">
            <span class="input-group-addon"><span class="glyphicon glyphicon-heart"></span></span>
            <input type="text" class="form-control" name = "memoria_ram" value="<?php echo $row['memoria_ram']; ?>" <!--id="exampleInputEmail2-->"
            </div>



            <form role="form">
            <div class="form group">
            <label for="exampleInputPassword1">Disco Duro 1</label>
            <div class="form-group">
            <span class="input-group-addon"><span class="glyphicon glyphicon-heart"></span></span>
            <input type="text" class="form-control" name = "disco_duro1" value="<?php echo $row['disco_duro1']; ?>" <!--id="exampleInputEmail2-->"
            </div>


            <form role="form">
            <div class="form group">
            <label for="exampleInputPassword1">Disco Duro 2</label>
            <div class="form-group">
            <span class="input-group-addon"><span class="glyphicon glyphicon-heart"></span></span>
            <input type="text" class="form-control" name = "disco_duro2" value="<?php echo $row['disco_duro2']; ?>" <!--id="exampleInputEmail2-->"
            </div>


            <form role="form">
            <div class="form group">
            <label for="exampleInputPassword1">Sistema Operativo</label>
            <div class="form-group">
            <span class="input-group-addon"><span class="glyphicon glyphicon-heart"></span></span>
            <input type="text" class="form-control" name = "sistema_operativo" value="<?php echo $row['sistema_operativo']; ?>" <!--id="exampleInputEmail2-->"
            </div>


            <form role="form">
            <div class="form group">
            <label for="exampleInputPassword1">Garantia Inicio</label>
            <div class="form-group">
            <span class="input-group-addon"><span class="glyphicon glyphicon-heart"></span></span>
            <input type="date" class="form-control" name = "g_inicio" value="<?php echo $row['g_inicio']; ?>" <!--id="exampleInputEmail2-->"
            </div>


            <form role="form">
            <div class="form group">
            <label for="exampleInputPassword1">Garantia Final</label>
            <div class="form-group">
            <span class="input-group-addon"><span class="glyphicon glyphicon-heart"></span></span>
            <input type="date" class="form-control" name = "g_final" value="<?php echo $row['g_final']; ?>" <!--id="exampleInputEmail2-->"
            </div>



            <hr/>
            <button type="button" class="btn btn-success"><span class="glyphicon glyphicon-circle-arrow-left"></span>Atras</button>
            <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-pencil"></span>Modificar</button>


            <p><br/></p>
            </form>
            </div>
            </div>
            </div>
            

  <span class="form-group"></span>

  </div>
  </div>
  <form>
    
    

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
    

  </center>
  </body>
</html>
<?php 
	
	require('../admin/conexion.php');
	
	$id=$_POST['id'];
	$empresa=$_POST['empresa'];
	$marca=$_POST['marca'];
	$modelo=$_POST['modelo'];
	$n_serie=$_POST['n_serie'];
	$tipo=$_POST['tipo'];

	$procesador=$_POST['procesador'];
	$vel_procesador=$_POST['$vel_procesador'];
	$memoria_ram=$_POST['$memoria_ram'];
	$disco_duro1=$_POST['$disco_duro1'];
	$disco_duro2=$_POST['disco_duro2'];
	$sistema_operativo=$_POST['sistema_operativo'];

	$g_inicio=$_POST['$g_inicio'];
	$g_final=$_POST['$g_final'];
	
	$query="UPDATE computadora SET empresa='$empresa', marca='$marca', modelo='$modelo', n_serie='$n_serie', tipo='$tipo', procesador='$procesador', vel_procesador='$vel_procesador', memoria_ram='$memoria_ram', disco_duro1='$disco_duro1', disco_duro2='$disco_duro2', sistema_operativo='$sistema_operativo', g_inicio='$g_inicio', g_final='$g_final' WHERE id='$id'";
	
	$resultado=$mysqli->query($query);
	
?>

<html>
	<head>
		<title>Modificar Computadora</title>
	</head>
	
	<body>
		<center>
			
			<?php 
				if($resultado>0){
				?>
				
				<h1>Computadora Modificada</h1>
				
					<?php 	}else{ ?>
				
				<h1>Error, Computadora NO Modificada</h1>
				
			<?php	} ?>
			
			<p></p>	
			
			<a href="../menu_principal.php">Menu Principal</a>
			
		</center>
	</body>
</html>
				
				
    
asked by EVM 05.09.2017 в 17:55
source

2 answers

1

The problem here is that there is no input in your form whose name is id:

<input type='text' name='id'/>

Because of this, it is not sent to the POST variable and when it arrives to the php file, it does not recognize the index id of the array $_POST

You must include this input in your form so that you can recognize the variable. And another error is in the index $_POST['$vel_procesador'] is badly written, it must be $_POST['vel_procesador'] otherwise you will be the same error as with id and will not recognize the index.

    
answered by 05.09.2017 / 23:10
source
0

In your PHP when starting to run the code you are receiving GET variable $id , when the variable has not been sent to the current page either from a form on the same page or in a external page, the variable is empty and the notice that appears is telling you that.

Now, to hide these notifications while the variable is empty you can use the following code:

error_reporting(error_reporting() & ~E_NOTICE);

In your code it would look like this:

    require('../admin/conexion.php');
  error_reporting(error_reporting() & ~E_NOTICE);
  $id=$_GET['id'];
  $query="SELECT empresa, marca, modelo, n_serie, tipo, procesador, vel_procesador, memoria_ram, disco_duro1, disco_duro2, sistema_operativo, g_inicio, g_final FROM computadora WHERE id='$id'";
  $resultado=$mysqli->query($query);
  $row=$resultado->fetch_assoc();

With that you should stop appearing the Notice.

Check that you are sending the variable $id to the file, otherwise you will not be able to perform the SQL query.

    
answered by 05.09.2017 в 18:41