PHP not executing and not adding information to database - php

I am on a localserver learning mysql and php on my local server MAMP, below i will put the code to all three of my files index.php, signup.php and dbh.php. I am trying to create a log in system but it does not send the information in the database but i do not get an error message either when running it runs fine but does not add the information that i entered in the sign up form.
My database name is drugcity and my username is drugcity_data and my password is admin
So please help been trying for hours!
index.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Drug City</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<form action="signup.php" method="POST">
<input type="text" name="usrname" placeholder="Username Here"><br>
<input type="password" name="pwd" placeholder="Password Here"><br>
<button>Sign Up</button>
</form>
</body>
</html>
signup.php
<?php
include 'dbh.php';
$username = $_POST['usrname'];
$pass = $_POST['pwd'];
$sql = "INSERT INTO users(usrname, pwd) VALUES ('$username','$pass')";
$result = mysqli_query($conn, $sql);
header("Location: index.php");
?>
dbh.php
<?php
$user = 'drugcity_data';
$password = 'admin';
$db = 'drugcity';
$host = 'localhost';
$port = 8889;
$conn = mysqli_connect($link, $host, $user, $password, $db, $port);
?>

I think, you have wrong connection to DB.
You have
$conn = mysqli_connect($link, $host, $user, $password, $db, $port);
Why you have there $link variable? Try it without $link, so
$conn = mysqli_connect($host, $user, $password, $db, $port);

Try removing $link from your $conn = mysqli_connect($link, $host, $user, $password, $db, $port);
The first variable of mysqli_connect should be your host.
Now your host is empty so it doesn't know what to connect to..

Related

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

Error with connection to mysql?

I have this in side my two php files.
<?php
include 'dbh.php';
$first = $_POST['first']
$last = $_POST['last']
$uid = $_POST['uid']
$pwd = $_POST['pwd']
echo $first;
echo $last;
echo $uid;
echo $pwd;
?>
and
<?php
$conn = mysql_connect("localhost", "root", "", "login");
if (!$conn) {
die("Connection faile:".mysql_connect_error());
}
?>
and my html is down there. What could be problem in my connection to mysql? Can anyone help me? I don't have much experience with mysql but i think it should be ok.
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="http://localhost/signup.php" method="POST">
<input type="text" name="first" placeholder="name"><br>
<input type="text" name="last" placeholder="lastname"><br>
<input type="text" name="uid" placeholder="username"><br>
<input type="pasword" name="pwd" placeholder="pasword"><br>
<button type="submit">sign up</button>
</form>
</body>
</html>
First change this database connection, and don't use mysql beacause is deprecated use mysqli instead.
<?php
// host, user, password, database name
$conn = mysqli_connect("localhost", "root", "", "login");
// if u use mysql then ur connection need to connect first then select database
// $conn = mysql_connect("localhost", "root", "");
// $db = mysql_select_db("database name");
if (!$conn) {
die("Connection faile: " . mysqli_connect_error());
}
?>
use mysqi because it is more secured and you only need to adjust mostly on parameters but better if you start using it, mysql is deprecated already so try to avoid using mysql functions and start using mysqli.. connect using this
$db_host = '192.168.1.9'; //sample
$db_user = 'dbuser'; //sample usename
$db_pass = 'dbpass'; //sample password
$db_name = 'databasename';
$db = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if(mysqli_connect_errno($db))
die('Failed to connect to MySQL: ' . mysqli_connect_error());
hope this helps.

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

PHP form not saving information to mySQL database error

