0

My form worked, but when i put lastname(lname) field won't work

<?php
include "connect.php";
echo "<h1>Register</h1>";
?>

<div style="color:#F00">
<?php
date_default_timezone_set('Europe/Zagreb');
  date_default_timezone_get();
  ini_get('timezone.default');
  getenv('TZ');
$num = 0;
$submit = $_POST['submit'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
date_default_timezone_get();
$date = date("Y-m-d G:i:s");
if ($submit){
if (!$username){
$num ++;
echo "<tr><td>".$num.". Username required.</td></tr><br />";
}
if (!$fname){
$num ++;
echo "<tr><td>".$num.". Firstname required.</td></tr><br />";
}
if (!$lname){
$num ++;
echo "<tr><td>".$num.". Lastname required.</td></tr><br />";
}
if (!$password){
$num ++;
echo "<tr><td>".$num.". Password required.</td></tr><br />";
}
if (!$email){
$num ++;
echo "<tr><td>".$num.". Email required.</td></tr><br />";
}
if ($username){
if (strlen($username) > 20){
$num ++;
echo "<tr><td>".$num.". Username is too long.(3-20)</td></tr><br />";
}
}
if ($username){
if (strlen($username) < 3){
$num ++;
echo "<tr><td>".$num.". Username is too short.(4-20)</td></tr><br />";
}
}
if ($fname){
if (strlen($fname) > 20){
$num ++;
echo "<tr><td>".$num.". Firstname is too long.(3-20)</td></tr><br />";
}
}
if ($fname){
if (strlen($fname) < 3){
$num ++;
echo "<tr><td>".$num.". Firstname is too short.(4-20)</td></tr><br />";
}
}
if ($lname){
if (strlen($lname) > 20){
$num ++;
echo "<tr><td>".$num.". Lastname is too long.(3-20)</td></tr><br />";
}
}
if ($lname){
if (strlen($lname) < 3){
$num ++;
echo "<tr><td>".$num.". Lastname is too short.(4-20)</td></tr><br />";
}
}
if ($password){
if (strlen($password) > 20){
$num ++;
echo "<tr><td>".$num.". Password is too long.(6-20)</td></tr>";
}
}
if ($password){
if (strlen($password) < 5){
$num ++;
echo "<tr><td>".$num.". Password is too short.(6-20)</td></tr>";
}
}
if ($username){
$sql = mysql_query("SELECT * FROM users WHERE username='$username'")or die(mysql_error());

if(mysql_num_rows($sql) > 0){
$num ++;
echo "<tr><td>".$num.". The username '<b>".$username."</b>' already exists.</td></tr><br />";
}
}

$name = $_FILES["img"]["name"];
$tmp = $_FILES["img"]["tmp_name"];
$error = $_FILES["img"]["error"];
if ($error > 0){
$num ++;
echo "<tr><td>".$num.". ERROR uploading file!</td></tr><br />";
}
if (!$name){
$num ++;
echo "<tr><td>".$num.". Profile image required.</td></tr><br />";
}
if ($num == 0){
echo "Success!";
include "connect.php";
$location = "img/$name";
move_uploaded_file($tmp,"img/".$name);
$queryreg = mysql_query("
INSERT INTO users VALUES ('','$fname','$lname','$username','$password','$email','$location','$date')
");
echo "You have been registered.<a href='index.php'>Click here to return to login page.</a>";
}
}
?>
</div>
<form action="register.php" method="post" enctype="multipart/form-data">
  <table>
  <tr>
  <td>
    Firstname:
  </td>
  <td>
  <input type="text" name="fname" value="<?php echo $fname; ?>" />
  </td>
  </tr>
   <tr>
  <td>
    Lastname:
  </td>
  <td>
  <input type="text" name="lname" value="<?php echo $lname; ?>" />
  </td>
  </tr>
  <tr>
  <td>
    Username:
  </td>
  <td>
  <input type="text" name="username" value="<?php echo $username; ?>" />
  </td>
  </tr>
  <tr>
  <td>
    Password:
  </td>
  <td>
  <input type="password" name="password" />
  </td>
  </tr>
  <tr>
  <td>
    Email:
  </td>
  <td>
  <input type="text" name="email" value="<?php echo $email; ?>" />
  </td>
  </tr>
  <tr>
  <td>
    Profile image:
  </td>
  <td>
  <input type="file" name="img" />
  </td>
  </tr>
  <tr>
  <td>
  <input type="submit" name="submit" value="Register" />
  </td>
  </tr>
</form>
3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by Will Gresham
0

Does it give you an error or does it just not perform the INSERT? Are you sure there is a column for last name in your table? Speaking of which, I usually use INSERT SET so I'm a little unsure of this part

$queryreg = mysql_query("
INSERT INTO users VALUES ('','$fname','$lname','$username','$password','$email','$location','$date')
");

I would normally expect to see something like

$queryreg = mysql_query("
INSERT INTO users (first_name, last_name, user_name, password, email, location, date) VALUES ('$fname','$lname','$username','$password','$email','$location','$date')
");

Just making an observation. You may be correct, I'm just not used to seeing it that way.

0

I solved this problem.
I have latest activity field in table and i must put:

$queryreg = mysql_query("
      INSERT INTO users VALUES ('','$fname','$lname','$username','$password','$email','$location','$date','')
      ");
1

You can avoid this problem by using a statement similar to CFROGs suggestion. Make sure all fields have a default value, or are allowed to be NULL.

Also, please look at your code, this can probably be reduced by 50% (in terms of the number of lines) due to a lot of repetition. For example, this:

if ($fname){
if (strlen($fname) > 20){
$num ++;
echo "<tr><td>".$num.". Firstname is too long.(3-20)</td></tr><br />";
}
}
if ($fname){
if (strlen($fname) < 3){
$num ++;
echo "<tr><td>".$num.". Firstname is too short.(4-20)</td></tr><br />";
}
}

Could become this:

if ($fname){
  if (strlen($fname) > 20 || strlen($fname) < 3){
    $num ++;
    echo "<tr><td>".$num.". Firstname must be between 3 and 20 characters.</td></tr><br />";
  }
}

That will do practically the same thing, but is only 6 lines rather than 12.

You probably won't see a difference in response in a script like this or on a low traffic site, but in busy sites or much longer scripts then cutting back on unneeded characters and whitespace can help performance :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.