blink359 Posted May 4, 2009 Share Posted May 4, 2009 I have a form that is trying to put account info into a database but when you submit it thinks this is allways true so that every time you try and make an account it thinks it allready exsists so it doesnt do the query: $result = mysql_query("SELECT * FROM `accounts` where login='$user'") or die("Error: (" . mysql_errno() . ") " . mysql_error()); $row = mysql_fetch_array( $result ); if(isset($row)) { $problemMessage .= "The username already exists <br />"; $success = false; } here is my PhP code for the form <?php $dbhost = "localhost"; $dbuser = "root"; $dbpass = "ascent"; $dbname = "logon"; mysql_connect($dbhost, $dbuser, $dbpass); mysql_select_db($dbname); $success = true; $problemMessage = ""; if (isset($_POST['Submit'])) { $user = isset($_POST['user'])?$_POST['user']:false; $pass = isset($_POST['pass'])?$_POST['pass']:false; $pass2 = isset($_POST['pass2'])?$_POST['pass2']:false; if(!$user || !$pass || !$pass2) { $problemMessage .= "Please complete all required data <br />"; $success = false; } if(strlen($user) < 5) { $problemMessage .= "User must be more that 5 characters <br />"; $success = false; } if(strlen($user) > 20) { $problemMessage .= "Your username cannot be longer than 20 characters <br />"; $success = false; } if($pass != $pass2) { $problemMessage .= "Your passwords do not match <br />"; $success = false; } if(strlen($pass2) < 5) { $problemMessage .= "Your password must be more than 5 characters<br />"; $success = false; } if(strlen($pass2) > 20) { $problemMessage .= "Your password cannot be longer than 20 characters <br />"; $success = false; } $result = mysql_query("SELECT * FROM `accounts` where login='$user'") or die("Error: (" . mysql_errno() . ") " . mysql_error()); $row = mysql_fetch_array( $result ); if(isset($row)) { $problemMessage .= "The username already exists <br />"; $success = false; } if ($success) { echo "Valid New Account Entry<br />"; $result = mysql_query("INSERT INTO `accounts` VALUES ('', '$'user', '$pass2', '', '', '0', '9', '', '', 'flag', 'enUS', '0', '0', null);") or die("Error: (" . mysql_errno() . ") " . mysql_error()); } $row = mysql_fetch_array( $result ); } ?> <?php if ($success == false) { echo $problemMessage; } ?> Account Creation Page<br> Username: <input name="user" type="text"/> <br> Password: <input name="pass" type="text"/> <br> Repeat Password: <input name="pass2" tpye="text"/> <br /> Pre TBC <input type="radio" name="flag" value="0"> <br> TBC Enabled <input type="radio" name="flag" value="8"> <br> Wotlk Enabled <input type="radio" name="flag" value="24"> <br> <input name="Submit" type="submit" value="submit" /> </p> <imput name="reset" type="reset" value="reset" /> </form> Thanks Blink359 Quote Link to comment https://forums.phpfreaks.com/topic/156759-solved-wont-sumbit-a-query/ Share on other sites More sharing options...
wildteen88 Posted May 4, 2009 Share Posted May 4, 2009 This is incorrect: $row = mysql_fetch_array( $result ); if(isset($row)) { $problemMessage .= "The username already exists <br />"; $success = false; } isset checks for variable existence. You telling PHP to check to see if the variable $row exists. As you have defined it in the line above it'll always return true. What you should of done is use mysl_num_rows to check to see if any results where returned from your query if(mysql_num_rows( $result ) > 0) { $problemMessage .= "The username already exists <br />"; $success = false; } Quote Link to comment https://forums.phpfreaks.com/topic/156759-solved-wont-sumbit-a-query/#findComment-825475 Share on other sites More sharing options...
blink359 Posted May 4, 2009 Author Share Posted May 4, 2009 Right thanks for that Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /var/www/nathan/wotlk/register.php on line 96 $row = mysql_fetch_array( $result ); Hopefully this is last thing i need to ask Reason im having problems is i wrote it to work on my computer but it doesnt work on others so i got it edited slightly and some of the code i dont know what it does i really need to start reading my "Sam's teach yourself Mysql, PhP and apache" book i got somewhere Quote Link to comment https://forums.phpfreaks.com/topic/156759-solved-wont-sumbit-a-query/#findComment-825479 Share on other sites More sharing options...
gevans Posted May 4, 2009 Share Posted May 4, 2009 Show us the lines before, the query and the assigning of $result Quote Link to comment https://forums.phpfreaks.com/topic/156759-solved-wont-sumbit-a-query/#findComment-825480 Share on other sites More sharing options...
wildteen88 Posted May 4, 2009 Share Posted May 4, 2009 What is line 96 in register.php? Post lines 90 to 100 here. That error is normally produced when a query fails. Quote Link to comment https://forums.phpfreaks.com/topic/156759-solved-wont-sumbit-a-query/#findComment-825481 Share on other sites More sharing options...
blink359 Posted May 4, 2009 Author Share Posted May 4, 2009 if ($success) { echo "Valid New Account Entry<br />"; $result = mysql_query("INSERT INTO `accounts` VALUES ('', '$user', '$pass2', '', '', '0', '9', '', '', 'flag', 'enUS', '0', null);") or die("Error: (" . mysql_errno() . ") " . mysql_error()); } $row = mysql_fetch_array( $result ); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/156759-solved-wont-sumbit-a-query/#findComment-825482 Share on other sites More sharing options...
wildteen88 Posted May 4, 2009 Share Posted May 4, 2009 Your closing your if prematurely. This line $row = mysql_fetch_array( $result ); Should be after: $result = mysql_query("INSERT INTO `accounts` VALUES ('', '$user', '$pass2', '', '', '0', '9', '', '', 'flag', 'enUS', '0', null);") or die("Error: (" . mysql_errno() . ") " . mysql_error()); Like so if ($success) { echo "Valid New Account Entry<br />"; $result = mysql_query("INSERT INTO `accounts` VALUES ('', '$user', '$pass2', '', '', '0', '9', '', '', 'flag', 'enUS', '0', null);") or die("Error: (" . mysql_errno() . ") " . mysql_error()); $row = mysql_fetch_array( $result ); } Quote Link to comment https://forums.phpfreaks.com/topic/156759-solved-wont-sumbit-a-query/#findComment-825483 Share on other sites More sharing options...
blink359 Posted May 4, 2009 Author Share Posted May 4, 2009 Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /var/www/nathan/wotlk/register.php on line 93 if ($success) { echo "Valid New Account Entry<br />"; $result = mysql_query("INSERT INTO `accounts` VALUES ('', '$user', '$pass2', '', '', '0', '9', '', '', 'flag', 'enUS', '0', null);") or die("Error: (" . mysql_errno() . ") " . mysql_error()); $row = mysql_fetch_array( $result ); } } Quote Link to comment https://forums.phpfreaks.com/topic/156759-solved-wont-sumbit-a-query/#findComment-825484 Share on other sites More sharing options...
gevans Posted May 4, 2009 Share Posted May 4, 2009 Look at the query, you're doing an INSERT Quote Link to comment https://forums.phpfreaks.com/topic/156759-solved-wont-sumbit-a-query/#findComment-825485 Share on other sites More sharing options...
blink359 Posted May 4, 2009 Author Share Posted May 4, 2009 Right thanks guys i got that work now to test it on other peoples computers Quote Link to comment https://forums.phpfreaks.com/topic/156759-solved-wont-sumbit-a-query/#findComment-825486 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.