ciao non capisco perche non mi fa registrare i nuovi utenti, se questi non ci sono nel database
codice HTML:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/3.14.1/math.min.js"></script>
<link href="config.css" type="text/css" rel="stylesheet">
</head>
<body>
<div id="form-riconoscimento">
<h4>modulo di riconoscimento</h4>
<button onclick="generaUsername()">genera username</button>
<table>
<form method="get" action="server.php">
<tr>
<td><input type="text" name="username" placeholder="username" id="username" required></td>
</tr>
<tr>
<td><input type="password" name="password" placeholder="password"></td>
</tr>
<tr>
<td><input type="radio" name="opzioneUtente" value="registrareUtente">registrasi</td></tr>
<tr>
<td><input type="radio" name="opzioneUtente" value="loggareUtente">accedere</td>
</tr>
<tr>
<td><input type="submit" value="invio"></td></tr>
</form>
</table>
</div>
<script type="text/javascript">
codiceAlfa = "HAvc13nmsdw";
caratteri = codiceAlfa.split(""); //return array
codici = [];
function generaUsername(){
for(i=0; i<=caratteri.length; i++){
codici.push(caratteri[math.randomInt(caratteri.length)]) ;
}
codice = codici.join(""); //return string
document.getElementById("username").value=codice;
}
</script>
</body>
</html>
Codice PHP:
<?php
session_start();
$conex = mysql_connect('localhost', 'root');
mysql_select_db("my_laboraccio", $conex);
$uname = $_GET['username'];
$pw = $_GET['password'];
$opzioneUtente = $_GET['opzioneUtente'];
function registraUtente(){
$sql = "SELECT * FROM utenti WHERE username='$uname'";
$resource = mysql_query($sql) or die (mysql_error());
$result = mysql_num_rows($resource);
echo $result;
if($result == 0) {
$sql = "INSERT INTO utenti (id, username, password) VALUES (NULL, '$uname', '$pw')";
mysql_query($sql);
header("Refresh:10; url=http://www.laboraccio.altervista.org/");
echo "nuovo utente creato";
echo "registrazione completata";
}
elseif($result == 1){
echo "username gia in uso, riprova con un altro";
header("Refresh:10; url=http://www.laboraccio.altervista.org/");
}
}
function loggareUtente(){
$sql = "SELECT * FROM utenti WHERE username='$uname' AND password='$pw'";
$resource = mysql_query($sql) or die (mysql_error());
$result = mysql_num_rows($resource);
if($result == 1) {
echo "b";
$_SESSION['username'] = $uname;
$_SESSION['password'] = $pw;
echo "benvenuto alla tua area personale".$uname;
}
}
echo $uname;
echo $pw;
if($opzioneUtente == 'registrareUtente'){
registraUtente();
}
elseif($opzioneUtente == "loggareUtente") {
loggareUtente();
}
?>