Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/156759-solved-wont-sumbit-a-query/
Share on other sites

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;
      }

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

 

        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 );

}

?>

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 );
        }

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 );
        }


} 
  

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.