christofurr Posted June 25, 2007 Share Posted June 25, 2007 Here's my first attempt: <?php $takenick = mysql_query("SELECT Nickname FROM table"); $takemail = mysql_query("SELECT Email_Address FROM table"); $usednick = mysql_fetch_array($takenick); $usedmail = mysql_fetch_array($takemail); if ($nicktrim == $usednick) {$error = "1";} if ($_POST["email"] == $usedmail) {$error = "2";} if ($error != "1" && $error != "2") { /*Execute database input*/ } ?> It's for a form. I connect to the database prior to this bit of code. The connection is fine, and if I leave the or die(mysql_error()) function in, it displays the duplicate entry error. However, I don't want that to happen. I want the above code to echo an error message like "This username is already taken" if the nickname is already in the database. But, it doesn't work. What do I change? Quote Link to comment Share on other sites More sharing options...
trq Posted June 25, 2007 Share Posted June 25, 2007 Are you even talking about the same code you posted? There is no call to die() or mysql_error(). PS: This can all be done (and should be) more efficiently in one simple query. Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 26, 2007 Author Share Posted June 26, 2007 The die function is a part of the database connection and selection, to which, I mentioned, I connected to prior to the bit of code that I posted. How should it all be done? Quote Link to comment Share on other sites More sharing options...
trq Posted June 26, 2007 Share Posted June 26, 2007 How should it all be done? I would simply use... <?php if ($result = mysql_query("SELECT Nickname, Email_Address FROM table WHERE NickName = '$nicktrim' && Email_Address = '{$_POST['email']}'")) { if (mysql_num_rows($result)) { echo "Username or email already taken"; } } ?> If you want to be more precise with your description. <?php if ($result = mysql_query("SELECT Nickname, Email_Address FROM table")) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { if ($row['NickName'] == $nicktrim)) { echo "Username already taken"; } if ($row['Email_Address'] == $_POST['email'])) { echo "Email already taken"; } } } } ?> Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 26, 2007 Author Share Posted June 26, 2007 Uhh.. Give me a few to look up some functions you used. Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 26, 2007 Author Share Posted June 26, 2007 Ok, I can see how your script will work, but can you explain why mine does not? Quote Link to comment Share on other sites More sharing options...
trq Posted June 26, 2007 Share Posted June 26, 2007 Yours only ever checks the first row of the database. Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 26, 2007 Author Share Posted June 26, 2007 In your script, why is the following line necessary? if (mysql_num_rows($result)) { Quote Link to comment Share on other sites More sharing options...
trq Posted June 26, 2007 Share Posted June 26, 2007 To make sure your query actually returns data before you attempt to use it. Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 26, 2007 Author Share Posted June 26, 2007 So... All I really needed to do is use the while() statement? <?php $takenick = mysql_query("SELECT Nickname FROM table"); $takemail = mysql_query("SELECT Email_Address FROM table"); while ($usednick = mysql_fetch_array($takenick) { if ($nicktrim == $usednick) {$error = "1";} } while ($usedmail = mysql_fetch_array($takemail) { if ($_POST["email"] == $usedmail) {$error = "2";} } if ($error != "1" && $error != "2") { /*Execute database input*/ } ?> Quote Link to comment Share on other sites More sharing options...
trq Posted June 26, 2007 Share Posted June 26, 2007 Why do you want to execute two queries when one is more efficient? Sheesh! Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 26, 2007 Author Share Posted June 26, 2007 When I see it this way, it's easier to understand. I'll start writing my scripts like yours when I'm more comfortable with PHP. And you didn't answer my question... Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 26, 2007 Author Share Posted June 26, 2007 Well, even with the while() statement, it doesn't work. The form doesn't error and the information submits to the database as if there are no duplicates. Quote Link to comment Share on other sites More sharing options...
teng84 Posted June 26, 2007 Share Posted June 26, 2007 declare the fileds in the db as unique or query the data then condition if ($nicktrim == $usednick) {$error1 = "1";} } while ($usedmail = mysql_fetch_array($takemail) { if ($_POST["email"] == $usedmail) {$error2 = "2";} } if ($error1 != "1" && $error2 != "2") { /*Execute database input*/ } Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 26, 2007 Author Share Posted June 26, 2007 declare the fileds in the db as unique or query the data then condition One of them is unique, and has been from the start. Can't decyfer the rest of your suggestion. Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 26, 2007 Author Share Posted June 26, 2007 How about this... <?php $takenick = mysql_query("SELECT Nickname FROM Froobs WHERE Nickname = $_POST['nick']"); $takemail = mysql_query("SELECT Email_Address FROM Froobs WHERE Email_Address = $_POST['email']"); if (mysql_num_rows($takenick) >= "1") {$error = "1";} if (mysql_num_rows($takemail) >= "1") {$error = "2";} if ($error != "1" && $error != "2") { /*Execute database input*/ } ?> Yes? YES?? Quote Link to comment Share on other sites More sharing options...
trq Posted June 26, 2007 Share Posted June 26, 2007 This last effert is your best so far, but really, executing two queries where one is sufficient is rediculous. If you learn to program properly now you wont teach yourself bad habbits. PS: Integers should not be surrouned in quotes. Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 26, 2007 Author Share Posted June 26, 2007 Err.. OK. Thanks! Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 26, 2007 Author Share Posted June 26, 2007 Ermm... Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /join.php5 on line 25 Line 25: $takemail = mysql_query("SELECT Email_Address FROM Froobs WHERE Email_Address = $_POST['email']"); Quote Link to comment Share on other sites More sharing options...
trq Posted June 26, 2007 Share Posted June 26, 2007 There is no parse error in that line, we need a few lines prior. That line would however produce a mysql error as values need quotes surrounding them. Also, if your going to embed complex variables (arrays) within a double quoted string the correct syntax is to use {}. eg; $takemail = mysql_query("SELECT Email_Address FROM Froobs WHERE Email_Address = '{$_POST['email']}'"); Quote Link to comment Share on other sites More sharing options...
mkoga Posted June 26, 2007 Share Posted June 26, 2007 shouldn't it be: <?php $takenick = mysql_query("SELECT Nickname FROM Froobs WHERE Nickname = '{$_POST['nick']}'"); $takemail = mysql_query("SELECT Email_Address FROM Froobs WHERE Email_Address = '{$_POST['email']}'"); if (mysql_num_rows($takenick)) {$error = "1";} if (mysql_num_rows($takemail)) {$error = "2";} if ($error != "1" && $error != "2") { /*Execute database input*/ } ?> Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 26, 2007 Author Share Posted June 26, 2007 I don't think the error has anything to do with that. If it did, line 24 would also be defective. I might have to remove the single quotes from line 25. Quote Link to comment Share on other sites More sharing options...
trq Posted June 26, 2007 Share Posted June 26, 2007 Can we see your current code? Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 26, 2007 Author Share Posted June 26, 2007 No more error on line 25, but now there's one on line 50. Something about the mysql_num_rows() argument not being valid. if (mysql_num_rows($takenick) >= 1) And now I've discovered a problem with the form's input values, but we'll get to that later. <?php $takenick = mysql_query("SELECT Nickname FROM Froobs WHERE Nickname = $_POST['nick']"); $takemail = mysql_query("SELECT Email_Address FROM Froobs WHERE Email_Address = $_POST[email]"); if (mysql_num_rows($takenick) >= 1) {$error = "1";} if (mysql_num_rows($takemail) >= 1) {$error = "2";} if ($error != "1" && $error != "2") { /*Execute database input*/ } ?> Quote Link to comment Share on other sites More sharing options...
ReDucTor Posted June 26, 2007 Share Posted June 26, 2007 Wow, I am suprised at half you guys code, and disappointed. <?php $q = mysql_query('SELECT COUNT(*) FROM Froobs WHERE Nickname=\''.$_POST['nick'].'\' OR Email_Address=\''.$_POST['email'].'\'') or die(mysql_error()); if(mysql_result($q,0,0)!=0) die('Already Exists'); ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.