Jump to content

Really basic PHP/MySQL Question (first post)


Jakesta42

Recommended Posts

Hey y'all,

 

I'm trying to write a PHP script for a login function. There are three elements, two text fields (username and password) and a button which calls the script.

 

Segment from index.php file:

<form action = "login.php" method = "POST">

Admin Login: <br>

Username: <input type = "text" name = "usernameField"/><br> <!-- Password field-->

Password: <input type = "password" name = "passwordField"/><br> <!-- Username field -->

  <input type = "button" value = "Login"  name = "submitButton"/> <!-- Login button -->

</form>

 

Segment from login.php file:

<?php

$connect = mysql_connect("localhost", "root", "root");

 

if(!$connect){//If user can't connect to database

die('Could not connect: ' . mysql_error()); //Throw an error

}

 

mysql_select_db("colin_db", $connect);

//Get given username and password from username field and password field

$givenUsername = $_POST["usernameField"];

$givenPassword = $_POST["passwordField"];

 

 

$myQuery = "SELECT * FROM ADMINS

WHERE USERNAME = '$givenUsername'

AND PASSWORD = '$givenPassword'";

 

$queryResult = mysql_query($myQuery);

$numRows = mysql_num_rows($queryResult);

 

if($numRows == 1){ //If the details are correct...

//Reload the page and login

echo "<script type = 'text/javascript'> window.location.reload() </script>";

}

elseif($numRows == 0){ //Else if the details are not found

//Display error accordingly

echo "Details not correct!";

}

mysql_close($connect);

?>

 

The problem is, when I click the login button, it doesn't do anything. What am I missing? (The information in the database is correct)

 

Thanks,

 

Jake

17583_.php

Link to comment
Share on other sites

change input type to 'submit' (instead of button)

 

or add a javascript onclick event to submit the form if you really need it to be a button for whatever reason

 

Okay, that issue is fixed; thanks so much. Still another issue stands. Is my SQL right? Because it says that the details are not correct (in my customized error message), but the information in the database matches my input.

 

Is the way I'm determining if the login details are correct right?

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.