How to Validate an ID (Primary Key) in php? [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a field in a php page to enter the register number and check the Details.
Here is the ScreenShot.
Register Number is the ID in the table.
So how can i validate the register number entered in the page with the register number in the Database.
Here is the Code:
<?php
session_start();
include 'connect1.php';
if (isset($_POST['login']))
{
$_SESSION['regno']= $_POST['regno'];
$regid = $_SESSION['regno'];
if (empty($regid)){
echo "<p class='echo'> *The Field Should not be empty </P>";
}
else if ($regid = )//WHAT SHOULD BE CHECKED HERE ??????
{
header('location: details.php');// If not empty, Details page is loaded.
}
}
?>
<html>
<head>
<title>
Login
</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="reg">
<h1 class="h1">Registered User?</h1>
<form action="" method="POST" >
<input type="number" class="textbox" name="regno" placeholder="Registration Number"/><br/>
<input type="submit" class="login" name="login" value="Check">
</form>
</div>
</body>
</html>
I need to check if the number entered in the field is in the Database??
Thanks in advance :)

Answer is in your pattern use the logic and implement it.
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// escape variables for security
$_SESSION['regno'] = mysqli_real_escape_string($con, $_POST['regno']);
$regid =$_SESSION['regno'];
if (empty($regid)){
echo "<p class='echo'> *The Field Should not be empty </P>";
}
else
{
$sql="select regno from your_table where regno='".$regid."'";
$result=mysqli_query($con,$sql);
$number=mysqli_num_rows($result);
if($number > 0 ){
header('location: details.php');// If not empty, Details page is loaded.
}else{
echo "<p class='echo'> *Not Found </P>";
}
}

Just make a request like this :
Select * From table_name Where ID = the_checked_ID
If it returns nothing, the ID is not in your database

Try this..
<?php
$current_path = $_SERVER['SCRIPT_NAME'];
//include database connection code here :)
if(isset($_POST['regno']))
{
$regnumber = $_POST['regno'];
$query = "Select * From your_table_name Where ID = '$regnumber'";
if($query_run = mysql_query($query))
{
$num_rows = mysql_num_rows($query_run);
if($num_rows == 1)
{
echo "This is registered User";
}
else
{
echo "Please Register";
}
}
}
?>
<html>
<head>
<title>
Login
</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="reg">
<h1 class="h1">Registered User?</h1>
<form action="<?php echo $current_path ?>" method="POST" >
<input type="number" class="textbox" name="regno" id="regno" placeholder="Registration Number"/><br/>
<input type="submit" class="login" name="login" value="Check">
</form>
</div>
</body>
</html>

Related

