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

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.

Related

Cant connect to localhost databse Error: Failed to connect: A connection could not be established because the destination computer actively refused it

Im trying to get my database for my local host up and running using MYSQLI and i get the message
"Failed to connect: A connection could not be established because the destination computer actively refused it"
My code is the following
the connect page called connect.php
<?php
require_once "user_db.php";
$mysqli = mysqli_connect($servername,$username,$password,$database);
if (mysqli_connect_errno()) {
echo "Failed to connect:",mysqli_connect_error();
}
function performQuery($sql) {
global $mysqli;
$result = mysqli_query($mysqli,$sql);
if ($result) {
return $result;
} else {
echo "Something went wrong";
}
}
?>
and in adition to that i have the user_db.php page with my database information
<?php
$servername = "localhost";
$username = "root";
$password = "12345";
$database = "recovery"
?>
and then the page where i try to add stuff to my database
<?php
require_once "DB_handling/connect.php";
if(isset($_POST["submit"])) {
$duration = $_POST['TD'];
$intensity = $_POST['TI'];
$RPE = $TI*$TD;
$New = performQuery("INSERT INTO rpe(duration, intensity, rpe) VALUES ('$duration', '$intensity', '$RPE')");
}
?>
with a simple form field to get the values
<form class="" action="insert.php" method="post">
Training Intensity
<input type="number" placeholder="1-10" name="TI">
<br>
Training Duration
<input type="number" placeholder="in minutes" name="TD">
<br>
<input type="submit" name="submit" value="Submit">
</form>
Im using MAMP as my localhost server
Does know how to fix the problem. Thanks in advance

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>

submit button is not working. i have a mysql database and i am trying to make a php form

when i hit the submit button, nothing happens. perhaps the database is not connected. i am trying to make a form using php and html. i am using xampp, i wrote the code in notepad++ and i saved form.php in htdocs. i don't know what is wrong. maybe the names i used for the variables.
this is the html code:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<form method="post" action="C:\xampp\htdocs\form.php">
Nume de utilizator : <input type="text" name="nume_de_utilizator" placeholder="Enter Your Name" >
Email : <input type="text" name="email" placeholder="Enter Your Email">
Parola: <input type="password" name="parola">
<input type="submit" value="submit" >
</form>
</body>
</html>
this is form.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "autentificare";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$nume_de_utilizator = mysqli_real_escape_string($conn, $_POST['nume_de_utilizator']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$parola = mysqli_real_escape_string($conn, $_POST['parola']);
// Attempt insert query execution
$sql = "INSERT INTO utilizatori (nume_de_utilizator, email, parola) VALUES ('$nume_de_utilizator', '$email', '$parola')";
if(mysqli_query($conn, $sql))
printf("%d Row inserted.\n", mysqli_affected_rows($con));
else
{ echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);}
// Close connection
mysqli_close($conn);
?>
and this is the "autentificare", the database
my database
Try to see the "online link"
eg: "http://localhost:8080/form.php"
Do a simple echo msg - file to check and after replace
action="C:\xampp\htdocs\form.php" with action=http_link

PHP file not working on webhosting service

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?

Adding user to MySQL database in php using phpMyAdmin

I think I am successfully connecting to my database by:
<?php
$user = 'root';
$pass = '9KSroMDjEqNmEYY4';
$db = 'chatservice';
$host = '127.0.0.1';
$conn = new mysqli($host, $user, $pass, $db, 3306) or die("Unable to connect");
if ($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
}
?>
My question is how I would use the registration code to successfully add a user to the database. When entering the form I press register I do not get any error messages stating that the registration didn't succeed. It seems that the php code is not being reached after the initial connection. I am new to php and mySQL so any tips on formatting would be nice too!
<?php
require('connect.php');
if(isset($_POST['user']) && isset($_POST['password'])){
$user = $_POST['user'];
$id = $_POST['IDNUM'];
$password = $_POST['password'];
$query = "INSERT INTO 'users' (user ,IDNUM ,password) VALUES('$user', '$id', '$password')";
$result = mysqli_query($query);
if($result){
$msg = "Registered Sussecfully";
echo $msg;
}
else
$msg = "Error Registering";
echo $msg;
}
?>
<div class="register-form">
<title>Chat Page Start</title>
<form action="" methods="POST">
<p>
<label>Username: </label>
<input id="user" type="text" name="user" placeholder="user" />
</p>
<p>
<label>ID: </label>
<input id="IDNUM" type="text" name="IDNUM" placeholder="ID number" />
</p>
<p>
<label>Password: </label>
<input id="password" type="password" name="password" placeholder="password" />
</p>
<a class="btn" href="login.php">Login</a>
<input class="btn register" type="submit" value="Register" />
</form>
</div>
Another thing is how would I check the status of my database connection and where I should be checking this status?
your database connection is mysqli_connect and you execute the query in mysql_query is not proper.
<?php
require('connect.php');
if(isset($_POST['user']) && isset($_POST['password'])){
$user = $_POST['user'];
$id = $_POST['IDNUM'];
$password = $_POST['password'];
$query = "INSERT INTO 'users' (user ,IDNUM ,password) VALUES('$user', ' $id ', '$password')";
$result = mysqli_query($query,$conn);
if($result){
$msg = "Registered Sussecfully";
}
else
$msg = "Error Registering";
}
?>
You are connecting database using mysqli:
$conn = new mysqli('localhost', $user, $pass, $db, 3306) or die("Unable to connect");
And executing query using mysql:
$query = "INSERT INTO 'users' (user ,IDNUM ,password) VALUES('$user', '$IDNUM', '$password')";
$result = mysql_query($query);

Categories