0

I have a problem with this piece of code because it does't work but it seems to be semantically correct. It arrive up to "die("Errore nella query: $query.");" but I can't understand why. Help me please:

<?php
// Connessione al DB Server
$conn = mysqli_connect("localhost","root","","mydb");
// Controllo
if (!$conn){
  echo "connessione database fallita";
  exit();
}
$nome = $_POST["nm"];
$cognome = $_POST["cgn"];
$email = $_POST["ml"];
$username = $_POST["nkn"];
$password = md5($_POST["psw1"]);

$nome = mysqli_real_escape_string($conn,trim($nome));
$cognome = mysqli_real_escape_string($conn,trim($cognome));
$email = mysqli_real_escape_string($conn,trim($email));
$username = mysqli_real_escape_string($conn,trim($username));
$password = mysqli_real_escape_string($conn,trim($password));

$query = "SELECT username FROM utenti WHERE username = '".$username."'";
$risultato = mysqli_query($conn,$query);
if (!$result) {
    die("Errore nella query: $query.");
}
$riga = mysqli_fetch_array($risultato,MYSQLI_NUM);
$n = mysqli_affected_rows($conn);
if($n >= 1){
    echo "Username non disponibile";
}else{
    $inserisci = "INSERT INTO utenti (nome,cognome,email,username,password) VALUES ('$nome','$cognome','$email','$username','$password')";
    mysqli_query($conn,$inserisci);
?>
<p>Utente: <?php echo $username ?> registrato con successo.</p>
<?php
}
mysqli_close($conn);
?>
3
  • try putting ` on either side of username so it becomes `username` Commented Jun 19, 2013 at 10:27
  • 1
    easy - $result is in english but your query is in Italian. Change if(!$result) to if(!$risultato) Ciao. Commented Jun 19, 2013 at 10:27
  • Sorry for the carelessness! few hours of sleep! Thank you very much! Commented Jun 19, 2013 at 10:30

2 Answers 2

1

You check for the $result variable, but the SQL Data is stored in $risultato. Rename your variables right

Sign up to request clarification or add additional context in comments.

1 Comment

Opppsssss I's trueeee :-) Thank you
1
$risultato = mysqli_query($conn,$query);
if (!$result) {
    die("Errore nella query: $query.");
}

It looks like you're checking the wrong variable. Try this:

if (!$risultato) {

1 Comment

Sorry for the carelessness! few hours of sleep! Thank you very much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.