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.
Related
I made a HTML form to link it to mysql workbench. The form or the php connection codes are not showing any errors but the changes arent reflected in the database. I have deleted some of the code which isn't important since the site doesn't allow me.
HTML FORM CODE
<form action="qrconnect.php" method="post">
<div>
<label for="name">Name:</label>
<input type="text" id="name" name="Name">
</div>
.
.
.
<div class="button">
<button name="submit" type="submit">Submit</button>
</div>
</form>
PHP code
<?php
include_once("connect.php");
include_once("qr.html");
/*Data from the html form is on the right. The objects that will be composed of that data is on the left.*/
if(isset($_POST['submit'])) {
$Name = $_POST['Name'];
$EMail = $_POST['E-mail'];
$Phone= $_POST['Phone'];
$Designation =$_POST['Designation'];
$Dept= $_POST['Dept'];
if (mysqli_connect_error())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
mysqli_query($mysqli, "INSERT INTO schema1.emp_table(Name,E-mail,Phone,Designation,Dept)
VALUES('$Name','$EMail','$Phone','$Designation','$Dept')");
echo"<font color='green'>Data added successfully";
}
?>
PHP Connect code
<?php
$host="localhost";
$port=3306;
$socket="";
$user="root";
$password="";
$dbname="schema1";
$mysqli = mysqli_connect($host, $user, $password, $dbname, $port,
$socket)
or die ('Could not connect to the database server' . mysqli_connect_error());
//$con->close();
?>
I try to do input number form and make search in page grapht.php. For example I write code:
<form action="grapht.php" method="post">
Name: <input type="text" name="number" autocomplete="off"><br>
<input type="submit">
</td>
</form>
<? $number= $_POST["number"]; ?>
And then make query my MySQL table:
$shipmin = 1;
$shipmax=$number;
$uzklausimas ="SELECT * FROM MyGuests WHERE id >= '$shipmin' AND id<= '$shipmax'";
$minmax=mysqli_query($conn,$uzklausimas);
while($ru=mysqli_fetch_assoc($minmax)){
echo "$ru[id] <br>";
}
But my code doesn't work. Maybe somebody could give me advice how need solve this my problem?
Do you initialize your db connection ($conn) before this line ?
$minmax=mysqli_query($conn,$uzklausimas);
if I write line $shipmax=45; it is working well, but I have static date from 1 to 45. If I write $shipmax=$number; It is not work.
all my code
<?php
$servername = "localhost";
$username = "xxxxx";
$password = "xxxx";
$dbname = "xxxxx";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$shipmin = 1;
$shipmax=$number;
$uzklausimas ="SELECT * FROM MyGuests WHERE id >= '$shipmin' AND id<='$number'";
$minmax=mysqli_query($conn,$uzklausimas);
while($ru=mysqli_fetch_assoc($minmax)){
echo "$ru[id] <br>";
}
mysqli_close($conn);
?>
<form action="forum.php" method="post">
Name: <input type="text" name="name" autocomplete="off"><br>
<input type="submit">
</td></form>
<? $number= (int)$_POST["name"]; ?>
<?php echo $number; ?>
<br>
Page where you could see it is http://ortex.lt/forum.php
I´m trying to create a form connected to a database but when I fill out the form and I refer to the table in phpMyAdmin I see that it have entered a blank record instead of form data. I´m using PhpStorm.
I think all this code is correct...
That is the form of the .html:
<form id="form1" name="form1" method="post" action="index.php">
<label for="userSignUp">Email</label>
<input type="text" name="userSign" id="userSignUp" />
<label for="passwordSignUp">Password</label>
<input type="password" name="passwordSign" id="passwordSignUp" />
<input type="submit" name="Submit" id="Submit" value="Submit" />
</form>
I have the following .php:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$db_selected = mysqli_select_db($conn, $dbname);
$userSignUp = ""; // If I substitute "" with characters at this time the table is well updated
$passwordSignUp = ""; // Same as before
if(isset($_POST['userSign'])){
$userSignUp = $_POST['userSign'];
}
if (isset($_POST['passwordSign'])) {
$passwordSignUp = $_POST['passwordSign'];
}
$sql = "INSERT INTO test.person (FirstName, Password) VALUES ('$userSignUp', '$passwordSignUp')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
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.
Hi guys I'm trying to write some code for a login, I've got it all but whenever I try login it says Could not select database. I can't figure it out, I've got hosting with One.com and when I go into the PHP & MySQL settings it says the database name is "c343_co_uk", which is what I've used in the code below:
UPDATE: It's detecting the database but whenever I try and log in with the exact same username and password that's on MySQL it says invalid login
Here is my Connection.php
<?php
$username = "c343_co_uk";
$password = "abc";
$hostname = "c343.co.uk.mysql";
//connection to the database
$connection = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
$selected = mysql_select_db("c343_co_uk",$dbhandle)
or die("Could not select Database");
?>
Loginform.php
<!DOCTYPE html>
<html>
Home
</font>
<head>
<style>
body {
font-size: 14px;
}
</style>
<link rel="stylesheet" type="text/css" href="website.css" />
</head>
<body>
<div id="loginform" style="font-family: 'ClearSans-Thin'; color: Black">
Please enter your login details<br /><br />
Username:<br />
<form method="post" action="loginsubmit.php">
<input type="text" name="username" />
<br />
Password:<br />
<input type="password" name="password" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</center>
</div>
</body>
</html>
Loginsubmit.php
<?php
session_start();
?>
<font face="ClearSans-Thin">
<font color="lightgray">
<?php
include 'connection.php';
include 'loginform.php';
?>
<center>
<?php
if (isset($_POST['submit']))
{
$user = $_POST['username'];
$pass = $_POST['password'];
//Counts up how many matches there are in the database
$query = "SELECT COUNT(*) AS cnt FROM users WHERE Username='" . mysqli_real_escape_string($connection, $user) . "' && Password='" . mysqli_real_escape_string($connection, $pass). "'";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_assoc($result);
$queryadmin = "SELECT COUNT(*) AS cnt FROM admin WHERE Username='" . mysqli_real_escape_string($connection, $user) . "' && Password='" . mysqli_real_escape_string($connection, $pass). "'";
$resultadmin = mysqli_query($connection, $queryadmin);
$rowadmin = mysqli_fetch_assoc($resultadmin);
//If count is more than 0, log user in.
if ($row["cnt"] > 0)
{
$_SESSION["userlogged"] = $user;
echo "Logged in - Press the home button to return to the homepage";
}
//count for user table is 0, if there are more than 0 matches in the admin database, start admin session
else if ($rowadmin["cnt"] > 0 )
{
$_SESSION["adminlogged"] = $user;
echo "Logged in - Press the home button to return to the homepage";
}
else
{
echo 'Not a valid login';
}
}
?>
</center>
try these:
$dbname = "c343_co_uk"
$username = "c343_co_uk";
$password = "abc";
$hostname = "c343.co.uk.mysql";
and replace
$selected = mysql_select_db("c343_co_uk",$connection)
or die("Could not select Database");
to this:
$selected = mysql_select_db($dbname,$connect)
or die("Could not select Database");
i hope that work! have a nice day!
try to replace
$selected = mysql_select_db("c343_co_uk",$dbhandle)
or die("Could not select Database");
with
$selected = mysql_select_db("c343_co_uk",$connection)
or die("Could not select Database");
you can try by this way
<?php
define("DB_HOST", "c343.co.uk.mysql");
define("DB_USER", "c343_co_uk");
define("DB_PASSWORD", "abc");
define("DB_DATABASE", "c343_co_uk");
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD,DB_DATABASE );
?>
Use this for connection :-
$username = "c343_co_uk";
$password = "abc";
$hostname = "c343.co.uk.mysql";
$connection = mysql_connect($hostname, $username, $password);
#mysql_select_db("c343_co_uk",$connection);
you haven't do the host with mysql, just do c343.co.uk:
<?php
$username = "c343_co_uk";
$password = "abc";
$hostname = "c343.co.uk";
//connection to the database
$connection = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
$selected = mysql_select_db("c343_co_uk",$dbhandle)
or die("Could not select Database");
?>
I hope this work! other i'll take a look again