Hola a todos,
Ya he solucionado este problemilla. Aqui os dejo la forma en que he hecho añadir un usario y Darle acceso a una seccion especifica:
ESTE ES EL FORMULARIO DONDE VEO LOS USUARIOS, CONTRASEÑAS Y LAS SECCIONES QUE HAY Y DONDE INSERTO UN NUEVO USUARIO, CONTRASEÑA Y USUARIO
Código PHP:
<?php require_once('Connections/Datuak3.php'); ?>
<?php
// AQUI SE COMPRUEBA SI EXISTE EL USUARIO
$MM_flag="MM_insert";
if (isset($_POST[$MM_flag])) {
$MM_dupKeyRedirect="ErabilGehi.php?Errorea=bai";
$loginUsername = $_POST['Usuario'];
$LoginRS__query = "SELECT Usuario FROM tabla WHERE Usuario='" . $loginUsername . "'";
mysql_select_db($database_Datuak3, $Datuak3);
$LoginRS=mysql_query($LoginRS__query, $Datuak3) or die(mysql_error());
$loginFoundUser = mysql_num_rows($LoginRS);
requested username
if($loginFoundUser){
$MM_qsChar = "?";
if (substr_count($MM_dupKeyRedirect,"?") >=1) $MM_qsChar = "&";
$MM_dupKeyRedirect = $MM_dupKeyRedirect . $MM_qsChar ."requsername=".$loginUsername;
header ("Location: $MM_dupKeyRedirect");
exit;
}
}
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
//LE OBLIGO A METER LAS SECCIONES ESPECIFICAS Y SI QUIERE UN UEVO ADMINISTRADOR
if (($_POST["Seccion"] == "LaSeccion1") || ($_POST["Seccion"] == "LaSeccion2") || ($_POST["Seccion"] == "LaSeccion3") || ($_POST["Seccion"] == "LaSeccion4") || ($_POST["Seccion"] == "Admnistrador") || (!empty($_POST["Seccion"]))){
$insertSQL = sprintf("INSERT INTO tabla (Id, Usuario, Contraseña, Seccion) VALUES (%s, %s, %s, %s)",
GetSQLValueString($_POST['Id'], "int"),
GetSQLValueString(ucfirst($_POST['Usuario']), "text"),
GetSQLValueString(ucfirst($_POST['Contraseña']), "text"),
GetSQLValueString(ucfirst($_POST['Seccion']), "text"));
mysql_select_db($database_Datuak3, $Datuak3);
$Result1 = mysql_query($insertSQL, $Datuak3) or die(mysql_error());
}else{
echo "LAS SeccionES SOLO PUEDEN SER LaSeccion1, LaSeccion2, LaSeccion3, LaSeccion4 O ADMINISTRADOR Y NO PUEDE ESTAR VACIO";
}
}
//AQUI HAGO EL SELECT PARA VER LOS USUARIOS, CONTRASEÑAS Y SECCIONES QUE HAY
mysql_select_db($database_Datuak3, $Datuak3);
$query_ErabilPasa = "select * from tabla";
$ErabilPasa = mysql_query($query_ErabilPasa, $Datuak3) or die(mysql_error());
$row_ErabilPasa = mysql_fetch_assoc($ErabilPasa);
$totalRows_ErabilPasa = mysql_num_rows($ErabilPasa);
?>
<!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>Usuario eta Contraseña gehitu</title>
<style type="text/css">
<!--
.Estilo1 {
font-size: 24px;
font-weight: bold;
}
-->
</style>
</head>
<body>
<p align="center" class="Estilo1"><u>Usuario eta Contraseña gehitu </u></p>
<p> </p>
<p>
<?
if ($_GET["Errorea"]=="bai"){
echo "Usuario existitzen da";
}
?>
</p>
<p> </p>
<table border="1">
<tr align="center">
<td width="155"><u><strong>Usuario</strong></u></td>
<td width="155"><u><strong>Contraseña</strong></u></td>
<td width="174"><u><strong>Seccion</strong></u></td>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_ErabilPasa['Usuario']; ?></td>
<td><?php echo $row_ErabilPasa['Contraseña']; ?></td>
<td><?php echo $row_ErabilPasa['Seccion']; ?></td>
</tr>
<?php } while ($row_ErabilPasa = mysql_fetch_assoc($ErabilPasa)); ?>
</table>
<p> </p>
<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
<table border="1" align="center">
<tr valign="baseline">
<td width="157" align="left" nowrap>Usuario</td>
<td width="192"><input type="text" name="Usuario" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td nowrap align="left">Contraseña</td>
<td><input type="text" name="Contraseña" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td nowrap align="left">Seccion</td>
<td><input type="text" name="Seccion" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td nowrap align="left"> </td>
<td> </td>
</tr>
</table>
<p>
<input type="hidden" name="IdErabilPasa" value="">
<input type="hidden" name="MM_insert" value="form1">
</p>
<p>
<input name="submit" type="submit" value="Insertar registro" />
</p>
</form>
<p> </p>
</body>
</html>
<?php
mysql_free_result($ErabilPasa);
?>
ESTE ES FORMULARIO DONDE INTRODUZCO EL USUARIO Y LA CONTRASEÑA:
Código PHP:
<body>
<form action="Control.php" method="POST">
<table align="center" width="225" cellspacing="2" cellpadding="2" border="0">
<tr>
<td colspan="2" align="center"
<? if ($_GET["errorusuario"]=="si"){?>
bgcolor=white><span class="Estilo2" style="color:ffffff"><b>Usuario edo Contraseña ez dira zuzenak</b></span>
<?}else{?>
bgcolor=white><span class="Estilo2"> Introduzca Usuario Y Contraseña</span><?}?></td>
</tr>
<tr>
<td align="left"><span class="Estilo1">Usuario</span></td>
<td><input name="Usuario" type="Text" id="Usuario" size="20" maxlength="50"></td>
</tr>
<tr>
<td align="left"><span class="Estilo1">Contraseña</span></td>
<td><input name="Contraseña" type="password" id="Contraseña" size="20" maxlength="50" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="Submit" class="big" value="Sartu"></td>
</tr>
</table>
</form>
</body>
</html>
Aqui es donde hago la comprobacion de si existe el usuario, contraseña y seccion:
Código PHP:
<body>
<?
//conecto con la base de datos
$conn = mysql_connect("localhost","usuario","contarseña");
//selecciono la BBDD
mysql_select_db("Datuak3",$conn);
//Sentencia SQL para buscar un usuario con esos datos
$ssql = "SELECT * FROM tabla WHERE Usuario='".ucfirst($_POST['Usuario'])."' and Contraseña='".ucfirst($_POST['Contraseña'])."' and Seccion='Laseccion1' ";
//Ejecuto la sentencia
$rs = mysql_query($ssql,$conn);
$ssql2 = "SELECT * FROM ErabilPasa WHERE Usuario='".ucfirst($_POST['Usuario'])."' and Contraseña='".ucfirst($_POST['Contraseña'])."' and Seccion='Laseccion2' ";
$rs2 = mysql_query($ssql2,$conn);
$ssql3 = "SELECT * FROM ErabilPasa WHERE Usuario='".ucfirst($_POST['Usuario'])."' and Contraseña='".ucfirst($_POST['Contraseña'])."' and Seccion='Laseccion3' ";
$rs3 = mysql_query($ssql3,$conn);
$ssql4 = "SELECT * FROM ErabilPasa WHERE Usuario='".ucfirst($_POST['Usuario'])."' and Contraseña='".ucfirst($_POST['Contraseña'])."' and Seccion='Laseccion3' ";
$rs4 = mysql_query($ssql4,$conn);
$ssql5 = "SELECT * FROM ErabilPasa WHERE Usuario='".ucfirst($_POST['Usuario'])."' and Contraseña='".ucfirst($_POST['Contraseña'])."' and Seccion='Administrador' ";
$rs5 = mysql_query($ssql5,$conn);
//vemos si el usuario y contraseña es váildo
//si la ejecución de la sentencia SQL nos da algún resultado
//es que si que existe esa conbinación usuario/contraseña/Seccion
if (mysql_num_rows($rs)!=0){
//usuario y contraseña válidos
//defino una sesion y guardo datos
session_start();
session_register("autentificado");
$autentificado = "SI";
header ("Location: Laseccion1.php");
}elseif (mysql_num_rows($rs2)!=0){
session_start();
session_register("autentificado");
$autentificado = "SI";
header ("Location: Laseccion2.php");
}elseif (mysql_num_rows($rs3)!=0){
session_start();
session_register("autentificado");
$autentificado = "SI";
header ("Location: Laseccion3.php");
}elseif (mysql_num_rows($rs4)!=0){
session_start();
session_register("autentificado");
$autentificado = "SI";
header ("Location: Laseccion4.php");
}elseif (mysql_num_rows($rs5)!=0){
session_start();
session_register("autentificado");
$autentificado = "SI";
header ("Location: Administrador.php");
}else {
//si no existe le mando otra vez a la portada
header("Location: index.php?errorusuario=si");
}
mysql_free_result($rs);
mysql_free_result($rs2);
mysql_free_result($rs3);
mysql_free_result($rs4);
mysql_close($conn);
?>
</body>
</html>
Seguro que se puede mejorar muchisimo pero a mi asi me funciona. Espero que os sirva de algo aunque sea para echros unas risas.