HTML not being loaded in PHP Document - php

I am writing a basic PHP login/Registration script and for some reason as soon as I add a PHP command the HTML isn't loaded. I had a look at the source but there is nothing loaded at all. I am hoping I have missed something very basic, but I am also having a couple of teething issues with the version of PHP installed on the web server.
<?php
require_once('connect.php');
if(isset($_POST) & !empty($_POST)){
$username = mysql_real_escape_string($_POST['username']);
$email = mysql_real_escape_string($_POST['email']);
$password =md5($_POST['password']);
$sql = "INSERT INTO 'login' (username, email, password) VALUES ('$username, $email, $password)";
$result = mysql_query($connection, $sql);
if($result){
echo "User Rego Secusseflllgk";
}else{
echo "User rego faile";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>USer Reg in PHP & MySQL</title>
</head>
<center>
<body>
<div class="container">
<form class="form-signin" method="POST">
<h2 class="form-sigin-heading">Please register</h2>
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">#</span>
<input type="test" name="username" class="form-control" placeholder="Username" required>
</div>
<label for="inputEmail" class="sr-only">Password</label>
<input type="email" name="email" id="inputEmail" class="form-control" placeholder="Email address" required autofocus>
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" required>
<button class="btn btn-lg btn-primary btn-block" type="submit">Register</button>
<a class="btn btn-lg btn-primary btn-block" href="login.php">Login</a>
</form>
</div>
</body>
</html>
I tried to print the POST request but this didnt shed any light on the issue
print_r($_POST);

You are using deprecated functions and your if-statement is incorrect.
Use if(isset($_POST) && !empty($_POST)){
The mysql_query should be mysqli_query, the function you use:
http://php.net/manual/en/function.mysql-query.php
mixed mysql_query ( string $query [, resource $link_identifier = NULL ] )
VS the new one:
http://php.net/manual/en/mysqli.query.php
mixed mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )
You are placing the connection first, and than the Query, which doesn't work for mysql_query. In mysqli_query that mark-up does work.

Check and review below code,
<?php
require_once('connect.php');
if(isset($_POST) & !empty($_POST)){
$username = mysql_real_escape_string($_POST['username']);
$email = mysql_real_escape_string($_POST['email']);
$password =md5($_POST['password']);
$sql = "INSERT INTO login (username, email, password) VALUES ('".$username."', '".$email."', '".$password."')";
$result = mysql_query($sql);
if($result){
echo "User Rego Secusseflllgk";
}else{
echo "User rego faile";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>USer Reg in PHP & MySQL</title>
</head>
<center>
<body>
<div class="container">
<form class="form-signin" method="POST">
<h2 class="form-sigin-heading">Please register</h2>
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">#</span>
<input type="test" name="username" class="form-control" placeholder="Username" required>
</div>
<label for="inputEmail" class="sr-only">Email</label>
<input type="email" name="email" id="inputEmail" class="form-control" placeholder="Email address" required autofocus>
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" required>
<button class="btn btn-lg btn-primary btn-block" type="submit">Register</button>
<a class="btn btn-lg btn-primary btn-block" href="login.php">Login</a>
</form>
</div>
</body>
</html>

use this
if(isset($_POST['email']) && !empty($_POST['email'])){

Related

use PHP function in <input type='submit'> from functions.php to other file

I have two files
functions.php
<?php
include 'config.php';
function signup(){
if (isset($_POST['submit'])) {
$uname = $_POST['uname'];
$email = $_POST['email'];
$password = $_POST['password'];
$cpassword = $_POST['cpassword'];
if($password == $cpassword) {
$hash = md5($password);
$insert = "INSERT INTO `users`(`user_name`, `email`, `password`) VALUES ('$uname','$email','$hash')";
$result = mysqli_query($con, $insert);
if ($result) {
echo '<script>alert("Your account has been successfully created.")</script>';
}
}
else {
echo '<script>alert("Passwords do not match!")</script>';
}
}
}
?>
signup.php
<?php
include 'functions.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- ===== Iconscout CSS ===== -->
<link rel="stylesheet" href="https://unicons.iconscout.com/release/v4.0.0/css/line.css">
<!-- ===== CSS ===== -->
<link rel="stylesheet" href="css/credential.css">
<title>Sing Up</title>
</head>
<body>
<div class="container">
<div class="forms">
<div class="form signup">
<span class="title">Sign Up</span>
<form method="POST" action="functions.php">
<div class="input-field">
<input type="text" name="uname" placeholder="Enter your full name" required>
<i class="uil uil-user"></i>
</div>
<div class="input-field">
<input type="email" name="email" placeholder="Enter your email" required>
<i class="uil uil-envelope icon"></i>
</div>
<div class="input-field">
<input type="password" class="password" name="password" placeholder="Create a password" required>
<i class="uil uil-lock icon"></i>
</div>
<div class="input-field">
<input type="password" class="password" name="cpassword" placeholder="Confirm a password" required>
<i class="uil uil-lock icon"></i>
<i class="uil uil-eye-slash showHidePw"></i>
</div>
<div class="checkbox-text">
<div class="checkbox-content">
<input type="checkbox" id="termCon">
<label for="termCon" class="text">I accepted all Terms and Conditions, Privacy Policy and Cookie Policy</label>
</div>
</div>
<div class="input-field button">
<input type="submit" value="Sign Up" name="submit">
</div>
</form>
<div class="login-signup">
<span class="text">Already have an account?
Login Now
</span>
</div>
</div>
<div class="form login">
<span class="title">Login</span>
<form action="#">
<div class="input-field">
<input type="email" placeholder="Enter your email" required>
<i class="uil uil-envelope icon"></i>
</div>
<div class="input-field">
<input type="password" class="password" placeholder="Enter your password" required>
<i class="uil uil-lock icon"></i>
<i class="uil uil-eye-slash showHidePw"></i>
</div>
<div class="checkbox-text">
<div class="checkbox-content">
<input type="checkbox" id="logCheck">
<label for="logCheck" class="text">Remember me</label>
</div>
Forgot password?
</div>
<div class="input-field button">
<input type="submit" value="Login" name="submit">
</div>
</form>
<div class="login-signup">
<span class="text">Don't have an account?
Signup Now
</span>
</div>
</div>
</div>
</div>
<script src="js/credential.js"></script>
</body>
</html>
I want something like this...
when I click on <input type="submit" of signup the signup() function from functions.php should work. But I don't know how to do it.
If I remove function signup(){} from functions.php and try without function then in url signup.php is replaced by functions.php and page is blank and no data is inserted in mysql localhost.
In 'config.php' file
<?php
$con = mysqli_connect("localhost","root","","get-viewed");
?>
Database name, Table name and field name are perfect I have double checked it.
The form action correctly point to function.php and the webserver execute it.
The result is blank because nothing in function.php get executed.
you defined function signup() but you don't call it
add signup(); as last code line, just before php closing tag ?>
Note 1: you can extract the code from the signup function, since it does not add any advantage.
Note 2: if the php closing tag is the last code line in the file (no html follow) you should omit, it is a good practice to avoid unwanted output.
This is a must once you start to use frameworks, otherwise header errors will popup
Thanks for helping me I have solved my question.
I updated functions.php
<?php
include 'config.php';
function signup() {
$uname = $_POST['uname'];
$email = $_POST['email'];
$password = $_POST['password'];
$cpassword = $_POST['cpassword'];
if($password == $cpassword) {
$hash = password_hash($password, PASSWORD_DEFAULT);
$insert = "INSERT INTO `users`(`user_name`, `email`, `password`) VALUES ('$uname','$email','$hash')";
$result = mysqli_query($con, $insert);
if ($result) {
echo '<script>alert("Your account has been successfully created.")</script>';
}
}
else {
echo '<script>alert("Passwords do not match!");location.replace("signup.php");</script>';
}
}
function login(){
if (isset($_POST['login'])) {
echo '<script>alert("login")</script>';
}
}
if (isset($_POST['signup'])) {
signup();
}
else {
login();
}
Now it is working perfectly as I wanted.

Using PHP Session variables in SQL queries [duplicate]

Im using the following code to insert users into a table called 'accounts'
session_start();
include("include/connect.php");
//Posted information from the form put into variables
$username = mysqli_escape_string($conn, $_POST["username"]);
$email = mysqli_escape_string($conn,$_POST["email"]);
$password = mysqli_escape_string($conn,$_POST["password_1"]);
//check if user already exists
$usernameCheck="SELECT * FROM accounts WHERE username='$username'";
//query the db
$usernameResult = mysqli_query($conn, $usernameCheck) or die(mysqli_error($conn));
//if no users exits then add data
if(mysqli_num_rows($usernameResult) == 0){
$AddUser = "INSERT INTO `accounts` (`username`, `email`, `password`) VALUES ('$username', '$email', '$password')";
$newUserSend = mysqli_query($conn, $AddUser) or die(mysqli_error($conn));
mysqli_close($conn);
$_SESSION["user_session"]= $username;
}
However every time it runs it inserts a row with the correct data and then a blank row below. For example
ID Name email password
1 test test#test.com test
2
This is the form where the data is posted from:
<form method="POST" class="signup-form" action="RegisterProcess.php">
<h2 class="form-title">Create account</h2>
<div class="form-group">
<input type="text" value="<?php echo $username; ?>" class="form-input" name="username" placeholder="Your Name" required/>
</div>
<div class="form-group">
<input type="email" value="<?php echo $email; ?>" class="form-input" name="email" placeholder="Your Email Address" required/>
</div>
<div class="form-group">
<input type="password" class="form-input" name="password_1" placeholder="Your Password" required/>
<span toggle="#password" class="zmdi zmdi-eye field-icon toggle-password"></span>
</div>
<div class="form-group">
<input type="password" class="form-input" name="password_2" placeholder="Confirm your password" required/>
<span toggle="#password" class="zmdi zmdi-eye field-icon toggle-password"></span>
</div>
<div class="form-group">
<input type="submit" class="form-submit" value="Sign up"/>
</div>
</form>

PHP inserts duplicate rows into database

Im using the following code to insert users into a table called 'accounts'
session_start();
include("include/connect.php");
//Posted information from the form put into variables
$username = mysqli_escape_string($conn, $_POST["username"]);
$email = mysqli_escape_string($conn,$_POST["email"]);
$password = mysqli_escape_string($conn,$_POST["password_1"]);
//check if user already exists
$usernameCheck="SELECT * FROM accounts WHERE username='$username'";
//query the db
$usernameResult = mysqli_query($conn, $usernameCheck) or die(mysqli_error($conn));
//if no users exits then add data
if(mysqli_num_rows($usernameResult) == 0){
$AddUser = "INSERT INTO `accounts` (`username`, `email`, `password`) VALUES ('$username', '$email', '$password')";
$newUserSend = mysqli_query($conn, $AddUser) or die(mysqli_error($conn));
mysqli_close($conn);
$_SESSION["user_session"]= $username;
}
However every time it runs it inserts a row with the correct data and then a blank row below. For example
ID Name email password
1 test test#test.com test
2
This is the form where the data is posted from:
<form method="POST" class="signup-form" action="RegisterProcess.php">
<h2 class="form-title">Create account</h2>
<div class="form-group">
<input type="text" value="<?php echo $username; ?>" class="form-input" name="username" placeholder="Your Name" required/>
</div>
<div class="form-group">
<input type="email" value="<?php echo $email; ?>" class="form-input" name="email" placeholder="Your Email Address" required/>
</div>
<div class="form-group">
<input type="password" class="form-input" name="password_1" placeholder="Your Password" required/>
<span toggle="#password" class="zmdi zmdi-eye field-icon toggle-password"></span>
</div>
<div class="form-group">
<input type="password" class="form-input" name="password_2" placeholder="Confirm your password" required/>
<span toggle="#password" class="zmdi zmdi-eye field-icon toggle-password"></span>
</div>
<div class="form-group">
<input type="submit" class="form-submit" value="Sign up"/>
</div>
</form>

WAMP object not found- URL not found on server?

I'm currently trying to complete this project for school and I would really appreciate some help.
I have been trying to learn PHP so I could extract data from my HTML and put it into my MySQL (which I'm accessing through XAMP). I have a problem that says:
The requested URL was not found on this server.
The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.
If you think this is a server error, please contact the webmaster.' when I press the register button. I don't know what this means? I have a table called usertable in the database Login so I feel like it should work.
This is my PHP code:
<?php
session_start();
$con = mysqli_connect('localhost', 'root', 'ocr2020');
mysqli_select_db($con, 'login');
$name = $_POST['user'];
$pass = $_POST['pass'];
$fname= $_POST['forename'];
$sname = $_POST['surname'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];
$dateofB= $_POST['dateB'];
$s= "select * from usertable where name='$name'";
$s2= "select * from usertable";
$result= mysqli_query($con,$s);
$num= mysqli_num_rows($result);
$num2= mysqli_num_rows($s2);
$id= $num2+1;
if($num==1){
echo" Username Is No Longer Available";
}else{
$reg= " insert into usertable(patientID, Forename, Surname, Username, Password, Email, Mobile,
DateOfBirth) values ('$id', '$fname', '$sname', '$name', '$pass', '$email', '$mobile', '$dateOfB')";
mysqli_query($con, $reg);
echo" Registration Successful";
}
?>
As I said, I would really appreciate any help or advice. Thanks!
Edit:
Here is the updated code having taken on everyone's comments:
<?php
ini_set('display_errors', 1);
ini_set('log_errors',1);
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);.
session_start();
$con = mysqli_connect('localhost', 'root', 'ocr2020');
mysqli_select_db($con, 'login');
$name = $_POST['user'];
$pass = $_POST['pass'];
$fname= $_POST['forename'];
$sname = $_POST['surname'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];
$dateofB= $_POST['dateB'];
$s= "select * from usertable where name='$name'";
$result= mysqli_query($con,$s);
$num= mysqli_num_rows($result);
if($num>0){
echo" Username Is No Longer Available";
}else{
$reg= " insert into usertable(patientID, Forename, Surname, Username, Password, Email, Mobile, DateOfBirth) values ('$id', '$fname', '$sname', '$name', '$pass', '$email', '$mobile', '$dateOfB')";
mysqli_query($con, $reg);
echo" Registration Successful";
}
?>
Here is the code from the login.php page which is where I press the 'register button' and it creates the problem:
<html>
<section id="loginBox">
<div class="container">
<div class="login-box">
<div class="row">
<div class="col-md-6 login-left">
<h3> Login Here </h3>
<form action="registration.php" method="POST">
<div class="form-group">
<label> Username </label>
<input type="text" name="user" placeholder="Enter your username" class="form-control" required>
</div>
<div class="form-group">
<label> Password </label>
<input type="password" name="password" placeholder="Enter your password" class="form-control" required>
</div>
<button type="submit" class="btn btn-primary"> Login </button>
</form>
</div>
<div class="col-md-6 login-right">
<h3> Register Here </h3>
<form action="validation.php" method="POST">
<div class="form-group">
<label> Username </label>
<input type="text" name="user" placeholder="Enter your username" class="form-control" required>
</div>
<div class="form-group">
<label> Password </label>
<input type="password" name="password" placeholder="Enter your password" class="form-control" required>
</div>
<div class="form-group">
<label> Forename </label>
<input type="text" name="forename" placeholder="Enter your forename" class="form-control" required>
</div>
<div class="form-group">
<label> Surname </label>
<input type="text" name="surname" placeholder="Enter your surname" class="form-control" required>
</div>
<div class="form-group">
<label> Mobile</label>
<input type="text" name="mobile" placeholder="Enter your mobile" class="form-control" required>
</div>
<div class="form-group">
<label> Email </label>
<input type="text" name="email" placeholder="Enter your email" class="form-control" required>
</div>
<div class="form-group">
<label> Date of Birth</label>
<input type="date" name="dateB" placeholder="Enter your date of birth" class="form-control" required>
</div>
<button type="submit" class="btn btn-primary"> Register </button>
</form>
</div>
</div>
</div>
</div>
</section>
</body>
</html>

Creating Log In page PhP SQL

im really new at this so i appriciate that if you could keep the answers really simple and basic so i can understand.
so i am trying to build a simple login platform but it doesnt work..
i think i have some sord of a problem with the sql code cause $rows tag doesnt count as one row that fit for the user that inculded in the database.
thx in advanced.
<?php
session_start();
?>
include "config.php";
$wrong_dt="";
if(isset($_POST['email']) && $_POST['email']!="" && isset($_POST['password']) && $_POST['password']!=""){
$pw = mysqli_prepare($conn , "SELECT `email` FROM `Users` WHERE `email` = ? AND `password` = ? LIMIT 1");
if($pw){
echo "good";
echo $_POST['email'];
echo $_POST['password'];
mysqli_stmt_bind_param($pw, "ss" , $_POST['email'] , $_POST['Password']);
mysqli_stmt_execute($pw);
$result= mysqli_stmt_get_result($pw);
echo mysqli_num_rows($result);
if(mysqli_num_rows($result) > `0){
$_SESSION['email']=$_POST['email'];
header("Location:Find.php");
}
else{
$wrong_dt="wrong details";
}
}
}
?>
MusicRun
</head>
<header>
<h1> MusicRun </h1>
<h5>Same Hobby.Same Music.</h5>
<p>MusicRun is a platform for people who likes running in a group with other people who likes the same music as they do</p>
</header>
<body background= "http://www.spyderonlines.com/images/nike_sports_athletics_run_running_81188.jpg">
<main>
<form method="post" action="login.php">
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" name="email" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" name="password" id="exampleInputPassword1" placeholder="Password">
<button type="submit" class="btn btn-primary" id="loginBtn">Login</button>
</div>
<p><?php echo $wrong_dt ?></p>
<div class="newAccount">
<p>Not a User yet?</p>
<button type="button" class="btn btn-primary" id="login_button" onclick="window.location.href='http://matanhm.myweb.jce.ac.il/Final%20Project/register.php'">Sign up</button>
</div>
</form>
</main>
<footer>
</footer>
</body>

Categories