Jump to content

Checking If $user & $pass = " "


Shaned145

Recommended Posts

Ok, Im obviously not doing this right. lol I need some help with my script:

 

if !( $user=="" || $pass=="");
  mysql_query("INSERT INTO users (id, sn, pw) 
  VALUES ( '$idhigh + 1', '$user', '$pass')");

 

It wont load up my page when I try to run it with this in my page, what am I doing wrong? It checks my text boxes to see if the user and pass equals a blank input when the person clicks on the submit button, and if it does, then it does nothing. Please, any help is appreciated.

Link to comment
Share on other sites

if !( $user=="" || $pass=="");

The exclamation mark means doe not equal "". I want it so the user cant enter in a blank input on either text boxes. Then the || means or so neither the username nor password can remain blank. So like if I put, "" as the username or password, that it does not enter it. But if I put in say "Shaned123" as the user and "tacos" for the pass, then it adds it in my db.

Link to comment
Share on other sites

you need this code:

 

if ( $user != "" || $pass != ""){
  mysql_query("INSERT INTO users (id, sn, pw) 
  VALUES ( '$idhigh + 1', '$user', '$pass')");
}

 

rather than:

 

if !( $user=="" || $pass=="");
  mysql_query("INSERT INTO users (id, sn, pw) 
  VALUES ( '$idhigh + 1', '$user', '$pass')");

Link to comment
Share on other sites

you need this code:

 

if ( $user != "" || $pass != ""){
  mysql_query("INSERT INTO users (id, sn, pw) 
  VALUES ( '$idhigh + 1', '$user', '$pass')");
}

 

rather than:

 

if !( $user=="" || $pass=="");
  mysql_query("INSERT INTO users (id, sn, pw) 
  VALUES ( '$idhigh + 1', '$user', '$pass')");

 

Thanks I'll try it out. I new there shouldve been a brackets in there somewhere.

Link to comment
Share on other sites

you need this code:

 

if ( $user != "" || $pass != ""){
  mysql_query("INSERT INTO users (id, sn, pw) 
  VALUES ( '$idhigh + 1', '$user', '$pass')");
}

 

rather than:

 

if !( $user=="" || $pass=="");
  mysql_query("INSERT INTO users (id, sn, pw) 
  VALUES ( '$idhigh + 1', '$user', '$pass')");

 

Actually he will want to use && instead of || if you write it like that. "If $user is not empty AND $pass is not empty, THEN put them in the database".

Link to comment
Share on other sites

never seen that one

 

this is the new code then:

 

if ( ($user !="") && ($pass !=""));
  mysql_query("INSERT INTO users (id, sn, pw) 
  VALUES ( '$idhigh + 1', '$user', '$pass')");
}

 

You've replaced the semi-colon again! So the IF statement will do nothing and the mysql function will always run regardless.

Link to comment
Share on other sites

Ok so I set it as unique, and it doesnt add another username in my table if its taken. But does anyone know how to make it so if the username is taken, that it redirects them to a page where it would say "ERROR: The username has already been registered"?

Link to comment
Share on other sites

Something like:

$query = "select * from users where sn='$user'";
$result = mysql_query($query) or die(mysql_error());
if(mysql_num_rows($result) > 0
{

   header("Location:error.php");

}

 

where error.php is the page that handles that eventuality.

Hmmmm, I cant seem to get it working. I made an error.php page but it doesnt show.

Link to comment
Share on other sites

To check to see if a username / password combination is correct (valid for login) you would use something like....

 

<?php

 if (isset($_POST['uname']) && isset($_POST['upass'])) {
   $uname = mysql_real_escape_string($_POST['uname']);
   $upass = mysql_real_escape_string($_POST['upass']);
   if ($result = mysql_query("SELECT uname,upass FROM users WHERE uname = '$uname' && upass = '$upass';")) {
     if (mysql_num_rows($result)) {
       // username / password combination valid.
     } else {
       // username / password combination invalid.
     }
   }
 }

?>

Link to comment
Share on other sites

I don't know if your question is still lingering, but to check if something is in a table you just do:

 

SELECT field FROM table WHERE field = data

 

Example:

 

$q = mysql_query("SELECT uname FROM users WHERE uname='".addslashes($_POST['uname']."'");

if($q && mysql_num_rows($q) < 0) {

//user doesn't exist

}

else {

//user exists

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.