php code for checking login from db [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
by using the below script, i register users into my db
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Regsitration Successful</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<div class="container">
<header>
<div id="logo"><img src="smu-logo.png"></div>
<ul class="nav_menu">
<li>Home</li>
<li>Register</li>
<li>Notices And Updates</li>
<li>Reach Us</li>
</ul>
<div class="clear"></div>
</header>
<h3>Success!</h3>
<?php
$f_name = $_POST['f_name'];
$m_name = $_POST['m_name'];
$l_name = $_POST['l_name'];
$reg_num = $_POST['reg_num'];
//$dept = $_POST['dept'];
$password = $_POST['password'];
$password2 = $_POST['password2'];
//$about = $_POST['about'];
//$etype = $_POST['etype'];
//connect code
$conn = mysqli_connect("localhost","root","");
$db = mysqli_select_db($conn, "tnp");
$cmd = "insert into ug_login_details values ('$reg_num','$password','$f_name','$m_name','$l_name')";
//for more columns add more after comma
//$cmd = "insert into ug_login_details values ('$reg_num','$password')";// adding user login credentials
if(mysqli_query($conn, $cmd))
{
echo "Quick Registration Successful";
}
else
{
echo "error";
}
echo "Dear, $l_name" ; //change this
?>
<p>Kindly check for the next notice on our Notice and Updates page to confirm.</p>
<p>Click here to migrate to the complete registration form.</p>
</div>
</body>
</html>
now, what code do i need to check users credentials when they login? how to compare values from the database? do i need to create a new page? i already have a login box on my homepage, index.html. i am a beginner, so please cope up. thanks
You are completely right about creating a new page.
You can have an index page like:
<form action="index.php" method="POST">
Username:<input type="text" name="username"/>
Password:<input type="text" name="password"/>
<input type="submit" value="Login"/>
</form>
And then in your PHP:
if($_POST)
{
$Username=$_POST["username"];
$Password=$_POST["password"];
$conn = mysqli_connect("localhost","root","");
$db = mysqli_select_db($conn, "tnp");
$cmd= "select * from ug_login_details where password='$Password' and username='$Username'";
$Result=mysqli_query($conn, $cmd);
$num=mysqli_num_rows($Result);
if($num>0)
{
echo "You have successfully logged in";
exit();
}
else
{
echo "Invalid username or password";
exit();
}
}

php login button that redirects [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am a real beginner and I made a simple login.php but I want to know how to make the login button redirect to another page. The script I have is:
<php
//Start the Session
session_start();
require('connect.php');
//3. If the form is submitted or not.
//3.1 If the form is submitted
if (isset($_POST['username']) and isset($_POST['password'])) {
//3.1.1 Assigning posted values to variables.
$username = $_POST['username'];
$password = $_POST['password'];
//3.1.2 Checking the values are existing in the database or not
$query = "SELECT * FROM `user` WHERE username='$username' and password='$password'";
$result = mysql_query($query) or die(mysql_error());
$count = mysql_num_rows($result);
//3.1.2 If the posted values are equal to the database values, then session will be created for the user.
if ($count == 1) {
$_SESSION['username'] = $username;
} else {
//3.1.3 If the login credentials doesn't match, he will be shown with an error message.
echo "Invalid Login Credentials.";
}
}
//3.1.4 if the user is logged in Greets the user with message
if (isset($_SESSION['username'])) {
$username = $_SESSION['username'];
echo "Hello " . $username . "
";
echo "This is the Members Area
";
?>
<!DOCTYPE html>
<head>
<title>Test Login</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<!-- Form for logging in the users -->
<div class="register-form">
<php
if(isset($msg) & !empty($msg)){
echo $msg;
}
?>
<h1>Login</h1>
<form action="" method="POST">
<p><label>User Name : </label>
<input id="username" type="text" name="username" placeholder="username" /></p>
<p><label>Password : </label>
<input id="password" type="password" name="password" placeholder="password" /></p>
<a class="btn" href="register.php">Signup</a>
<input class="btn register" type="submit" name="submit" value="Login" />
</form>
</div>
<php } ?>
and the page I want it to redirect to is in a called site/form.html located in the parent directory.
Thanks for any and all input!
Just put
header('Location: http:// URL to the Page You Want /');
In where your successful login code is.
Welcome to StackOverflow. If the user doesn't have an account, you would like the button to direct them to a signup page. Right? You could use a javascript onclick event like so.
<button class="btn" type="button" onclick=window.parent.location.href='register.php' target='_parent'>Sign Up!</button>
your old code has quite a few errors. This one fixes most of them, and redirects to form.html.
BTW, the line you want is <form action="form.html" method="POST">
Ok full code:
<?php //Start the Session
session_start();
require('connect.php');
if (isset($_POST['username']) and isset($_POST['password'])){
$username = $_POST['username'];
$password = $_POST['password'];
$query = "SELECT * FROM `user` WHERE username='$username' and password='$password'";
$result = mysql_query($query) or die(mysql_error());
$count = mysql_num_rows($result);
if ($count == 1){
$_SESSION['username'] = $username;
}
else{
echo "Invalid Login Credentials.";
}
}
if (isset($_SESSION['username'])){
$username = $_SESSION['username'];
echo "Hello " . $username;
echo "This is the Members Area";
?>
<!DOCTYPE html>
<head>
<title>Test Login</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<!-- Form for logging in the users -->
<div class="register-form">
<?php
if(isset($msg)){
echo $msg;
}
?>
<h1>Login</h1>
<form action="form.html" method="POST">
<p><label>User Name : </label>
<input id="username" type="text" name="username" placeholder="username" /></p>
<p><label>Password : </label>
<input id="password" type="password" name="password" placeholder="password" /></p>
<a class="btn" href="register.php">Signup</a>
<input class="btn register" type="submit" name="submit" value="Login" />
</form>
</div>
<?php } ?>

Log in Script not returning the values from MYSQL [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
there are three diffrent files all in php please help mysql doen't returns a value from the database ?
//login.php
<?php
session_start();
include("includes/html_codes.php");
if (isset($_POST["email"])&&isset($_POST["password"])) {
$username = mysql_real_escape_string($_POST["email"]);
$password = mysql_real_escape_string($_POST["password"]);
if (!empty($username)&&!empty($password)) {
$query = mysql_query('SELECT * FROM users WHERE email=("$username") AND password=("$password")');
$count = mysql_num_rows($query);
$row = MySQL_fetch_row($query);
if($row==1){
echo 'ok';}
else {
echo 'Wrong Username or Password';
}
} else {
echo 'You must provide a username and password.';
} }
?>
##i guess the query is incorrect please help new to php##
<html xmlns="http://www.w3.org/1999/xhtml">
<html lang="en">
<head>
<title>Log in</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="css/main.css"/>
<link rel="stylesheet" href="css/form.css"/>
<link rel="stylesheet" href="css/register.css"/>
</head>
<body style="background-color:#EEEEEE">
<header>
<?php topBarm(); ?>//some function which works perfectly
</header>
<div id="wrapperLogin">
<center>
<form id="generalForm" class="container" action="<?php echo $current_file; ?>" method="POST">
<div class="field">Username: <input type="email" name="email" id ="email" class="input1"> </div> <div class="field">Password: <input type="password" name="password" id="password" class="input1"></div></center>
<center><div class="field"><input type="submit" class="buton" value="Log in" ></div>
</form></center></html>
""<please read the index.php file also it contains some important stuff>"
index file is including all the files so i didn't need to include it in login.php
//index.php
<?php
require 'includes/core.php';
require 'includes/connect.php';
include("login.php");
echo $current_file;
?>
##connection is established with this file##
>this code works perfectly
//connect.php
<?php
$conn_error = 'Could not connect.';
$mysql_host = 'localhost';
$mysql_user = 'my_user';
$mysql_pass = '';
$mysql_db = 'my_database';
if(!#mysql_connect($mysql_host, $mysql_user, $mysql_pass)||!#mysql_select_db($mysql_db)) {
die(mysql_error());
}
echo 'Connected!'
?>
Your query is badly formed. Try
$query = mysql_query("SELECT * FROM users WHERE email='" . $username . "' AND password='" . $password . "');"
Also, note that the mysql_ functions are deprecated. Update your code to mysqli or PDO.
First of all why do you have echos in the connection.php and why are you including so many files?
To learn basic debugging, you can do something like this with the result you are getting on your queries:
<pre>
<?php echo print_r($result) ?>
</pre>
Assuming you are doing a mysql_fetch_assoc or something similar with the result of the MySQL query
the variable enclosed in single quotes will not be displayed
$query = mysql_query("SELECT * FROM users WHERE email='".$username."' AND password='".$password."'");

Database login doesn't seem to be working when password is correct [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In the following code I check a username and password with a database and then run an if statement to either allow the user to view the page or just show them a "Please login" message. At this point it is only returning the false values even if the passwords are equal.
I know I can pick up code for this on the internet, but I want to figure out where my logic (only in my head) is going wrong. I also know I haven't sanitized the data, but this is just a tutorial for grade 10 students just learning HTML and CSS with a little bit of PHP. I will get to that protecting data later - one concept at a time.
Thanks for the help!
<?php
// connects to server
include('includes/connect2.php');
// sets post to variables don't know if this is needed
$user=$_POST[username];
$pass=$_POST[password];
?>
<?php
// query to find ifnoramtion in database
$result = mysqli_query($con, "SELECT * FROM social_register WHERE Login='$user'");
mysqli_real_escape_string($con, $user) . "'" );
$row = mysqli_fetch_assoc($result);
if ($pass == $row['Password']) { ?>
<div id="container">
<div id="banner">
The social Site
<div id="site_logo">
<img src="images/logo_thumb.gif" />
<?php
while($row = mysqli_fetch_array($result))
{
echo $row['First_Name'] . " " . $row['Last_Name'];
echo "<br>";
}
mysqli_close($con);
?>
</div>
</div>
<div id="person">
<h1>Welcome to your test web site</h1>
</div>
</div>
<?php
}
else
{
?>
<div id="container">
<div id="banner">
The social Site
<div id="site_logo">
<img src="images/logo_thumb.gif" />
</div>
</div>
<div id="person">
<h1>You have not loged into the site, please login.</h1>
</div>
<?php
}
?>
You should fetch data with mysqli_fetch_assoc before match password
$user = mysqli_real_escape_string($con, $user);
$result = mysqli_query($con, "SELECT * FROM social_register WHERE Login = '".$user."'");
$row = mysqli_fetch_assoc($result);
if ($pass == $row['Password']) { ?>
Well, I have put you an Login.php, and a Check.php, I hope that you enjoy it... :P
Try that:
//Login.php
session_start(); //create the session
if(isset($_POST['login']))
{
$con = mysqli_connect("myhost","myuser","mypassw","mybd") or die("Error " . mysqli_error($con));
$user = mysqli_fetch_assoc(mysqli_query($con, "SELECT * FROM social_register WHERE Login='" . mysqli_real_escape_string($con, $user) . "'" ));
$pass = $user['Password']; //put the pass variable
if ( $pass == $user['Password'] ) {
$_SESSION['ready'] = true;
header('Location: you_page.php'); //redirect to your page when you have the correct pass
exit;
} else
{
?>
<script type="text/javascript">
<!--
alert('Incorrect Password')
//-->
</script>
<?php
}
}
//continue with the next block
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> Login </title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<center>
<h3 style="color:#fff;">Password:<h3>
<form method="post" action="">
<input type="password" style="text-align:center;" name="pass"><br>
<input type="submit" name="login" value="Iniciar sesiĆ³n">
</form>
</center>
</body>
</html>
//Check.php
<?php
session_start();
if (!isset($_SESSION['ready'])
|| $_SESSION['ready'] !== true) {
header('Location: login.php'); //Redirect to the login if you haven't the session set
exit;
}
?>
In your_page.php, you have to put:
require 'check.php';
Logout.php
<?php
session_start();
if (isset($_SESSION['ready'])) {
unset($_SESSION['ready']);
}
header('Location: index.php');
exit;
?>

Can anyone see any errors with this MySQL user log in code? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to create a script where an email and password are sent from an form and passes to an action page where a query is ran. No matter what I enter, it always returns 'You have logged out', can anyone see why?
<div id="signinform">
<fieldset>
<legend>Please enter your email address and password</legend>
<form action="account.php" method="POST">
<label>Email :</label>
<input type="text" name="email" /><br />
<label>Password :</label>
<input type="text" name="password" /><br/>
<input class="signbutt" type="submit" value="Log in"/><br />
</form>
</fieldset>
</div>
action page:
<?php
session_start();
$con = mysql_connect("localhost","******","******");
mysql_select_db("deucalio_photostore", $con);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
if(isset($_POST['email']) && isset($_POST['password']))
{
$username = $_POST['email'];
$password = $_POST['password'];
//used because if md5 is used in password itself md5 has is created for blank as well so user can get away with not entering password
if(!empty($username) && !empty($password))
{
$query = "SELECT userID FROM users WHERE email = '$username' AND password = '$password' ";
if($query_run = mysql_query( $con, $query, array()))
{
$query_num_rows = mysql_num_rows($query_run);
if($query_num_rows == 0)
{
echo 'Invalid username/password combination';
}
else if($query_num_rows == 1)
{
while($row = mysql_fetch_array($query_run,mysql_fetch_assoc))
{
$user_name = $row['email'];
$userid = $row['userID'];
$_SESSION['userID'] = $userid;
$_SESSION['email'] = $user_name;
}
}
}
}
else
{
echo 'You must supply a username and password';
}
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>My account</title>
<meta name="description" content="Easy HTML5 Template">
<meta name="author" content="">
<link rel="stylesheet" type="text/css" href="styles.css" />
<script type="text/javascript" src="script.js"></script>
</head>
<body class="home">
<div id="header"> <!-- THIS IS THE TOP DIV THAT CONTAINS THE LOGIN, SITE LOGO AND BASKET CONTAINER -->
</div> <!-- END HEADER CONTAINER -->
<div id="main">
<div id="content">
<?php
if (isset($_SESSION['userID'])){
echo'<p > Hello ' . $user_name. '!</p>' ;}
else{
echo 'You have logged out';}
?>
</div> <!-- END CONTENT CONTAINER -->
</div> <!-- END MAIN CONTAINER -->
<div id="footer" >
</div> <!-- END FOOTER CONTAINER -->
</body>
</html>
</html>
I have been working on this for two hours, I think it may be the MySQL syntax.
Thanks.
have you tried to echo $userid; and see if it outputs anything, because if it doesn't that will answer your question as to why you are always getting "echo 'You have logged out';"
You need to call session_start() on every request before using $_SESSION. I don't see it anywhere in your "Action page".

Categories