Jump to content

Recommended Posts

I've made a log in script but when I press submit...The script just appears to refresh.

 

Can anyone see what's going on?:)

 

Thanks

 

<html>
<head><title>Register</title></head>
<body>
<?php
$self = $_SERVER['PHP_SELF'];;
$username = $_POST['username'];
$password = $_POST['password'];

if( ( $username == NULL ) or ( $password == NULL ) ) {
        $form ="Please enter all new user details...";
        $form.="<form action=\"$self\"";
        $form.="method=\"post\">Username: ";
        $form.="<input type=\"text\" name=\"username\"";
        $form.=" value=\"$username\"><br>Password: ";
        $form.="<input type=\"text\" names=\"password\"";
        $form.=" value=\"$password\"><br>";
        $form.="<input type=\"submit\" value=\"Submit!\">";
        $form.="</form>";
        echo( $form );
}else{
        // MySQL details
        $mysql_host="localhost";
        $mysql_user="nikon";
        $mysql_pass="lol";
        $mysql_dbname="nutrition_db";


        //Connect to MySQL
        $conn = @mysql_connect( "localhost", "nikon", "lol" )
                or die("Could not connect to MySQL");
        
        
        //Selects the database
        $db = @mysql_select_db( "nutrition_db", $conn )
                        or die("Could not select database");
        
        //creates the query
        $sql = "insert into users (username, password) values ('".$username."', '".$password."')";
        
        $result = @mysql_query( $sql, $conn )
                or die("Could not execute query");
        
        if( $result !== FALSE)  
                 echo( "New user $username added" ); 
}
?>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/51991-pressing-submit-refreshes-my-login/
Share on other sites

Why are doing your form in PHP format?

It just makes it much easier to do it in HTML.

I also don't really understand why you put form values in "$username".

 

But this is what I would have done

<?
if(isset($_POST['Submit']))
{ 
if ($_POST['username'] == "" or $_POST['password'] == "" ) 
{
echo "Please complete Username and Password";
}
else
{
$self = $_SERVER['PHP_SELF'];
$username = $_POST['username'];
$password = md5($_POST[password]); //password encryption

$mysql_host="localhost";
$mysql_user="nikon";
$mysql_pass="lol";
$mysql_dbname="nutrition_db";

$conn = @mysql_connect( "localhost", "nikon", "lol" )
or die("Could not connect to MySQL");
        

$db = @mysql_select_db( "nutrition_db", $conn )
or die("Could not select database");

$sql = "insert into users (username, password) values ('$username', '$password')";
        
$result = @mysql_query( $sql, $conn )
or die("Could not execute query");
        
if($result) 
{
echo( "New user $username added" ); 
}
}
}
?>
Please enter all new user details...
<form action="<? $self ?>"method="post">
Username:<input type="text" name="username" value="">
<br>
Password:
<input type="text" names="password" value="">
<br>
<input type="submit" value="Submit" name="Submit">
</form>

Thank you for your reply...but all this does is refreshes and makes "Please enter all new user details..." into "Please complete Username and PasswordPlease enter all new user details..."

 

The script that i posted above was 90% copied out of a book...I didn't imagine the book to be wrong =/

 

 

Why are doing your form in PHP format?

It just makes it much easier to do it in HTML.

I also don't really understand why you put form values in "$username".

 

But this is what I would have done

<?
if(isset($_POST['Submit']))
{ 
if ($_POST['username'] == "" or $_POST['password'] == "" ) 
{
echo "Please complete Username and Password";
}
else
{
$self = $_SERVER['PHP_SELF'];
$username = $_POST['username'];
$password = md5($_POST[password]); //password encryption

$mysql_host="localhost";
$mysql_user="nikon";
$mysql_pass="lol";
$mysql_dbname="nutrition_db";

$conn = @mysql_connect( "localhost", "nikon", "lol" )
or die("Could not connect to MySQL");
        

$db = @mysql_select_db( "nutrition_db", $conn )
or die("Could not select database");

$sql = "insert into users (username, password) values ('$username', '$password')";
        
$result = @mysql_query( $sql, $conn )
or die("Could not execute query");
        
if($result) 
{
echo( "New user $username added" ); 
}
}
}
?>
Please enter all new user details...
<form action="<? $self ?>"method="post">
Username:<input type="text" name="username" value="">
<br>
Password:
<input type="text" names="password" value="">
<br>
<input type="submit" value="Submit" name="Submit">
</form>

<html>
<head><title>Register</title></head>
<body>
<?php
$self = $_SERVER['PHP_SELF'];;
$username = $_POST['username'];
$password = $_POST['password'];
if (!isset($_POST['submit'])) { //form hasn't been submitted
//the reason the next line is wrong is because   will return a true statement
//if( ( $username == NULL ) or ( $password == NULL ) ) {
        $form ="Please enter all new user details...";
        $form.="<form action=\"$self\"";
        $form.="method=\"post\">Username: ";
        $form.="<input type=\"text\" name=\"username\"";
        $form.=" value=\"$username\"><br>Password: ";
        $form.="<input type=\"text\" names=\"password\"";
        $form.=" value=\"$password\"><br>";
        $form.="<input type=\"submit\" value=\"Submit!\">";
        $form.="</form>";
        echo( $form );
}else{ //form's been submitted
         // MySQL details
        $mysql_host="localhost";
        $mysql_user="nikon";
        $mysql_pass="lol";
        $mysql_dbname="nutrition_db";


        //Connect to MySQL
        $conn = @mysql_connect( "localhost", "nikon", "lol" )
                or die("Could not connect to MySQL");
        
        
        //Selects the database
        $db = @mysql_select_db( "nutrition_db", $conn )
                        or die("Could not select database");
        
        //creates the query
        $sql = "insert into users (username, password) values ('".$username."', '".$password."')";
        
        $result = @mysql_query( $sql, $conn )
                or die("Could not execute query");
        
        if( $result !== FALSE)  
                 echo( "New user $username added" ); 
}
?>
</body>
</html>

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.