My MySql, php combination is not working properly [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
So i wanted to make a social network and followed some youtube videos from codeetastic, but at the forth movie, my login function didn't react properly. Do you guys have any idea?
(index.php)
<?php include "templates/nav.php"; ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>EverTime</title>
<link rel="stylesheet" href="css/main.css"/>
<script src="js/main.js"></script>
</head>
<body>
<div id="middle">
<h1 id="welcome">Welcome to EverTime!</h1>
<form action="parse/login.php" method="post">
<input type="text" name="username" id="username" placeholder="Username..."/>
<input type="password" name="password" id="password" placeholder="Password..."/>
<input type="submit" name="submit" id="submit" value="Log in"/><a id="link" href="register.php">Create an account</a>
</form>
</div>
</body>
</html>
(login.php)
<?php
//connect
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("evertime") or die(mysql_error());
//login script
if(isset($_POST['username'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$md5pass = md5($password);
$query = mysql_query("SELECT * FROM `users` WHERE username='$username' AND password='$md5pass'");
$get = mysql_fetch_assoc($query);
if($get = ""){
echo "User does not exist";
} else {
echo "User does exist";
}
}
?>
(config.php)
<?php
//connect to database
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("evertime") or die(mysql_error());
?>
Btw, i am using xampp with php 5.4.15 and mysql (not mysql*i*)

if($get = ""){
should probably be
if($get == ""){

Related

How to Validate an ID (Primary Key) in php? [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 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>

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

How to fix "syntax error, unexpected end of file" error in PHP? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I get this error and have tried all steps to eliminate it but can't, have also tried the steps demonstrated on stack overflow but to no avail:
error:
Parse error: syntax error, unexpected end of file in C:\wamp\www\project-Feedback form\project docs\login.php on line 80
I have attached the entire snippet of code:
<?php
session_start(); // Starting Session
$error=''; // Variable To Store Error Message
?>
<html>
<head>
<title>Login Page</title>
<link rel="stylesheet" type="text/css" href="login.css">
</head>
<body>
<div id="header">
<center><p><img src="http://www.ruparel.edu/_/rsrc/1338008090424/config/customLogo.gif?revision=29" align="left" /> <img id="image" src="http://www.auplod.com/u/pudlao4d2f7.png" /> </p></center>
</div>
<hr></hr>
<center><h1> Welcome to the Login page for <img src="http://www.auplod.com/u/uoalpd4d31a.png" /> </h1></center>
<center><h2>Please Login!</h2></center>
` <br />
<form method="post" action=" ">
<div id="radio"><b><img src="https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcQMfjN-cd00MPhHqoKOKnUHp-VP3HSU3FBuuNDJbJL2yQF0fyWR"/>Student<input type="radio" name="radio1" value="Student"></p>
<b><img src="https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcSSnk6-xJ7aCZL8aODuWyRMAENzk7XIFitACfPJALdJOaTzQq1b-w" />Professor<input type="radio" name="radio1" value="Professor"></b> </div>
<div id="fields"><b>ROll no:<input type="text" name="username" ></b>
<b>Password:<input type="password" name="pwd"></b><b> <input type="submit" name="submit" value="Login"></b></div>
</form>
<?php
if (isset($_POST['submit']))
{
if (empty($_POST['username']) || empty($_POST['password']))
{
$error = "Username or Password is invalid";
}
else
{
// Define $username and $password
$username=$_POST['username'];
$password=$_POST['password'];
// Establishing Connection with Server by passing server_name, user_id and password as a parameter
$connection = mysqli_connect("localhost", "root", "");
// To protect MySQL injection for Security purpose
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysqli_real_escape_string($username);
$password = mysqli_real_escape_string($password);
// Selecting Database
$db = mysqli_select_db($connection,"feedback data");
// SQL query to fetch information of registerd users and finds user match.
$query = mysqli_query($connection,"select * from login where password='$password' AND username='$username'");
$rows = mysqli_num_rows($query);
if ($rows == 1) {
$_SESSION['login_user']=$username; // Initializing Session
//now we write php code to decide if student or teacher
$subject=$_SESSION['login_user'];
$pattern="/[2$]/";
if($_POST['radio1']=="Professor"&& preg_match($pattern,$subject)==1)//condition for teacher using BRE
{
header("location: http://localhost/project-Feedback%20form/project%20docs/selection_page_teacher.php");
}
elseif($_POST['radio1']=="Student"&& preg_match($pattern,$subject)==0)//condition for student using BRE
{
header("location: http://localhost/project-Feedback%20form/project%20docs/selection_page_student.php");
}
else
{
echo"The selection you have made is incorrect Mr.".$_SESSION['login_user'];
}
mysql_close($connection); // Closing Connection
}
}
?>
</body>
</html>
You're missing a closing brace } for one of your conditional statements, being for this one:
if (isset($_POST['submit']))

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."'");

change password html form in php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Im trying to create an html form to change password in php.
my database is created in easyphp.
Here is the code to I wrote to change the password, when i test it nothing happened or changed.
<?
$user_name = "root";
$pass_word = "";
$database = "login";
$server = "127.0.0.1";
if(isset($_POST['submit']))
{
$oldpassword = md5($_POST['cur_password']);
$newpassword= md5($_POST['new_password']);
$confirm_password = md5($_POST['confirm_password']);
$usermane = $_SESSION['username'];
$con = mysqli_connect($server, $user_name, $pass_word,$database);
if (!$con)
{
die('Could not connect: ' . mysqli_connect_error());
}
$select=mysqli_query("select * from login where L1='$username'");
$fetch=mysqli_fetch_array($select);
$data_password=md5($fetch['password']);
if($newpassword==$confirm_password && $data_password==$oldpassword)
{
$insert=mysqli_query("update login set L2='$confirm_password' where L1='$username'");
}
if($insert)
{
echo "Password changed";
}
else
{
echo "Password not changed";
}
}
}
mysqli_close($con);
?>
<html>
<head>
</head>
<body>
<FORM NAME ="form1" METHOD ="POST" ACTION ="changepassword.php">
<p>old password<br />
<input type="password" name="current_password" /></p>
<p>New password<br />
<input type="password" name="new_password"/>
</p>
<p>Confirm password<br />
<input type="password" name="confirm_password"/>
</p>
<input name="submit" type="submit" value="Save Password" />
</body>
</html>
Please tell me what is wrong here.
Thanks :D
$usermane on line 12. Looks like a syntax error. Shouldn't you be getting any errors?
Also usermane and username mismatch
$usermane = $_SESSION['username'];
$select=mysqli_query("select * from login where L1='$username'");
change
$data_password=md5($fetch['password']);
to
$data_password=$fetch['password'];
you are already storing the passwords as hashes (at least according to your insert statement), when you take the hash of a hash you just get a new hash.
which causes $data_password to not equal $oldpassword
if($newpassword==$confirm_password && $data_password==$oldpassword)
{
$insert=mysqli_query("update login set L2='$confirm_password' where L1='$username'");
}
also as Eisa Adil pointed out
$usermane
should be
$username

Categories