Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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);
?>
share|improve this question
    
try putting ` on either side of username so it becomes `username` – Dave Jun 19 '13 at 10:27
1  
easy - $result is in english but your query is in Italian. Change if(!$result) to if(!$risultato) Ciao. – Robert Seddon-Smith Jun 19 '13 at 10:27
    
Sorry for the carelessness! few hours of sleep! Thank you very much! – user2467899 Jun 19 '13 at 10:30

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

share|improve this answer
    
Opppsssss I's trueeee :-) Thank you – user2467899 Jun 19 '13 at 10:28
    
You're welcome :) – CharlyDelta Jun 19 '13 at 10:30
$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) {
share|improve this answer
    
Sorry for the carelessness! few hours of sleep! Thank you very much – user2467899 Jun 19 '13 at 10:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.