vorrei inserire uno script in PHP che permetta di evitare che un nuovo utente inserisca un "nome utente" che è già presente nel database mysql
vorrei inserire uno script in PHP che permetta di evitare che un nuovo utente inserisca un "nome utente" che è già presente nel database mysql
ecco il codice che ovviamente nn funziona:
$sqlquery = "SELECT * FROM $miodatabase WHERE utente= '".$utente."'";
$result = mysql_query($sqlquery);
if (mysql_num_rows($result) == 0){
echo "il nome utente non esiste";
}else{
echo "il nome utente esiste";
}
considera che ti passi i dati da un form in POST
quindi avrai:
dopo di che ti basta fare una select sul tuo db cercando se qualcuno ha già lo stesso nome:codice:$email=$_POST['email'];
questo codice è funzionante..dovrai giustamente modificare la select mettendo i tuoi dati del tuo database..ciao!codice:$query = "SELECT * FROM `utente` WHERE email='$email'"; $ris = mysql_query($query) or die("Errore nella query: " . mysql_error()); while($array = mysql_fetch_array($ris)){ $conta="$array[email]"; } if (empty($conta)) $conta = 0; else $conta=count($conta); //controllo sulla variabile if (empty($conta)) { //inserisco il record $query = "INSERT INTO `utente` VALUES ('$nome','$cognome','$email','$telefono')"; $ris = mysql_query($query) or die("Errore nella query: " . mysql_error()); echo("<META http-equiv='REFRESH' content='0; url=index.php'>"); echo "<script>alert(\"Benvenuto $nome $cognome\");</script>"; } else { echo "<script>alert(\"La tua email risulta già registrata\");</script>"; } //------------- FINE PARTE GESTIONE SQL -----------------------------------![]()
dovrei inserire questo codice PHP ( in alto dentro <?php ..... ?> )nella pagina in PHP dove c'è il form da compilare giusto?
il database mysql funziona correttamente
il database è composto da: NOME, UTENTE, PASSWORD
mi basta che il nuovo utente che si inscrive non abbia un doppione nella colonna "utente"
Allora, credo che tu abbia le idee un pò confuse, cmq vediamo se ti spiego meglio:
hai la pagina registrati.php, dentro questa pagina hai un form
questo form quindi conterrà tre campi dove inserire nome, utente, password e un pulsante "Registrati"Codice PHP:
<form action="_sign.php" method="post">
<input name="nome" type="text" size="25"/>
<input name="utente" type="text" size="25"/>
<input name="password" type="text" size="25"/>
<input name="registrati" type="submit" value="Registrati" />
</form>
Dopo che clicchi questo sul pulsante, il form vede cosa hai scritto su action, non farà altro che inviare in POST questi tre dati (nome, utente, password) alla pagina _sign.php
------------
Ora siamo dentro _sign.php
qui devi naturalmente richiamare il db, fatto questo ti devi memorizzare le variabili passate:
ora fai una query al db con il dodice che ti ho ripasso:Codice PHP:
$nome=$_POST['nome'];
$utente=$_POST['utente'];
$password=$_POST['password'];
Codice PHP:
$query = "SELECT nome FROM `tabella_utenti` WHERE utente='$utente'";
$ris = mysql_query($query) or die("Errore nella query: " . mysql_error());
while($array = mysql_fetch_array($ris)){
$conta="$array[nomel]";
}
if (empty($conta))
$conta = 0;
else
$conta=count($conta); //controllo sulla variabile
//se $conta è uguale a 0 vuol dire che il nome non è doppione
if (empty($conta)) {
//inserisco il record
$query = "INSERT INTO `tabella_utenti` VALUES ('$nome','$utente','$password')";
$ris = mysql_query($query) or die("Errore nella query: " . mysql_error());
echo("<META http-equiv='REFRESH' content='0; url=index.php'>");
echo "<script>alert(\"Benvenuto $nome\");</script>";
}
else {
echo "<script>alert(\"La tua email risulta già registrata\");</script>";
}
Facci sapere..![]()
il mio form da compilare inizia cosi:
<form action="<?php echo $editFormAction; ?>" method="POST" name="formregistrazione">
<p class="a1">Inserisci i tuoi dati</p>
<span class="a1">Nome:</span>
<input type="text" name="nome" value=""maxlength="20"/></p>
ecc.....
non so se ti può essere utile, ma utilizzo dreamweaver cs3 e phpmyadmin(per creare le tabelle) e ho un database mysql aruba in remoto
usa questo codice..ti crea un form..codice:<form action="_sign.php" method="post"> <table border="0"> <tr> <td>Nome:</td> <td><input name="nome" type="text" size="20" /></td> </tr> <tr> <td> Utente:</td> <td><input name="utente" type="text" size="20" /></td> </tr> <tr> <td>Password:</td> <td><input name="password" type="text" size="20" /></td> </tr> <tr> <td colspan="2"> <input name="registrati" type="submit" value="Registrati" /> </td> </tr> </table> </form>
Non riesco a capire perchè in action hai scritto questa cosa?
action="<?php echo $editFormAction; ?>"
E poi mi viene un dubbio..il database come è strutturato?
dovrebbe essere così:
Nome database: db_utenti
dentro il database, dovrebbe esserci una sola tabella contenente tre campi
Nome tabella: persona
dentro la tabella dovresti avere tre campi
nome VARCHAR(30), utente VARCHAR(30), password VARCHAR(30)
Hai veramente tutto per poter risolvere il problema, penso che il collegamento al database lo effettui correttamente????? giusto??????????
ho notato che
questa roba: GetSQLValueString($_POST['nome'], "text"),
GetSQLValueString($_POST['utente'], "text"),
GetSQLValueString($_POST['password'], "text"),
sono sempre all'interno del <?php .... ?> del file php dove c'è il form
posso dirti che queste righe di codice php (circa 50 righe) si sono create in automatico dopo che con dreamweaver ho fatto "inserisci record"
il database ha anche id (ma è messa in auto increment e nn prende valori da nessuno)
cmq si, il database funziona perfettamente
<?php require_once('../Connections/connessione_registrazione.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($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"] == "formregistrazione")) {
$insertSQL = sprintf("INSERT INTO databaseutenti (nome, utente, password, regione) VALUES (%s, %s, %s, %s)",
GetSQLValueString($_POST['nome'], "text"),
GetSQLValueString($_POST['utente'], "text"),
GetSQLValueString($_POST['password'], "text"),
GetSQLValueString($_POST['regione'], "text"));
mysql_select_db($database_connessione_registrazion e, $connessione_registrazione);
$Result1 = mysql_query($insertSQL, $connessione_registrazione) or die(mysql_error());
$insertGoTo = "../index.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
?>
nessuno mi aiuta????![]()
![]()
![]()