Jump to content

ayuda con PHP y Mysql


Recommended Posts

Buenas warianos. hace rato no pasaba por aca.

 

Tengo un duda con mis script, acabo de realizar un tutorial que encontre en youtube. pero me tira un error de "UNDEFINED INDEX :consulta"

 

El tema es que lo otro funciona bien pero no inserta nada en la bd.

Los codigos son los siguientes.

 

 

index.php

 

<?php
$html="
<a href ='index.php?consulta=agregar'>Agregar<a/>
! <a href ='index.php?consulta=actualizar'>Actualizar<a/>
! <a href ='index.php?consulta=eliminar'>Eliminar<a/>
";
switch($_GET['consulta']){
case 'agregar':
agregar();
break;
case 'actualizar':
actualizar();
break;
case 'eliminar':
eliminar();
break;
default:
echo $html;
}
function agregar(){
$html="
<form method = 'post' action = 'consultas.php?consulta=agregar'>
Ingrese id :<input type = 'text' name = 'id'><br/>
Ingrese nombre :<input type = 'text' name = 'nombre'><br/>
<input type = 'submit' value ='OK'>
<a href = 'index.php'>REGRESAR<a/>
<form/>
";
echo $html;
}
function actualizar(){
$html="
<form method = 'post' action = 'consultas.php?consulta=actualizar'>
Ingrese id a actualizar :<input type = 'text' name = 'id'><br/>
Ingrese nombre a actualizar:<input type = 'text' name = 'nombre'><br/>
<input type = 'submit' value ='OK'>
<a href = 'index.php'>REGRESAR<a/>
<form/>
";
echo $html;
}
function eliminar(){
$html="
<form method = 'post' action = 'consultas.php?consulta=eliminar'>
Ingrese id a eliminar :<input type = 'text' name = 'id'><br/>
<input type = 'submit' value ='OK'>
<a href = 'index.php'>REGRESAR<a/>
<form/>
";
echo $html;
}
?>

 

y consultas.php

 

 

 

<?php
switch($_GET['consulta']){
case 'agregar':
agregar();
break;
case 'actualizar':
actualizar();
break;
case 'eliminar':
eliminar();
break;
default:
echo "<a href = 'index.php'>REGRESAR<a/>";
}
function miconexion(){
return mysql_connect('localhost','bd','');
}
function agregar(){
$conexion = miconexion();
$queryStr = sprintf("INSERT INTO bd.usuario(nombre) VALUES('%i','%s')",$_POST['id'],$_POST['nombre']);
mysql_query($queryStr);
mysql_close($conexion);
echo "<a href = 'index.php'>REGRESAR<a/>";
}
function actualizar(){
$conexion = miconexion();
$queryStr = sprintf("UPDATE bd.usuario SET nombre ='%s' WHERE id =%s",$_POST['nombre'],$_POST['id']);
mysql_query($queryStr);
mysql_close($conexion);
echo "<a href = 'index.php'>REGRESAR<a/>";
}
function eliminar(){
$conexion = miconexion();
$queryStr = sprintf("DELETE FROM bd.usuario WHERE id =%s",$_POST['id']);
mysql_query($queryStr);
mysql_close($conexion);
echo "<a href = 'index.php'>REGRESAR<a/>";
}
?>

 

Tengo mi bd en xampp como localhost, sin pass y la base de dato se llama bd, la tabla se llama usuario y esta posee 2 campos, id y nombre.

 

espero me puedan ayudar.

Link to comment
Share on other sites

para ese tipo de notificaciones puedes modificar el error_reporting de php
colocando esto en tu codigo...
error_reporting(E_ALL ^ E_NOTICE);

 

index.php no veo problemas, en consultas.php

 

la conexion es asi:


function miconexion(){
return mysql_connect("localhost","root","");
}

 

te falta seleccionar la bd

 

function dbSelection(){

return mysql_select_db("bd"):

}

 

en agregar:

 

function agregar(){
$conexion = miconexion();

$dbSel = dbSelection();

$query = "INSERT INTO bd.usuario (nombre) VALUES ('".$_POST['nombre']."')";
$queryStr = sprintf($query);
mysql_query($query);
mysql_close($conexion);

echo "<a href = 'index.php'>REGRESAR<a/>";
}

 

en actualizar:

 

function actualizar(){
$conexion = miconexion();

$dbSel = dbSelection();

$query = "UPDATE bd.usuario SET nombre='%s' WHERE id='".$_POST['id']."'";

$queryStr = sprintf($query);
mysql_query($query);
mysql_close($conexion);

echo "<a href = 'index.php'>REGRESAR<a/>";
}

 

en eliminar:

 

function eliminar(){
$conexion = miconexion();

$query = "DELETE FROM bd.usuario WHERE id ='".$_POST['id']."'":

$queryStr = sprintf($query);
mysql_query($query);
mysql_close($conexion);

echo "<a href = 'index.php'>REGRESAR<a/>";
}

 

 

a ver como andas con eso cumpa...

Edited by cañangasñangas
Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...