Ok, so I have tested if the database is connected and it is. But, my problem is that the form won't go onto the output.php page to be able to save it to the database, I'm getting a NOT FOUND error. I have included a screenshot of the error, the code seems right to me but I can't figure out why it can't find the page when it's clearly in the FOLDER. Thanks for your time in advance.
form.php file
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<?php
$fname = $surname = "";
?>
<h2>Fill out user details</h2>
<form action="output.php" method="post" >
First Name: <input id="fname" type="text" name="fname" >
<br><br>
Surname <input id="surname" type="text" name="surname">
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
output.php file
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<?php $fname=$_POST["fname"];
$surname=$_POST["surname"];?>
<!--Connect to the database-->
<?php
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully"; ?>
<?php
$sql="INSERT INTO 'user'.'user_tbl'('user_fname','user_surname') VALUES ('$fname','$surname')";
mysqli($conn,$sql);
?>
<?php
header("Location: http://localhost/mysite/");
?>
</body>
</html>
There are a few things wrong with your code.
You are using the wrong identifiers for your table and columns, being single quotes.
Use bacticks
$sql="INSERT INTO `user`.`user_tbl`(`user_fname`,`user_surname`) VALUES ('$fname','$surname')";
Then there is this line mysqli($conn,$sql); it should be mysqli_query($conn,$sql);
or
if(!$result = $conn->query($sql)){
die('There was an error running the query [' . $conn->error . ']');
}
Then this
$conn = new mysqli($servername, $username, $password);
there should be a 4th parameter for it being for the database
$conn = new mysqli($servername, $username, $password, $database);
however, I see no variables set for any of these.
Here is an example taken from http://php.net/manual/en/function.mysqli-connect.php
$link = mysqli_connect("myhost","myuser","mypassw","mybd")
or die("Error " . mysqli_error($link));
or using your variables and the one that was missing, and replacing with your own credentials
$servername = "localhost"; // or what your service provider said to use
$username = "username"; // your username
$password = "password"; // if one is needed. Leave blank if no password is needed
$database = "your_database"; // your database name
$conn = new mysqli($servername, $username, $password, $database);
Plus, your present code is open to SQL injection. Use prepared statements, or PDO with prepared statements, they're much safer.
Checking for errors would have signaled the error, if you had used mysqli_query instead of just mysqli
or die(mysqli_error($conn)) to mysqli_query()
However, you are doing what is called "outputting before header" with HTML above your PHP
the header being
header("Location: http://localhost/mysite/");
you need to place your HTML under PHP.
Actually, you don't need the following tags for your SQL/PHP
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
and
</body>
</html>
Just the PHP/SQL.

I have created an admin login with PHP but its not working. Can someone guide me through it or offer appropriate amendments?

This is my login form (admin_login.php) with username and password inputs which should then run the login.php script when I hit submit.
admin_login.php
//the login form
<?php
$dbhost = 'xxxxxx';
$dbuser = 'xxxxxx';
$dbpass = 'xxxxxx';
$con = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $con )
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('xxxxxx');
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/master.css">
</head>
<body>
<form method="post" action="login.php">
User:<input name="username" type="text">
Pass:<input name="password" type="password">
<input name="submit" type="submit" value="Submit">
</form>
</body>
</html>
The following piece of code should take the username and password value from the previous file admin_login.php and allow the user to go the admin_control_panel.php if the username and password is the same as the one stored in my database. Else go back to login.
login.php
//the action script
<?php
$dbhost = 'xxxxxx';
$dbuser = 'xxxxxx';
$dbpass = 'xxxxxx';
$con = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $con )
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('xxxxxx', $con);
$query = "SELECT username FROM users ".
"WHERE username=\"$username\" ".
"AND password = \"$password\"";
$result = mysql_query($query, $con);
if (mysql_num_rows($result) == 0)
header("Location: admin_login.php");
else
header("Location: admin_control_panel.php");
?>
If all is well I should have an admin control panel which I should be able to use like a normal page except only I can access it.
admin_control_panel.php
//where I will be able to access the admin panel, only I should be authorized.
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Control Panel</title>
</head>
<body>
<p>You are in the admin control panel</p>
</body>
</html>
`
Thank you in advance.
You're not getting the $_POST results. Try:
$query = "SELECT username FROM users ".
"WHERE username='".$_POST[username]."' ".
"AND password = '".$_POST[password]."'";
Also, this is just a guideline. You're open to sql injection and you should look into using PDO
You might also need mysql_data_seek($result, 0); after $result = mysql_query($query, $con);
You need to move the internal result pointer to the beginning with mysql_data_seek.
$username = $_POST['username'] and same with password.

Categories