This code defined as edit loggedin users profile. Show all info of loggedin user as well all code running without error. but doesn't update the users info.
show success message that it is updated. but actually no update occur in the database.
and in the URL not show any id refer that is now editing .
edit.php
<?php
// We need to use sessions, so you should always start sessions using the below code.
session_start();
// If the user is not logged in redirect to the login page...
if (!isset($_SESSION['loggedin']))
{ header('Location: index.php');
exit();
}
//iconic19base Connection
require 'inc/connect.php';
require 'inc/head.php';
// We don't have the phone or email info stored in sessions so instead we can get the results from the database.
$stmt = $conn->prepare('SELECT student_id, name, name_bangla, photo ,phone, email, blood, district, address, address_bangla FROM iconic19 WHERE id = ?');
// In this case we can use the account ID to get the account info.
$stmt->bind_param('s', $_SESSION['id']);
$stmt->execute();
$stmt->bind_result($student_id, $name, $name_bangla, $photo, $phone, $email, $blood, $district, $address, $address_bangla);
$stmt->fetch();
$stmt->close();
//Get student_id from iconic19base
if(isset($_SESSION['id'])){
$sql = "SELECT * FROM iconic19 WHERE id =" .$_SESSION['id'];
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($result);
}
//Update Information
if(isset($_POST['btn-update'])){
$student_id = $_POST['student_id'];
$name = $_POST['name'];
$name_bangla = $_POST['name_bangla'];
$blood = $_POST['blood'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$district = $_POST['district'];
$address_bangla = $_POST['address_bangla'];
$address = $_POST['address'];
$update = "UPDATE iconic19 SET student_id='$student_id', name='$name',name_bangla='$name_bangla',blood='$blood',email='$email',phone='$phone',district='$district',address_bangla='$address_bangla',address='$address' WHERE id=". $_SESSION['id'];
$up = mysqli_query($conn, $update);
if(!isset($sql)){
die ("Error $sql" .mysqli_connect_error());
}
else
{
header("location: home.php");
}
}
?>
<!--Create Edit form -->
<!doctype html>
<html>
<body>
<div class="center-login">
<h2 class='header-2'>১৯ এর ডায়েরি</h2>
<form id="edit" method="post">
<label>Student ID:</label><input type="text" name="student_id" placeholder="student_id" value=" <?=$student_id?>" maxlength="10" readonly><br/><br/>
<label>Name:</label><input type="text" name="name" placeholder="name" value="<?=$name?>" required><br/><br/>
<label>নাম:</label><input type="text" name="name_bangla" placeholder="name_bangla" value="<?=$name_bangla?>" required><br/><br/>
<label>Blood:</label><input type="text" name="blood" placeholder="blood" value="<?=$blood?>" required><br/><br/>
<label>Email:</label><input type="email" name="email" placeholder="email" value="<?=$email?>" required><br/><br/>
<label>Phone:</label><input type="tel" name="phone" placeholder="phone" value="<?=$phone?>" maxlength="11" required><br/><br/>
<label>District:</label><input type="text" name="district" placeholder="district" value="<?=$district?>" required><br/><br/>
<label>Address_bangla:</label><input type="text" name="address_bangla" placeholder="address_bangla" value="<?=$address_bangla?>" required></input>
<label>Address:</label><input type="text" name="address" placeholder="address" value="<?=$address?>" required></input>
<button type="submit" name="btn-update" id="btn-update" onClick="update()"><strong>Update</strong></button>
<button type="button" value="button">Cancel</button>
</form>
</div>
</body>
<!-- Alert for Updating -->
<script>
function update(){
var x;
if(confirm("Updated ১৯ এর ডায়েরি Sucessfully") == true){
x= "update";
}
}
</script>
</body>
<style>
#edit button[type="button"] {
background: #b10e08;
cursor: pointer;
width: 100%;
border: none;
color: #FFF;
margin: 0 0 5px;
padding: 10px;
font-size: 15px;
}
.center-login {
max-width: 400px;
width: 100%;
margin: 0 auto;
position: unset;
left: 0;
top: 0;
transform: unset;
padding: unset;
background: unset;
border-radius: unset;
}
#edit input[type="text"],
#edit input[type="email"],
#edit input[type="tel"],
#edit input[type="url"],
#edit input[type="number"],
#edit textarea,
#edit button[type="submit"] {
font: 400 12px/16px "Roboto", Helvetica, Arial, sans-serif;
}
#edit {
background: #F9F9F9;
padding: 25px;
box-shadow: 0 0 20px 0 rgba(0, 0, 0, 0.2), 0 5px 5px 0 rgba(0, 0, 0, 0.24);
border-radius: 4px;
}
#edit h3 {
display: block;
font-size: 30px;
font-weight: 300;
margin-bottom: 10px;
}
#edit h4 {
margin: 5px 0 15px;
display: block;
font-size: 13px;
font-weight: 400;
}
fieldset {
border: medium none !important;
margin: 0 0 10px;
min-width: 100%;
padding: 0;
width: 100%;
}
#edit input[type="text"],
#edit input[type="email"],
#edit input[type="tel"],
#edit input[type="url"],
#edit input[type="number"],
#edit textarea {
width: 100%;
border-radius: 4px;
border: 1px solid #ccc;
background: #FFF;
margin: 0 0 5px;
padding: 10px;
}
#edit input[type="text"]:hover,
#edit input[type="email"]:hover,
#edit input[type="tel"]:hover,
#edit input[type="url"]:hover,
#edit input[type="number"]:hover,
#edit textarea:hover {
-webkit-transition: border-color 0.3s ease-in-out;
-moz-transition: border-color 0.3s ease-in-out;
transition: border-color 0.3s ease-in-out;
border: 1px solid #aaa;
}
#edit textarea {
height: 100px;
max-width: 100%;
resize: none;
}
#edit button[type="submit"] {
cursor: pointer;
width: 100%;
border: none;
background: #4CAF50;
color: #FFF;
margin: 0 0 5px;
padding: 10px;
font-size: 15px;
}
#edit button[type="submit"]:hover {
background: #43A047;
-webkit-transition: background 0.3s ease-in-out;
-moz-transition: background 0.3s ease-in-out;
transition: background-color 0.3s ease-in-out;
}
#edit button[type="submit"]:active {
box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.5);
}
.copyright {
text-align: center;
}
#edit input:focus,
#edit textarea:focus {
outline: 0;
border: 1px solid #aaa;
}
::-webkit-input-placeholder {
color: #888;
}
:-moz-placeholder {
color: #888;
}
::-moz-placeholder {
color: #888;
}
:-ms-input-placeholder {
color: #888;
}
</style>
</html>
Related
Im trying to make a field longer if there's an error so I can fit the error message in it.
In addAccToDB.php I set $error to "Wachtwoorden komen niet overeen." if the 2 entered passwords do not match and I redirect the user back to createaccount.php. Back in createaccount.php $error appearently is still not set though so it doesn't add the class "extend" to the field to make it longer.
This is what I tried:
createaccount.php:
<html>
<head>
<title>Account maken</title>
<link href="accounts.css" rel=stylesheet>
</head>
<body>
<div class="field <?php if (isset($error)){echo "extend";}?>" id=createaccount >
<h1 class="title">Account aanmaken</h1>
<h4 class="undertitle">Als je een leraar bent kun je hier je account aanmaken.
Als je een leerling bent moet een leraar je account maken.</h4>
<form action="addAccToDB.php">
Voornaam:<br>
<input class="input" placeholder="Voornaam" type="text" name="firstname"><br>
Achternaam:<br>
<input class="input" placeholder="Achternaam" type="text" name="lastname"><br>
Email:<br>
<input class="input" placeholder="Email" type="text" name="email"><br>
Wachtwoord:<br>
<input class="input" placeholder="Wachtwoord" type="password" name="pass"><br>
Herhaal wachtwoord:<br>
<input class="input" placeholder="Herhaal wachtwoord" type="password" name="repass"><br>
<input class="submit" type="submit" value="Account aanmaken">
</form>
</div>
</body>
</html>
addAccToDB.php:
<?php
include "db_connection.php";
if ($_GET['pass'] == $_GET['repass']){
$Voornaam = $_GET['firstname'];
$Achternaam = $_GET['lastname'];
$Email = $_GET['email'];
$Wachtwoord = $_GET['pass'];
mysqli_query(OpenCon(),"INSERT INTO accounts (Firstname, Lastname, Email, Password, Type)
VALUES ('$Voornaam', '$Achternaam', '$Email', '$Wachtwoord',0)");
} else {
$error = "Wachtwoorden komen niet overeen.";
header("Location: createaccount.php");
}
accounts.css:
#font-face {
font-family: Torus Regular;
src: url("Torus Regular.otf");
}
body {
background-image: url('RekensiteBackground.png');
}
.field {
font-family: "Torus Regular";
margin-top: 25px;
color: white;
text-align: center;
position: absolute;
left: 50%;
transform: translate(-50%, 0%);
width: 600px;
border-radius: 16px;
font-size: 17px;
background-color: #333;
}
#signin {
height: 450px;
}
#createaccount {
height: 720px;
}
.extend {
animation:extend;
animation-duration: 500ms;
animation-fill-mode: forwards;
}
#keyframes extend{
to {height: 750px;}
}
#forgotpass {
height: 350px;
}
.title {
font-size: 35px;
}
.undertitle {
font-size: 20px;
position: relative;
transform: translate(-50%, -50%);
left: 50%;
width: 550px;
margin-top: 30px;
margin-bottom: 20px;
}
.input {
font-family: "Torus Regular";
font-size: 20px;
color: white;
background-color: #222;
border: none;
border-radius: 7px;
padding: 7px 10px;
width: 20em;
height: 2em;
margin-bottom: 25px;
margin-top: 5px;
}
.submit {
font-family: "Torus Regular";
color: white;
font-size: 20px;
height: 55px;
width: 350px;
position: absolute;
transform: translate(-50%, -50%);
margin-top: 35px;
left: 50%;
background-color: red;
border: none;
border-radius: 10px;
}
.submit:hover {
cursor: pointer;
animation: hover 200ms;
animation-fill-mode: forwards;
}
.signinlinks {
font-family: "Segoe UI";
font-size: 16px;
position: absolute;
transform: translate(-50%, -50%);
left: 50%;
width: 500px;
margin-top: 100px;
}
.error {
}
.hyperlinks:link {
text-decoration: none;
color: rgb(255, 204, 34);
}
.hyperlinks:hover {
text-decoration: underline;
}
.hyperlinks:visited {
color: rgb(255, 204, 34);
}
#keyframes hover {
from {
filter: brightness(100%);
}
to {
filter: brightness(85%);
}
}
The HTTP protocol is stateless. That means you cannot save data in variable for later. You can use a session to store values in siuations like this one. Check PHP Sessions for more info.
In short:
Start session if you did not already done it.
Save message in session $_SESSION["error"] = "Error message";
Check and echo error value
if isset($_SESSION["error"]) { echo $_SESSION["error"]; }
I made a header design in html file and everything worked nice, but when I put it in .php file they became like idle so I can't click or hover them. PHP code:
<?php
session_start();
require_once "stuff.php";
if (isset($_SESSION['encnick'])){
header("Location: my.php");
return;
}
//On form submit
if (isset($_POST['register'])){
if (!isset($_POST['nick'])){
$_SESSION['error'] = "Nickname is required";
header("Location: register.php");
return;
}
if (!isset($_POST['password'])){
$_SESSION['error'] = "Password is required";
header("Location: register.php");
return;
}
if (!isset($_POST['cpassword'])){
$_SESSION['error'] = "Password is required";
header("Location: register.php");
return;
}
if ($_POST['password'] !== $_POST['cpassword']){
$_SESSION['error'] = "Passwords don't match";
header("Location: register.php");
return;
}
else{
$statement = $pdo->prepare("SELECT * FROM users WHERE nick = :nick");
$statement->execute(array(':nick' => $_POST['nick']));
$row = $statement->fetch(PDO::FETCH_ASSOC);
if ($row['nick'] === $_POST['nick']){
$_SESSION['error'] = "This nickname is already taken";
header("Location: register.php");
return;
}
$statement = $pdo->prepare("INSERT INTO users (nick, password, company, encodednick, user_group) VALUES (:nick, :password, :company, :encodednick, :user_group)");
$statement->execute(array(
':nick' => htmlentities($_POST['nick']),
':password' => password_hash($_POST['password'], PASSWORD_BCRYPT),
':company' => htmlentities($_POST['company']),
':encodednick' => md5($_POST['nick']),
':user_group' => "user"
));
$_SESSION['encnick'] = md5($_POST['nick']);
$encnick = $_SESSION['encnick'];
header("Location: my.php?id=$encnick");
return;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>ReactSynchro</title>
<link rel="stylesheet" href="css/register.css">
<link rel="stylesheet" href="css/fonts.css">
<link rel="stylesheet" href="css/main.css">
</head>
<body>
<?php printheader() ?>
<div id="formdiv">
<form method="post">
<label for="nick">Nickname</label>
<input type="text" name="nick" id="nick" placeholder="Enter unique nickname" minlength="6" maxlength="30" required>
<label for="password">Password</label>
<input type="password" name="password" id="password" placeholder="Enter password" minlength="8" maxlength="60" required>
<label for="cpassword">Confirm Password</label>
<input type="password" name="cpassword" id="cpassword" placeholder="Confirm password" minlength="8" maxlength="50" required>
<label for="company">Company</label>
<input type="text" name="company" id="company" placeholder="Enter your company" minlength="3" maxlength="50">
<input type="submit" value="Register" name="register" id="regbtn">
</form>
</div>
<?php
if (isset($_SESSION['error'])){
echo "<h2 id='errormessage'>".$_SESSION['error']."</h2>";
unset($_SESSION['error']);
}
?>
</body>
</html>
main.css code:
body{
background-color: #333;
margin: 0;
}
#header{
width: 100%;
position: fixed;
top: 0;
left: 0;
right: 0;
background-color: #444;
height: 110px;
}
#header img{
height: 100px;
position: relative;
left: 5px;
top: 5px;
bottom: 5px;
}
#header a{
color: white;
text-decoration: none;
display: block;
position: relative;
float: left;
text-align: center;
margin: 5px;
padding: 5px;
border: 0.5px solid white;
font-family: 'Ubuntu';
font-size: 20px;
-webkit-transition: background-color 200ms linear;
-ms-transition: background-color 200ms linear;
transition: background-color 200ms linear;
-moz-transition: background-color 200ms linear;
-o-transition: background-color 200ms linear;
}
#hlinks1{
position: relative;
left: 120px;
top: -71px;
}
#hlinks2{
position: absolute;
right: 5px;
top: 50%;
transform: translateY(-50%);
}
#hlinks2 a{
background-color: rgba(255, 140, 0, 0.9);
}
#hlinks1 a:hover{
background-color: rgba(255, 255, 255, 0.1);
}
#hlinks2 a:hover{
background-color: rgba(255, 140, 0, 1);
}
#errormessage{
color: red;
text-align: center;
font-family: 'Ubuntu';
}
#media all and (max-width: 537px){
#hsupbtn{
visibility: hidden;
}
}
#media all and (max-width: 440px){
#hregbtn{
visibility: hidden;
}
}
register.css code:
#formdiv{
width: 50%;
height: 350px;
position: absolute;
left: 0;
right: 0;
margin: auto;
top: 50%;
transform: translateY(-50%);
border: 0.5px solid white;
max-width: 500px;
min-width: 250px;
}
#formdiv label{
text-align: center;
font-family: 'Ubuntu';
color: white;
width: 100%;
display: block;
font-size: 24px;
line-height: 50px;
}
#formdiv input{
width: 90%;
position: relative;
margin: auto;
display: block;
font-family: 'Ubuntu';
line-height: 20px;
}
#regbtn{
width: 50% !important;
top: 20px;
background-color: rgba(255, 140, 0, 0.9);
color: white;
-webkit-transition: background-color 200ms linear;
-ms-transition: background-color 200ms linear;
transition: background-color 200ms linear;
-moz-transition: background-color 200ms linear;
-o-transition: background-color 200ms linear;
}
#regbtn:hover{
background-color: rgba(255, 140, 0, 1);
}
#formdiv input:empty{
border: 0.5px solid white;
}
#formdiv input:valid{
border: 0.5px solid green;
}
The menu printing function in stuff.php file:
function printheader(){
echo "<div id='header'>
<img src='images/logo.png'>
<div id='hlinks1'>
<a href='../index.php'>Home</a>
<a href='../about.html'>About</a>
<a href='../support.php' id='hsupbtn'>Support</a>
</div>
<div id='hlinks2'>
<a href='../register.php' id='hregbtn'>Register</a>
<a href='../login.php'>Login</a>
</div>
</div>
<div style='height: 110px; position: relative;'></div>";
}
So I mean that when I hover or click links nothing happens at all, they are idle like pictures, but if I disconnect main.css file they start working normally.
You have following in your printheader function
<div style='height: 110px; position: relative;'></div>
Remove the following:-
position: relative
I've been working on a system that allows a user to insert data, which will pass into a MySql database using SQL querieshowever, I've noticed that every time the page is refreshed, empty data is inserted into the database. Connection, queries, PHP, HTML and every other code works. The redirection works as intended but I realise my system just needs a slight correction. Am I doing something wrong?
<?php
$con = mysqli_connect("localhost","root","","system");
$error = 0;
$errormessage = "";
if( isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_GET['email'];
$password = $_POST['password'];
$password = hash('sha256' , $password);
}else{
$errormessage = $errormessage . "Please Fill all Fields";
$error++;
}
if($errors == 0){
$sql = "INSERT INTO User
(name, email, password)
VALUES
('$name','$email','$password')";
mysqli_query($con, $sql) or die (mysqli_error($con));
$errormessage = "Data Successfully Entered";
}
?>
<!DOCTYPE html>
<html>
<body>
<header>
<div style="background-color:black"><div style="text-align:center;"> <h1><style="color:Black;">Register</h1></div></div> </header>
<style>
input[type=text], select {
width: 50%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 5px solid magenta;
border: 5px solid magenta;
border-radius: 4px;
box-sizing: border-box;
align:center;
}
input[type=button] {
width: 50%;
background-color: #4CAF50;
color: white;
padding: 14px 40px;
margin: 8px 0;
border: none;
border-radius: 4px;
cursor: pointer;
align:center;
}
input[type=password], select {
width: 50%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 5px solid magenta;
border: 5px solid magenta;
border-radius: 4px;
box-sizing: border-box;
align:center;
}
input[type=email], select {
width: 50%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 5px solid magenta;
border: 5px solid magenta;
border-radius: 4px;
box-sizing: border-box;
align:center;
}
input[type=submit] {
width: 50%;
background-color: #4CAF50;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
border-radius: 4px;
cursor: pointer;
align:center;
}
input[type=submit]:hover {
background-color: green;
}
div {
margin-left:auto;
margin-right:auto;
width: 90%;
border: 5px solid lime;
border-radius: 5px;
background-color: #ECF0F1;
padding: 20px;
}
</style>
</nav>
<div>
<form method="Post">
<label for="name">
<div style="text-align:center;"><input type= "button" id="RegisterPage" value = "Go to Register Page" onclick="document.location.href='example.php'">
<input type= "button" id="LoginPage" value = "Go to Login Page" onclick="document.location.href='http://localhost/example2.php'"><p>Your Name</label><br>
<input type="text" id="name" name="name" placeholder="Enter Name" required><br>
<label for="email">E-Mail:</label><br>
<input type="email" id="email" name="email" placeholder="Your e-mail" required><br>
<label for="password">Password:</label><br>
<input type="password" name="password" placeholder="At least 6 characters" required><br>
<input type="submit" name = "submit" value="Create your Account ">
</form></div>
<footer><div style="background-color:aqua; border: 5px solid black;">
</form></div>
</footer>
</nav>
</body>
</html>
You can do a ON DUPLICATE KEY UPDATE with your insert statement to update the database records instead of entering more. Also, make sure to setup your primary keys in your DB.
$sql = "INSERT INTO User
(name, email, password)
VALUES
('$name','$email','$password')
ON DUPLICATE KEY UPDATE
name = `$name`, email = `$email`, `$password`";
Created a mail form for a contact section. There's a mark tag after the form that displays after validating that all required fields aren't satisfied and lists which ones need to be satisfied.
Upon submitting though, if values are entered, they are echoed as they're suppose to, HOWEVER the mark tag does not display...
I need help with having the mark tag display when there are errors...
HTML:
<form method="post" action="">
<input type="text" name="name" placeholder="Name" value="<?php echo $_POST['name']; ?>">
<input type="tel" name="phone" placeholder="Phone Number" value="<?php echo $_POST['phone']; ?>">
<input type="email" name="email" placeholder="Email" value="<?php echo $_POST['email']; ?>">
<input type="text" name="invoice" placeholder="Invoice Number (optional)" value="<?php echo $_POST['invoice']; ?>">
<textarea name="comments" maxlength="500" rows="10" cols="10" placeholder="Please enter your comments here..." value="<?php echo $_POST['comments'];?>"></textarea>
<button type="submit">Submit</button>
</form>
<mark><?php echo $MSG[0]; ?></mark>
PHP:
<?php
if(!empty($_POST)) {
$POST=filter_post($_POST);
$MSG=check_empty($POST);
if(!array_filter($MSG)) {
send_mail($POST);
if(send_mail($POST)) {
$MSG[]="Email Success";
}
else {
$MSG[]="Email Failed";
}
}
}
function filter_post($POST) {
$keys=array('name', 'phone', 'email', 'invoice', 'comments');
$POST=array_intersect_key($POST, array_flip($keys));
$POST=array_map('strip_tags', $POST);
return($POST);
}
function check_empty($POST) {
foreach($POST as $key=> $value) {
if(empty($value)) {
$MSG[]="You need to fill out the $key section";
}
}
return($MSG);
}
function send_mail($POST) {
extract($POST);
$to='7servicerepairs#gmail.com';
$sbj='New Question For Se7en Service!';
$msg="Name: $name \n Phone: $phone \n Email: $email \n Invoice #: $invoice \n Comments: $comments";
$headers="From: $email";
return(mail($to, $sbj, $msg, $headers));
}
?>
CSS:
form{
cursor: default !important;
display: -webkit-flex;
display: flex;
-webkit-justify-content: center;
justify-content: center;
-webkit-align-self: center;
align-self: center;
-webkit-flex-direction: column;
flex-direction: column;
background: #ECF0F1;
-webkit-box-shadow: 0px 2px 7px rgba(0, 0, 0, 0.40);
box-shadow: 0px 2px 7px rgba(0, 0, 0, 0.40);
-webkit-border-radius: 0.3em;
border-radius: 0.3em;
padding: 1.3em;
}
form>input{
width: 22.1em;
height: 2.5em;
margin: 0.2em 0;
font-size: 1em;
-webkit-font-smoothing: antialiased;
font-family: HelveticaNeue-Light,"Helevetica Neue",Helvetica,Arial;
text-align: center;
border: 1px solid #d5dadc;
-webkit-border-radius: 2px;
border-radius: 2px;
color: #7C7C7C;
outline: none;
}
form>button{
width: 22.35em;
height: 2.5em;
margin: 0.2em 0;
padding: 0;
font-size: 1em;
-webkit-font-smoothing: antialiased;
font-family: HelveticaNeue-Light,"Helevetica Neue",Helvetica,Arial;
cursor: pointer;
outline: none;
border: none;
color: #fff;
background: #2ECC71;
-webkit-border-radius: 2px;
border-radius: 2px;
}
form>textarea{
margin: 0.2em 0;
font-size: 1em;
-webkit-font-smoothing: antialiased;
font-family: HelveticaNeue-Light,"Helevetica Neue",Helvetica,Arial;
border: 1px solid #d5dadc;
-webkit-border-radius: 2px;
border-radius: 2px;
color: #7C7C7C;
outline: none;
max-width: 22em;
}
mark{
display: -webkit-flex;
display: flex;
-webkit-justify-content: center;
justify-content: center;
-webkit-align-self: center;
align-self: center;
height: 1.5em;
margin: 1.5em;
font-size: 1em;
color: #fff;
-webkit-font-smoothing: antialiased;
font-family: HelveticaNeue-Light,"Helevetica Neue",Helvetica,Arial;
background: none;
}
White letters over no background? It is surely there, you just can't distinguish it.
mark {
...
color: #ff0000;
...
}
http://jsfiddle.net/2hsh4zmb/
i can not get my Login script to login... i have an index.php with a register form and a login form, the register form works perfectly, but it seems like the login form does not get the information from the database when you enter the "login" button, when logging in you is redirectet to "home.php" which wil show your username with help of sessions. but i get this error "Notice: Undefined variable: username in home.php on line 12"... I think its because its not logging in and the session gets an undefined variabel. I just cant find where the problem is
i have a database named "thesozializer"
and the sql for the table is:
CREATE TABLE IF NOT EXISTS users (
id int(11) NOT NULL,
username varchar(255) NOT NULL,
first_name varchar(255) NOT NULL,
last_name varchar(255) NOT NULL,
email varchar(255) NOT NULL,
password varchar(32) NOT NULL,
sign_up_date date NOT NULL,
activated enum('0','1') NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=latin1;
index.php looks like this:
<?php
mysql_connect("localhost","root","") or die("couldn't connect to database.");
mysql_select_db("thesocializer") or die("couldn't select database");
$reg = #$_POST['reg'];
//declaring variables to prevent errors
$fn = ""; //First Name
$ln = ""; //Last Name
$un = ""; //Username
$em = ""; //Email
$em2 = ""; //Email 2
$pswd = ""; //Password
$pswd2 = ""; // Password 2
$d = ""; // Sign up Date
$u_check = ""; // Check if username exists
//registration form
$fn = strip_tags(#$_POST['fname']);
$ln = strip_tags(#$_POST['lname']);
$un = strip_tags(#$_POST['username']);
$em = strip_tags(#$_POST['email']);
$em2 = strip_tags(#$_POST['email2']);
$pswd = strip_tags(#$_POST['password']);
$pswd2 = strip_tags(#$_POST['password2']);
$d = date("Y-m-d"); // Year - month - day
if ($reg) {
if ($em==$em2) {
// Check if user already exists
$u_check = mysql_query("SELECT username FROM users WHERE username='$un'");
// Count the amount of rows where username = $un
$check = mysql_num_rows($u_check);
//Check whether Email already exists in the database
$e_check = mysql_query("SELECT email FROM users WHERE email='$em'");
//Count the number of rows returned
$email_check = mysql_num_rows($e_check);
if ($check == 0) {
if ($email_check == 0) {
//check all of the fields have been filed in
if ($fn&&$ln&&$un&&$em&&$em2&&$pswd&&$pswd2) {
// check that passwords match
if ($pswd==$pswd2) {
// check the maximum length of username/first name/last name does not exceed 25 characters
if (strlen($un)>25||strlen($fn)>25||strlen($ln)>25) {
echo "maximum length of username/first name/last name is 25 characters!";
}
else
{
// check the maximum length of password does not exceed 25 characters and is not less than 5 characters
if (strlen($pswd)>30||strlen($pswd)<5) {
echo "Password must be between 5 and 25 characters!";
}
else
{
//encrypt password and password 2 using md5 before sending to database
$pswd = md5($pswd);
$pswd2 = md5($pswd2);
$query = mysql_query("INSERT INTO users VALUES ('','$un','$fn','$ln','$em','$pswd','$d','0')");
die("<h2>welcome to The Socializer!</h2>Login to get started");
}
}
}
else {
echo "your passwords is incorrect";
}
}
else
{
echo "fill in all fields";
}
}
else
{
echo "email already in use";
}
}
else
{
echo "username already in use";
}
}
else {
echo "The emails is not alike!";
}
}
//User Login Code
if (isset($_POST["user_login"]) && isset($_POST["password_login"])) {
$user_login = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["user_login"]);
$password_login = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["password_login"]);
$password_login_md5 = md5($password_login);
$sql = mysql_query("SELECT id FROM users WHERE username='$user_login' AND password='$password_login_md5' LIMIT 1");
//Check for their existance
$userCount = mysql_num_rows($sql); //Count the number of rows returned
if ($userCount == 1) {
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
}
$_SESSION["user_login"] = $user_login;
header("location: home.php");
exit();
}
else {
echo 'username or password is incorrect';
exit();
}
}
session_start();
if (!isset($_SESSION["user_login"])) {
}
else
{
$username = $_SESSION["user_login"];
}
?>
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#registrer-deg").click(function(){
$("#registrerdeg").show();
});
$("#registrer-deg").click(function(){
$("#logginn").hide();
});
$("#logg-inn").click(function(){
$("#logginn").show();
});
$("#logg-inn").click(function(){
$("#registrerdeg").hide();
});
});
</script>
<link rel="stylesheet" type="text/css" href="main.css"/>
<title>The Socializer</title>
</head>
<body>
<div id="sidebarLeft">
<div id="logo"></div>
<ul>
<li>Login</li>
<li>Register</li>
<li>About</li>
<li>Contact</li>
</ul>
</div>
<div id="timeline">
<div id="registrering">
<form id="registrerdeg" action="index.php" method="POST" style="display: none;">
<input type="text" name="fname" size="10" placeholder="First name"><br/>
<input type="text" name="lname" size="10" placeholder="Last name"><br/>
<input type="text" name="username" size="10" placeholder="Username"><br/>
<input type="text" name="email" size="10" placeholder="Email"><br/>
<input type="text" name="email2" size="10" placeholder="Confirm email"><br/>
<input type="text" name="password" size="10" placeholder="Password"><br/>
<input type="text" name="password2" size="10" placeholder="Confirm Password"><br/>
<input type="submit" name="reg" value="Registrer!">
</form>
</div>
<div id="logg_inn">
<form id="logginn" action="index.php" method="POST" style="display: none;">
<input type="text" name="user_login" size="10" placeholder="Username"><br/>
<input type="text" name="password_login" size="10" placeholder="Password"><br/>
<input type="submit" name="login" value="Logg inn!">
</form>
</div>
</div>
</body>
</html>
* {
background-color: #2C3E50;
font-family: Arial, Helvetica, Sans-serif;
font-size: 16px;
color: #AFEEEE;
}
#sidebarLeft {
width: 220px;
height: 550px;
top: 0;
left: 0;
margin-top: 50px;
margin-left: 0px;
margin-bottom: 50px;
position: fixed;
}
#sidebarRight {
width: 220px;
height: 550px;
right: 0;
top: 0;
margin-top: 50px;
margin-right: 0px;
margin-bottom: 50px;
position: fixed;
}
ul {
width: 220px;
list-style-type: none;
margin: 0px;
padding: 0;
margin-top: 30px;
}
li {
height: 35px;
width: 220px;
list-style-type: none;
margin: 5px;
}
#logo {
width: 150px;
height: 150px;
background-image: url("../img/logo.png");
-moz-border-radius: 75px;
-webkit-border-radius: 750px;
border-radius: 75px;
margin-left: 35px;
margin-top: 25px;
}
#sidebarLeft ul li a {
display: block;
width: 60px;
width: 220px;
height: 16px;
text-align: center;
margin-top: 9px;
text-decoration: none;
color: #AFEEEE;
}
#timeline {
width: 780px;
height: 550px;
margin-top: 50px;
margin-left: 240px;
top: 0;
}
input[type="text"] {
background-color: #FFFFFF;
border: 1px solid #E2E2E2;
color: #000000;
font-size: 15px;
font-weight: bold;
padding: 5px;
width: 200px;
height: 12px;
margin-bottom: 3px;
margin-top: 3px;
outline: none;
}
::-webkit-input-placeholder {
font-weight: normal;
}
:-moz-input-placeholder {
font-weight: normal;
}
::-moz-input-placeholder {
font-weight: normal;
}
:-ms-input-placeholder {
font-weight: normal;
}
input[type="submit"] {
border-top: 1px solid #96d1f8;
background: #61a6d4;
background: -webkit-gradient(linear, left top, left bottom, from(#316c94), to(#61a6d4));
background: -webkit-linear-gradient(top, #316c94, #61a6d4);
background: -moz-linear-gradient(top, #316c94, #61a6d4);
background: -ms-linear-gradient(top, #316c94, #61a6d4);
background: -o-linear-gradient(top, #316c94, #61a6d4);
padding: 5px 10px;
-webkit-border-radius: 7px;
-moz-border-radius: 7px;
border-radius: 7px;
-webkit-box-shadow: rgba(0,0,0,1) 0 1px 0;
-moz-box-shadow: rgba(0,0,0,1) 0 1px 0;
box-shadow: rgba(0,0,0,1) 0 1px 0;
text-shadow: rgba(0,0,0,.4) 0 1px 0;
color: #ffffff;
font-size: 12px;
font-family: Helvetica, Arial, Sans-Serif;
text-decoration: none;
vertical-align: middle;
}
input[type="submit"]:hover {
border-top-color: #49718c;
background: #49718c;
color: #ccc;
}
input[type="submit"]:active {
border-top-color: #1b435e;
background: #1b435e;
}
and home.php looks like this:
<?php
mysql_connect("localhost","root","") or die("couldn't connect to database.");
mysql_select_db("thesocializer") or die("couldn't select database");
session_start();
if (!isset($_SESSION["user_login"])) {
}
else
{
$username = $_SESSION["user_login"];
}
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/main.css"/>
<title>The Socializer</title>
</head>
<body>
<div id="sidebarLeft">
<div id="logo">
</div>
<ul>
<li>Logg inn</li>
<li>Registrer deg</li>
<li>Om</li>
<li>Kontakt</li>
</ul>
</div>
<div id="timeline">
<?php echo "Hello, ".$username; ?>
</div>
</body>
</html>
* {
background-color: #2C3E50;
font-family: Arial, Helvetica, Sans-serif;
font-size: 16px;
color: #AFEEEE;
}
#sidebarLeft {
width: 220px;
height: 550px;
top: 0;
left: 0;
margin-top: 50px;
margin-left: 0px;
margin-bottom: 50px;
position: fixed;
}
#sidebarRight {
width: 220px;
height: 550px;
right: 0;
top: 0;
margin-top: 50px;
margin-right: 0px;
margin-bottom: 50px;
position: fixed;
}
ul {
width: 220px;
list-style-type: none;
margin: 0px;
padding: 0;
margin-top: 30px;
}
li {
height: 35px;
width: 220px;
list-style-type: none;
margin: 5px;
}
#logo {
width: 150px;
height: 150px;
background-image: url("../img/logo.png");
-moz-border-radius: 75px;
-webkit-border-radius: 750px;
border-radius: 75px;
margin-left: 35px;
margin-top: 25px;
}
#sidebarLeft ul li a {
display: block;
width: 60px;
width: 220px;
height: 16px;
text-align: center;
margin-top: 9px;
text-decoration: none;
color: #AFEEEE;
}
#timeline {
width: 780px;
height: 550px;
margin-top: 50px;
margin-left: 240px;
top: 0;
}
input[type="text"] {
background-color: #FFFFFF;
border: 1px solid #E2E2E2;
color: #000000;
font-size: 15px;
font-weight: bold;
padding: 5px;
width: 200px;
height: 12px;
margin-bottom: 3px;
margin-top: 3px;
outline: none;
}
::-webkit-input-placeholder {
font-weight: normal;
}
:-moz-input-placeholder {
font-weight: normal;
}
::-moz-input-placeholder {
font-weight: normal;
}
:-ms-input-placeholder {
font-weight: normal;
}
input[type="submit"] {
border-top: 1px solid #96d1f8;
background: #61a6d4;
background: -webkit-gradient(linear, left top, left bottom, from(#316c94), to(#61a6d4));
background: -webkit-linear-gradient(top, #316c94, #61a6d4);
background: -moz-linear-gradient(top, #316c94, #61a6d4);
background: -ms-linear-gradient(top, #316c94, #61a6d4);
background: -o-linear-gradient(top, #316c94, #61a6d4);
padding: 5px 10px;
-webkit-border-radius: 7px;
-moz-border-radius: 7px;
border-radius: 7px;
-webkit-box-shadow: rgba(0,0,0,1) 0 1px 0;
-moz-box-shadow: rgba(0,0,0,1) 0 1px 0;
box-shadow: rgba(0,0,0,1) 0 1px 0;
text-shadow: rgba(0,0,0,.4) 0 1px 0;
color: #ffffff;
font-size: 12px;
font-family: Helvetica, Arial, Sans-Serif;
text-decoration: none;
vertical-align: middle;
}
input[type="submit"]:hover {
border-top-color: #49718c;
background: #49718c;
color: #ccc;
}
input[type="submit"]:active {
border-top-color: #1b435e;
background: #1b435e;
}
you have to move the session_start();
in both pages at the begin of your script.
index.php:
<?php
session_start();
mysql_connect("localhost","root","") or die("couldn't connect to database.");
...
home.php:
<?php
session_start();
mysql_connect("localhost","root","") or die("couldn't connect to database.");
...