<?php
function log_page(){
print_r($_POST);
//connection variables
$host = 'localhost';
$user = 'root';
$password = '';
$db = 'firstwebsite';
//create mysql connection
$mysqli = mysqli_connect($host,$user,$password);
if(!$mysqli){
echo " connection";
}
else{
echo "no connection";
}
$select = mysqli_select_db( $mysqli, $db);
$_SESSION['message']='';
if (isset($_POST['register'])) {
if ($_POST['password'] == $_POST['confirmpassword'])
{
$$username = $mysqli->real_escape_string($_POST['username']);
$email = $mysqli->real_escape_string($_POST['email']);
$password = md5($_POST['password']);
echo $username . " " . $email . " " . $password;
}
}
?>
<link rel="stylesheet" href="log_style.css" type="text/css">
<div class="body-content">
<div class="module">
<h1>Create an account</h1>
<form class="form" action="log_page.php" method="post" autocomplete="off">
<input type="text" placeholder="User Name" name="username" required />
<input type="email" placeholder="Email" name="email" required />
<input type="password" placeholder="Password" name="password" autocomplete="new-password" required />
<input type="password" placeholder="Confirm Password" name="confirmpassword" autocomplete="new-password" required />
<input type="submit" value="Register" name="register" class="btn btn-block btn-primary" />
<input type="submit" value="Already registered? Sign in" name="register2" class="btn btn-block btn-primary" />
</form>
</div>
</div>
<?php
}
?>
my code is not printing anything when i ask for echoing username and email and password. it is not entering the $_POST['register'] if statement. I think the php code is executed before the form values getting executed
Do not use same names in a form. Also you don't need enctype="multipart/form-data" if you are not uploading file in your form.
<form class="form" action="log_page.php" method="post" autocomplete="off">
<input type="text" placeholder="User Name" name="username" required />
<input type="email" placeholder="Email" name="email" required />
<input type="password" placeholder="Password" name="password" autocomplete="new-password" required />
<input type="password" placeholder="Confirm Password" name="confirmpassword" autocomplete="new-password" required />
<input type="submit" value="Register" name="register" class="btn btn-block btn-primary" />
<input type="submit" value="Already registered? Sign in" name="register2" class="btn btn-block btn-primary" />
</form>
Note that this should be in your log_page.php;
if ($_POST['register']) {
if ($_POST['password'] == $_POST['confirmpassword'])
{
$username = $mysqli->real_escape_string($_POST['username']);
$email = $mysqli->real_escape_string($_POST['email']);
$password = md5($_POST['password']);
echo $username . " " . $email . " " . $password; //test purpose
}
}
Related
I have entered the correct username and password but my login page does not respond to the first attempt: it does respond though on th second attempt.
My login page is login.php and landing page is index.php
Here is my html:
<form method="post" >
<input type="text" name="email" class="form-control" placeholder="Username" required />
<input type="password" name="password" class="form-control" placeholder="Password" required />
<button class="btn btn-default submit" >Log In</button>
</form>
Here is my php code
if($_POST){
$email=$_POST['email'];
$password=$_POST['password'];
$select="select * from user_registration where email='$email' and password='$password'";
$res=mysqli_query($con,$select);
$data=mysqli_fetch_array($res);
if($data['email']!=$email or $data['password']!=$password or $data['status']=='Pending')
{
echo "Invalid username or password";
header("location:login.php");
}
elseif($data['email']==$email and $data['password']==$password)
{
$_SESSION['name']=$email;
$_SESSION['user_name']=$data['user_name'];
$_SESSION['user_id']=$data['id'];
header("location:index.php");
}
}
Try using this one.
Your HTML code :
<form method="post" >
<input type="text" name="email" class="form-control" placeholder="Username" required />
<input type="password" name="password" class="form-control" placeholder="Password" required />
<button name="submit" class="btn btn-default submit" >Log In</button>
</form>
And your PHP code :
if(isset($_POST['submit'])){
$email=$_POST["email"];
$password=$_POST["password"];
//do form validation here
$sql="SELECT * FROM user_registration WHERE email='{$email}' AND password='{$password}'";
$res=mysqli_query($con,$sql);
if (mysqli_num_rows($res)==1)
{
$row=mysqli_fetch_assoc($res);
$_SESSION["name"] =$row['email'];
$_SESSION['user_name']=$row['user_name'];
$_SESSION['user_id']=$row['id'];
$_SESSION["login_status"] =TRUE;
header("Location: index.php");
}else{
$msg="Wrong Username and Password";
header("Refresh:0");
}
}
}
If this help you please let me know.
<?php
session_start();
$host = "localhost";
$user = "root";
$password = "";
$database = "tuition";
$conn = mysqli_connect($host, $user, $password, $database);
?>
<div role="tabpanel" class="tab-pane fade in" id="sign_up">
<form action="" method="post">
<input type="text" class="form-control" name="register_name" placeholder="Name" required />
<input type="text" class="form-control" name="register_ic" placeholder="IC" required />
<input type="telephone" class="form-control" name="register_phone" placeholder="Telephone" required />
<input type="email" class="form-control" name="register_email" placeholder="Email" required />
<input type="password" class="form-control" name="register_password" placeholder="Password" required />
<input type="submit" class="form-control" name="register_btn" value="Sign up" />
</form>
</div>
<?php
if(isset($_POST['register_btn'])){
$name = $_POST['register_name'];
$ic = $_POST['register_ic'];
$phone = $_POST['register_phone'];
$email = $_POST['register_email'];
$password = md5($_POST['register_password']);
$result = mysqli_query($conn,"select parent.parent_ic from parent where parent_ic ='$ic'");
if(mysqli_num_rows($result)!=1){
mysqli_query($conn,"insert into parent(parent_name,parent_ic,parent_email,parent_contact_num,parent_password) values ('$name','$ic','$email','$phone','$password')");
?>
<script type="text/javascript">
alert("success");
</script>
<?php
}
else{
?>
<script type="text/javascript">
alert("fail");
</script>
<?php
}
}
?>
It coming out with alert success, but it do not insert the data into the database.But when I hard code insert the data into the database it work! Why?
Here is the result after I click submit button:
You should see by printing error after insert data query
echo("Error description: " . mysqli_error($conn));
I think your MySQL error will be there.
I have a member page that lands after user signs in. From there I need to populate that page with all their data in a form format (same as the one they filled out initially) so they can edit and update/save.
<form>
<fieldset>
<legend>Edit My Account
</legend>
<div>
<label class="label" for="username">Username</label>
<input class="user" type="text" name="username" id="username" value="<?php if(isset($error)){ echo $_POST['username']; } ?>" tabindex="2" required />
</div>
<div>
<label class="label" for="email">Email</label>
<input class="email" type="email" name="email" id="email" value="<?php if(isset($error)){ echo $_POST['email']; } ?>" tabindex="3" required />
</div>
<div>
<label class="label" for="password">Password</label>
<input class="password" type="password" name="password" id="password" tabindex="4" required />
</div>
<div>
<label class="label" for="passwordConfirm">Confirm Password</label>
<input class="password" type="password" name="passwordConfirm" id="passwordConfirm" tabindex="5" required />
</div>
<div>
<input class="showbox" type="checkbox" name="terms" id="terms" tabindex="6" onFocus="this.tabIndex=1;"onBlur="this.tabIndex=6;"required />
<label for="terms">I agree to the Terms</label>
</div>
</fieldset>
<fieldset>
<div>
<input name="submit" type="submit" value="Update" />
</div>
</fieldset>
</form>
Secondly I want them to be able to delete their entire account with a "Delete My Account" button via a input type 'submit' that would appear on same member page.
<fieldset>
<form action="delete.php?" method="post">
<input type="hidden" name="id" value="<?php echo $members['memberID']; ?>">
<input type="submit" name="submit" value="Delete My Account">
</form>
</filedset>
I've been searching for days now... mostly this platform and have not found any sound solution(s).
I'm using MySQL db using PDO $stmt = $db->prepare('INSERT INTO... to create insert for new users and that all works fine.
I include a separate connection config file for db connection as well.
I created a delete.php file for the statement.
<?php require('config.php');
$id=$_SESSION['memberID'];
$stmt = $db->prepare('DELETE FROM members where memberID = $id');
?>
I'm not able to find a solution to populate the member page with logged in user data then edit and update it and/or capture the users logged in memberID to submit the delete account request using that memberID.
Some guidance would be appreciated, Thanks!
Here is my login.php code
<?php
//include config
require_once('config.php');
//check if already logged in move to home page
if( $user->is_logged_in() ){ header('Location: memberpage.php'); }
//process login form if submitted
if(isset($_POST['submit'])){
$username = $_POST['username'];
$password = $_POST['password'];
if($user->login($username,$password)){
$_SESSION['username'] = $username;
header('Location: memberpage.php');
exit;
} else {
$error[] = '<h2 class="red ctr thanks">Wrong username or password or your account has not been activated.</h2>';
}
}//end if submit
?>
At first you must set id user.after login user in admin page
and next you can use of that
<?php
$userId= $_GET['id'];//get user id you can use session also
if (isset($_POST['submit'])){
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$passwordConfirm = $_POST['passwordConfirm'];
$terms = $_POST['terms'];
if (($password===$passwordConfirm) and ($terms===1)){
$query = "UPDATE members SET username = :username ,email = :email,"
."password = :password WHERE id = :id";
$stmt = $db->prepare($query);
$stmt->bindParam(':username',$username, PDO::PARAM_STR);
$stmt->bindParam(':email', $email, PDO::PARAM_STR);
$stmt->bindParam(':password', $password, PDO::PARAM_STR);
$stmt->bindParam(':id',$userId, PDO::PARAM_INT);
}
}
$query = "SELECT * FROM `members` WHERE id = `$userId`"; //Get user info
$sth = $db->prepare($query);
$sth ->execute();
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
if ($result) {
// output data of each row
foreach($result as $row){
$username = $row['username'];
$email = $row['email'];
$password = $row['password'];
}
}
?>
<form method="post" class="form-horizontal" action="<?php filter_input(INPUT_SERVER, 'PHP_SELF', FILTER_SANITIZE_FULL_SPECIAL_CHARS); ?>">
<fieldset>
<legend>Edit My Account
</legend>
<div>
<label class="label" for="username">Username</label>
<input class="user" type="text" name="username" id="username" value="<?php echo $username ?>" tabindex="2" required />
</div>
<div>
<label class="label" for="email">Email</label>
<input class="email" type="email" name="email" id="email" value="<?php echo $email?>" tabindex="3" required />
</div>
<div>
<label class="label" for="password">Password</label>
<input class="password" type="password" name="password" value="<?php echo $password ?>" id="password" tabindex="4" required />
</div>
<div>
<label class="label" for="passwordConfirm">Confirm Password</label>
<input class="password" type="password" name="passwordConfirm" id="passwordConfirm" tabindex="5" required />
</div>
<div>
<input class="showbox" type="checkbox" name="terms" id="terms" tabindex="6" onFocus="this.tabIndex=1;"onBlur="this.tabIndex=6;"required />
<label for="terms">I agree to the Terms</label>
</div>
</fieldset>
<fieldset>
<div>
<input name="submit" type="submit" value="Update" />
</div>
</fieldset>
</form>
The following code does not work as intended, when the submit button of the form is clicked it with no data entered it goes to blog.php instead of showing the error above the form?
<?php
session_start();
include_once('connection.php');
if (isset($_SESSION['logged_in'])){
//display index
} else {
if (isset($_POST['username'], $_POST['password'])) {
$username = $_POST['username'];
$password = $_POST['password'];
if (empty($username) or empty($password)) {
$error = 'All fields are required!';
}
}
}
?>
linked with the following html form
<?php if (isset($error)) { ?>
<small style="color:#aa0000;"><?php echo $error; ?> </small>
<?php } ?>
<form action="blog.php" method="post">
<input type="text" name="username" placeholder="username" />
<input type="password" name="password" placeholder="password" />
<input type="submit" value="login" />
</form>
If the actual validation is being done in admin.php, shouldn't the action be pointing to admin.php?
<?php if (isset($error)) { ?>
<small style="color:#aa0000;"><?php echo $error; ?> </small>
<?php } ?>
<form action="admin.php" method="post">
<input type="text" name="username" placeholder="username" />
<input type="password" name="password" placeholder="password" />
<input type="submit" value="login" />
</form>
I have created a login form for my website but currently, it is not working and I am being directed to invalid.php which shows my authentication has failed.
<?php
session_start();
include("scripts/dbconnect.php");
$numrows=0;
$password=$_POST['password'];
$email=$_POST['email'];
$query="select fname,lname,email from mayan_users where (password='$password' && email='$email')";
$link = mysql_query($query);
if (!$link) {
die('login error');
}
$numrows=mysql_num_rows($link);
if ($numrows>0){ // authentication is successfull
$row = mysql_fetch_array($link, MYSQL_ASSOC);
$_SESSION['user']['fname']=$row['fname'];
$_SESSION['user']['lname']=$row['lname'];
$_SESSION['user']['email']=$row['email'];
header("location:../index2.php");
} else {
header("location:../invalid.php"); // authentication was unsuccessfull
}
?>
and here is my login form
<div id="Loginform" style="background-color:fuchsia; width:100%">
<span id="logspan">
<input type="email" name="email" id="email" placeholder="Email" required />
<input type="password" name="password" id="password" placeholder="Password" required/>
<input type="button" name="submit" style="cursor:pointer" id="submit" value="Log In" onclick="logMeIn()" />
</span>
</div>
Your login may need to be to be wrapped in form tags if you aren't submitting it via some other method.
<form method="post" action="">
<span id="logspan">
<input type="email" name="email" id="email" placeholder="Email" required />
<input type="password" name="password" id="password" placeholder="Password" required/>
<input type="button" name="submit" style="cursor:pointer" id="submit" value="Log In" onclick="logMeIn()" />
</span>
</form>
If your PHP is in another document then set the action to the form processor
action="form-processor.php"
(Thanks njk!)
You don't have the form tags.Add them like this
<form method="POST" action="">
<!-- INSERT INPUTS HERE -->
</form>
Try this:
<form id="Loginform" style="background-color:fuchsia; width:100%">
<span id="logspan">
<input type="email" name="email" id="email" placeholder="Email" required />
<input type="password" name="password" id="password" placeholder="Password" required/>
<input type="button" name="submit" style="cursor:pointer" id="submit" value="Log In" onclick="logMeIn()" />
</span>
</form>
First of all, I suggest you to use PDO if your PHP version support it.
Then I suppose that your javascript function sends in the correct way the parameters.
Finally, you could avoid to check the number of rows. Simply
if($row = mysql_fetch_array($link))
{
...
}
else
{
header("location: ../invalid.php");
exit; //It's a good practice
}