-5

When I save info. I works fine. No errors and databse is saved. But when I enter something in database which already exists like firstname(to check if uniqueness works). It gives the following NOTICE and also echo the message - "Username taken." . I made many changes but no results, Thanks in adcance

<?php

require_once('includes/database.php');
require_once('includes/user.php');
require_once('includes/functions.php');
require_once('includes/session.php');

?>

<?php

if(isset($_POST['submit'])){ //Form submitted


 $username  = $database->escape_value($_POST['vnumber']);
$firstname = $database->escape_value($_POST['fname']);
$lastname  = $database->escape_value($_POST['lname']);
$email     = $database->escape_value($_POST['email']);
$password  = $database->escape_value($_POST['password']);
$password  = md5($password);

//Check fields aren't empty

if (empty($username)){

echo "Please fill in username";

} else if (empty($firstname)){

echo "Cannot be blank";   

} else if(empty($lastname)){

echo "Cannot be blank";    

} else if(empty($email)){

echo "Cannot be blank";

} else if (empty($password)){

echo "Cannot be blank";

} else {

//Checking uniqueness of username

$query = mysql_query("SELECT * FROM user_info WHERE fname='$firstname'");

$rows = mysql_num_rows($query);

if ($rows == 1){

echo "Username taken !";    

} else {

//If rows = 0. Fields not empty. Data can be saved.

 $result ="";

 global $database;

 $sql    = "INSERT INTO user_info (vnumber, fname, lname, email, password) VALUES 

   ('$username', '$firstname', '$lastname', '$email', '$password')";

 $result = $database->query($sql);
}
if($result){
       die( "Regisgtration success !");
} else {

       die("Something went wrong !");
}
}

}
?>
  • you intialize the $result variable in one of your else statements. if $rows == 1 then it will never be initialized and a notice will appear. Put the $result = ""; before the if($rows == 1) and it will work I think. – Christoph Diegelmann Sep 02 '13 at 11:46
  • Selecting by first name may result in more than one row ... SELECT * FROM user_info ... also you shoud avoid SELECT * if possible. – djot Sep 02 '13 at 11:49
  • Bing a newbie, you should avoid writing such long and complex scripts from scratch. But start from simpler ones – Your Common Sense Sep 02 '13 at 11:50

2 Answers2

0

$result is not defined before (the if statement above does not count for this), that's why you get this notice.

On top of your script, initialize

$result = FALSE;

and everythig is fine.

djot
  • 2,952
  • 4
  • 19
  • 28
  • I added this query. It worked. Everything is working fine. But still I'm not getting why I have to initialize it ? Is it because of else staement ? – user2739698 Sep 02 '13 at 15:35
  • $result is likely NOT to be initialized by your other statements. Later on, PHP then might ask for a variable that is not set, which does and should result in a notice. As said, put $return = FALSE directly below your very first if statement, where you initialize the other variables like $username, ... – djot Sep 02 '13 at 15:44
0

$result is unset because $rows == 1 for sure. So it fails at if ($result) as you don't create the $result variable before, nor set it in the if block.

There are many guides on the Internet to learn how to handle PhP errors, where do they come form, how to solve them, etc ... check it out, it won't be the last time this happens to you !

Clément Malet
  • 5,062
  • 3
  • 29
  • 48