PHP file not working on webhosting service - php

This is my code (sorry for ill formatted code. I'm newbie to web development)
<html>
<head>
<?php
if (isset($_POST['login']))
{
$email = $_POST['email'];
$pass = $_POST['pass'];
$server='localhost';
$dbuser = 'database username';
$dbpass = 'database password';
$dbname = 'database name';
// Create connection
$conn = new mysqli($server, $dbuser, $dbpass, $dbname)
$sql = "INSERT INTO table (email, password)
VALUES ('$email','$pass')";
if ($conn->query($sql) === TRUE)
{
header('Location: address');
}
$conn->close();
}
?>
</head>
<body>
<form method="post" action="index.php">
email:<br>
<input type="text" name="email"><br>
password:<br>
<input type="password" name="pass"><br>
<input type = "submit" name="login" value="login">
</form>
</body>
</html>
I have saved this file as index.php in public_html and when I open my hosted website URL, it shows a blank page. Why?

Related

How to check if username is already taken, PostgreSQL and php

I need to сheck if username is already taken or not. And if it is ok, to redirect to another page but if not (here I am stuck) to make the "username is already taken" appear under the input line.
there is my php code:
<?php
$host = "localhost";
$dbusername = "postgres";
$dbpassword = "admroot";
$db = "local_db_server_test";
$con = pg_connect("host=$host dbname=$db user=$dbusername password=$dbpassword") or die ("Could not connect to Server\n");
if(!$con){ die('Error: Unable to open database'); }
else {
$username = $_POST['username'];
$password = $_POST['password'];
if(strlen($password) < 6) {
pg_close($con); // also can use die() but without header and redirection
header("Location:sign_up_pass_err.html");
}
$query = "INSERT INTO register(username, password) VALUES ('$username',crypt('$password',gen_salt('md5')))";
$result = pg_query($con, $query);
header("Location: login.html");
}
pg_close($con);
?>
And this is my html code:
<!DOCTYPE html>
<html>
<head></head>
<body>
<form action="sign_up.php" method="post">
<input type="text" name="username" placeholder="Username" required><br><br>
<input type="password" name="password" placeholder="Password" required><br><br>
<input type="submit" value="Sign up">
</form>
</body>
</html>

Registration Form Works but shows error

I made a registration form. it actually works but it shows the "Failed To Register, Try Again" message.
Here's my code.
HTML and PHP
<!DOCTYPE html>
<html>
<head>
<title>Register</title>
</head>
<body>
<form action="#" method="POST">
<h3>Register</h3>
Username: <input type="text" name="uname"><br>
Password: <input type="password" name="pass"><br>
<input type="submit" name="btn">
</form>
</body>
</html>
<?php
if (isset($_POST['btn'])) {
$username = $_POST['uname'];
$password = $_POST['pass'];
mysql_connect("localhost","root","");
mysql_select_db("login2");
$query = "INSERT INTO users VALUES ('','$username','$password')";
mysql_query($query);
echo "Registered Successfully";
}
else {
echo "Failed To Register, Try Again";
}
?>
You can check the post.
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// …
}
Also add uname and pass control.
<?php
if (isset($_POST['uname']) && !empty($_POST['uname']) && isset($_POST['pass']) && !empty($_POST['pass'])) {
$username = $_POST['uname'];
$password = $_POST['pass'];
............................
}
else {
echo "Failed To Register, Try Again";
}
?>
1.There is a problem with you if statement which shows failed to register on page load
Mysql is deprecated, use mysqli_*.
Here is an updated code
<!DOCTYPE html>
<html>
<head>
<title>Register</title>
</head>
<body>
<form action="#" method="POST">
<h3>Register</h3>
Username: <input type="text" name="uname"><br>
Password: <input type="password" name="pass"><br>
<input type="submit" name="btn" value="Submit">
</form>
</body>
</html>
<?php
if (isset($_POST['btn'])) {
$username = $_POST['uname'];
$password = $_POST['pass'];
mysqli_connect("localhost","root","");
mysqli_select_db("login2");
$query = "INSERT INTO users VALUES ('','$username','$password')";
$results=mysqli_query($query);
if(mysqli_affected_rows($con)>0){ //check if inserted
echo "Registered Successfully";
}
else{
echo "Failed To Register, Try Again";
}
}
?>
Try the below code and check
<?php
if (isset($_POST['btn'])) {
$username = $_POST['uname'];
$password = $_POST['pass'];
mysql_connect("localhost","root","");
mysql_select_db("login2");
$query = "INSERT INTO users VALUES ('','$username','$password')";
if(mysql_query($query))
{
echo "Registered Successfully";
}
else
{
echo "Failed To Register, Try Again";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Register</title>
</head>
<body>
<form action="#" method="POST">
<h3>Register</h3>
Username: <input type="text" name="uname"><br>
Password: <input type="password" name="pass"><br>
<input type="submit" name="btn">
</form>
</body>
</html>
<?php
if (isset($_POST['btn']))
{
$username = $_POST['uname'];
$password = $_POST['pass'];
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO users(`username`, `password `) VALUES ('$username','$password ')";
$result = $conn->query($sql);
if (!$result)
{
echo("Error description: " . mysqli_error($conn));
}
else
{
echo "successfully inserted";
}
}
?>

Using php file in notepad++ to connect to XAMPP SQL

Hello I am currently doing a school project where I have to produce an application on android. Recently I began making the login/register page ,however I am new to PHP so i am unable to connect to my php file due to current errors. I was wondering if it was due to the database username being incorrect as when I installed XAMPP I was required to change the port numbers so it could work. The following code that i'm having difficulty with is:
<?php
$servername = "localhost:8080";
$username = "root";
$password = "";
$dbname = "db_client";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
Due to this I am unable to connect(connection) to my login page:
<?PHP
include_once("connection.php");
if( isset($_POST['txtUsername']) && isset($_POST['txtPassword']) ) {
$username = $_POST['txtUsername'];
$password = $_POST['txtPassword'];
$query = "SELECT username, password FROM tbl_client ".
" WHERE username = '$username' AND password = '$password'";
$result = mysqli_query($conn, $query);
if($result->num_rows > 0){
echo "success";
}
else{
echo "Login Failed <br/>";
}
}
?>
<html>
<head><title>Login|Matt</title></head>
<body>
<h1>Login Example</h1>
<form action="<?PHP $_PHP_SELF ?>" method="post">
Username <input type="text" name="txtUsername" value="" /><br/>
Password <input type="password" name="txtPassword" value="" /><br/>
<input type="submit" name="btnSubmit" value="Login"/>
</form>
</body>
</html
Any ideas on the issue will be very much appreciated =)
remove the port 8080 from your server name. it is not the default port for mysql. 3306 is the default port.

Trying to add items to a database using php

I am trying to a items to my database (sql) using php and a form, however the data is not being added and nothing seems to happening i just stay on the create.php page.
php code
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "PolyTest";
// Create connection
$conn = mysql_connect($servername, $username, $password);
mysql_select_db($dbname)
$doorName = $_POST['doorName'];
$doorDes = $_POST['doorDes'];
$doorPrice = $_POST('doorPrice');
$doorColour = $_POST('doorColour');
$doorImage = $_POST['doorImage'];
if(!$_POST['submit']){
echo "please fill in the boxs";
header('Location: dooradd.php');
} else {
mysql_query("INSERT INTO Doors ('ID', 'name', 'description', 'price', 'colour', 'image') VALUES(NULL, '$doorName', '$doorDes', '$doorPrice', '$doorColour', '$doorImage')") or die(mysql_error());
echo "Door been added!";
header('Location: doorlist.php');
}
?>
HTML FORM
<form class="add" action="doorCreate.php" method="post">
<input type="text" name="doorName" value="doorName">
<input type="text" name="doorDes" value="doorDes">
<input type="text" name="doorPrice" value="doorPrice">
<input type="text" name="doorColour" value="doorColour">
<input type="text" name="doorImage" value="doorImage">
<input type="submit" name="submit">
</form>
change mysql_select_db($dbname) with mysql_select_db($dbname);
and change;
$doorPrice = $_POST('doorPrice');
$doorColour = $_POST('doorColour');
with
$doorPrice = $_POST['doorPrice'];
$doorColour = $_POST['doorColour'];

Register using PHP isn't working

Trying to simply register, although file is named as Login.html/.php
My HTML form in Login.html :
<!DOCTYPE html>
<html>
<head>
<title>Register/ Login</title>
<link rel="stylesheet" type="text/css" href="Assets/bootstrap.css">
<script src="Assets/jquery.min.js"></script>
<script src="Assets/bootstrap.min.js"></script>
</head>
<body>
<form action="Login.php" method="post">
<fieldset>
<legend>Register:</legend>
Username : <input type="text" name="Username"> <br>
Password : <input type="password" name="Password"> <br>
<input type="submit"><br>
</fieldset>
</form>
</body>
</html>
Have set up the MySQL side of the code and run it as well, it works fine.
The PHP file, Login.php :
<?php
$userErr = $passErr = "";
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testdb";
$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed");
$Username = $_POST['Username'];
$Password = $_POST['Password'];
echo("Reached 1. <br>");
$stmt = "INSERT INTO Login (Username, Password) VALUES ('$Username', '$Password')";
$result = mysqli_query($conn, $stmt);
if($result) {
echo("Registered Successfully!");
}
mysqli_close($conn);
?>
When I enter the details and submit, the code for Login.php is displayed, with the url being: (file:///C:/xampp/htdocs/Login.php)
Also, Login.html was run on localhost, i.e., url being:
(http://localhost/Login.html)
I can guess you are browsing your Login.html with
file:///C:/xampp/htdocs/Login.html
But you have to use like this
http://localhost/Login.html

Categories