Hello i am having trouble connecting to sqlserver 2008 i am trying to create a simple log in system and this is what i have:
<?php
session_start();
if(!isset($_SESSION["user_id"])){
header("location:../../login.html");
}
?>
<?php
$username = $_POST[txt_usernmae];
$user_id = $_POST[txt_password];
mysql_connect($server, $username, $password) or die("No Server Found");
mysql_select_db($schema) or die("No Connection");
?>
I think you mispelled username
$username = $_POST[txt_usernmae];
Instead use
$username = $_POST['txt_usernmae'];
Remember to use '' when you handle POST or GET
Change this
$username = $_POST[txt_usernmae];
$user_id = $_POST[txt_password];
to
$username = $_POST['txt_usernmae'];
$user_id = $_POST['txt_password'];
Related
Hi so i am trying to make a login system using mysql and php but i constantly get the error
Warning: mysql_connect(): Access denied for user 'testuser'#'localhost' (using password: YES) in E:\PHP_Runner\php\root\process.php on line 10
The user is created as seen in the Any suggestions/help?
<?php
$username = $_POST['user'];
$password = $_POST['pass'];
$username = stripcslashes($username);
$password = stripcslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
mysql_connect("localhost",$username, $password) or die("Could not connect to database");
mysql_select_db("users");
$result = mysql_query("select * from users where username = '$username' and password = '$password'")
or die("Failed to query database" .mysql_error());
$row = mysql_fetch_array($result);
if($row['username'] == $username && $row['password'] == $password){
echo "Login success !! welcome".$row['username'];
}else{
echo "Failed to login";
}
?>
To connect to mysql db phpmyadmin or any interface you will need to have mysql login credentials which is not a user login credentials this is usually set at the installation of mysql or you will be using the default. Now this set credentials will replace mysql_connect("localhost", $TheSetUserNameForMysqlDb, $TheSetPasswordForMysqlDb) I hope this help
Which I don't recommend using the default
mysql connect should be something like
mysql_connect("localhost","root", ""); // if you use phpmyadmin in localhost. here root is the defualt admin username of mysql and default password is empty.
I'm trying to make a web/login page, where if a user is in certain location then they can be able to insert data in the database based on their location.
For example, if we have two users bob and max, one in A(bob) and the other in B(max).
If bob logs into the system username... bob password... 123 location.. A, he should be able to to insert his data on the database located on his PC same as max but they should use a single application.
Now my question is how can I achieve this. For example I've a login script below which is communicating with my localhost (A) and i also want to include a remote connection to my other PC on the LAN (B), using Mysql database installed on both computers, I can connect to Mysql database using Mysql workbench but how can I do it using a script in PHP?
login page
<?php
if(isset($_POST['login'])){
include 'includes/config.php';
$uname = $_POST['uname'];
$pass = $_POST['pass'];
$query = "SELECT * FROM admin WHERE uname = '$uname' AND pass = '$pass'";
$rs = $conn->query($query);
$num = $rs->num_rows;
$rows = $rs->fetch_assoc();
if($num > 0){
session_start();
$_SESSION['uname'] = $rows['uname'];
$_SESSION['pass'] = $rows['pass'];
echo "<script type = \"text/javascript\">
alert(\"Login Successful.................\");
window.location = (\"admin/index.php\")
</script>";
} else{
echo "<script type = \"text/javascript\">
alert(\"Login Failed. Try Again................\");
window.location = (\"login.php\")
</script>";
}
}
?>
connection.php
<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "cars";
$conn = new mysqli($host, $user, $pass, $db);
if($conn->connect_error){
echo "Failed:" . $conn->connect_error;
}
?>
Configuring two connection scripts is what i have in mind, but i don't
think it will work.
Any suggestions would be appreciated.
Thanks
Im trying to create a login for my website and i need to store emails, usernames, passwords, ect in a database i have created already using phpMyAdmin. I have gone through article after article and nothing seems to be working. i have my connect.php like this:
<?
$hostname = "localhost";
$username = "username";
$password = "password";
$databaseName = "_mySiteUserDataBase";
mysql_connect($hostname, $username, $password) or die("Cannot connect to server");
mysql_select_db($databaseName) or die("Cannot select database");
?>
And my main.php like this:
<?
include("connect.php");
$tableName = "myUsers";
$sql = "SELECT * FROM $tableName";
$result = mysql_query($sql);
?>
And i have created a simple form in my html like this:
<html>
<head></head>
<body>
<form>
<input type = "submit" action = "main.php" method = "post" value = "Login">
</form>
</body>
</html>
After submitting the form it says cannot connect to server. I am new to php and mysql and i dont understand what each parameter in the mysql_connect is, and i dont know what they do therefore im not sure what im supposed to enter in but everyone i keep reading about seems to be inputing random values? I could use a brief explanation on that, because i am stuck at connecting and cant even get past this point sadly enough. Also i have been reading that mysql_connect is deprecated and isnt valid anymore but i dont understand what im supposed to use as an alternative. I know its mysqli but thats it and im unclear of the syntax.
mysqli:
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
echo "start<br/>";
try {
$mysqli= new mysqli('localhost', 'myusername', 'mypassword', 'dbname');
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
echo "I am connected and feel happy.<br/>";
$mysqli->close();
} catch (mysqli_sql_exception $e) {
throw $e;
}
?>
If you need to know how to create users, what the heck the hostname is, how to grant access (often useful after the connect :>), just ask.
Try this code in 'connect.php'
<?php
error_reporting(0);
$con=mysql_connect('localhost','root','');// here 'root' is your username and "" is password
if(!$con)
{
echo 'not connect';die;
}
mysql_select_db('dbname',$con);// here 'dbname' is your database name
?>
And also try following code to include sql connection in your other php file(main.php)
<?php
include 'connect.php';
$sql = "SELECT * FROM myUsers";
$result=mysql_query($sql);
?>
Let me convert it to mysqli for you and maybe that will fix the problem. Also, make sure the username, password, and database name are correct.
Try this code. At very least, it will provide a better error message for debugging.
<?
$hostname = "localhost";
$username = "username";
$password = "password";
$databaseName = "_mySiteUserDataBase";
$con = mysqli_connect($hostname, $username, $password, $databaseName) or die(mysqli_error($con));
?>
Main.php
<?
include("connect.php");
$tableName = "myUsers";
$sql = "SELECT * FROM $tableName";
$result = mysqli_query($con,$sql);
?>
I have created a site that is working fine on localhost, but when i uploaded it on server its giving me the following error message.
Access denied for user 'azher94_ivsapps'#'localhost' (using password: YES)
There are my setting for database connection
$database= "db_name";
$server = "";
$username= "db_username";
$password= "db_password";
if(mysql_connect($server, $username, $password) && mysql_select_db($database))
//if( mysqli_connect($server, $username, $password , $database) )
{
echo "connected";
}
else
{
echo mysql_error();
}
Kindly guide me what i am doing wrong here.
Thanks
For password you are using $password and after that you are using $Password with capital P. Please change it to $password.
First of all you need to define the localhost and one more thing you are doing wrong is you are not calling the variable properly. Use the code below
$database= "db_name";
$server = "localhost";
$username= "db_username";
$password= "db_password";
if(mysql_connect($server, $username, $password,$database) )
//if( mysqli_connect($server, $username, $password , $database) )
{
echo "connected";
}
else
{
echo mysql_error();
}
Hope this helps you
Try with this code it will work
$con=mysqli_connect("localhost","root","your password");
mysqli_select_db("your_db",$con);
I'm trying to get my config.php file to work but everytime i use it with my login.php it just gives me a white page rather than continuing through my login.php file towards my members.php page. I put my connection info into my login.php script and it works properly listed below is what i been trying to do.
config.php
<?php
$con = mysql_connect("mysql","DBUSER","DBPASS");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("DBNAME", $con);
/* WHAT I ORIGINALLY WANTED TO USE
$localhost = "mysql";
$dbuser = "DBUSER";
$dbpass = "DBPASS";
$dbname = "DBNAME";
$connect = mysql_connect($localhost, $dbuser, $dbpass);
mysql_select_db("$dbname", $connect);
*/
?>
login.php
<?php
// I ALSO USED includes"config.php";
require("config.php");
$username = $_POST['username'];
$password = $_POST['password'];
$query = mysql_query("SELECT * FROM member WHERE username = '$username' AND password = '$password'");
$data = mysql_fetch_assoc($query);
if(mysql_num_rows($query)){
session_start();
$_SESSION['username'] = $data['username'];
header("Location: members.php");
exit;
}
header("Location: index.php");
?>
I'm new to PHP so don't laugh at my code please thanks for the help!
On top of your code turn on errors:
ini_set("display_errors","On");
and make sure you can see your mysql errors:
$query = mysql_query(...) or die("Error: ".mysql_error());
And one last thing: although mysql_* functions are being deprecated, if you use them always escape your data before you use it in your query; you can be victim of SQL injection.
try to use:
include "config.php";
it should be include not includes