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
Related
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);
?>
Solution:
I tinkered around with kcdwayne's test (using test.php and register.php) and determined that the issue resided in using the file name "db.php." I renamed it to "datab.php" and it appears to be working wherever it is being used. Interesting. Thank you for your answers!
Original Post:
I have one file - checkuser.php - that is POST'd to from a login form to verify a user's credentials. username and password are given. Through an external file - db.php - I am trying to establish a connection to the MySQL database. The setup:
checkuser.php:
session_start();
error_reporting(E_ALL);
ini_set( 'display_errors','1');
require "db.php";
db.php:
$con = mysqli_connect("server", "user", "pass", "db");
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
... later in checkuser.php:
$username = $_POST['username'];
$password = $_POST['password'];
$sql = mysqli_query($con, "SELECT * FROM userdata WHERE username='$username' AND password='$password' AND activation='true'");
And thus, here lies my problem:
Notice: Undefined variable: con in /path/to/checkuser.php on line 23
checkuser.php and db.php are in the same folder; the MySQL connection can easily be established if the code in db.php is moved into checkuser.php itself.
What am I doing wrong?
checkuser.php:
<?php
session_start();
error_reporting(E_ALL);
ini_set( 'display_errors','1');
require "db.php";
$username = $_POST['username'];
$password = $_POST['password'];
if((!$username) || (!$password)){
echo "<font color='white'>Please enter ALL of the information! <br />";
include 'login.php';
exit();
}
$password = md5($password);
$sql = mysqli_query($con, "SELECT * FROM userdata WHERE username='$username' AND password='$password' AND activation='true'");
$login_check = mysqli_num_rows($sql);
if($login_check > 0) {
while($row = mysqli_fetch_assoc($sql)) {
... set some $_SESSION variables ...
}
}
?>
db.php:
<?php
$con = mysqli_connect("server", "user", "password", "database");
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
If db.php is included/required on checkuser.php, the variable should be there, provided that:
$con is not scoped inside of a block where checkuser.php does not have access to.
The place you're requesting it can receive it (i.e., it's passed as an argument into the function or it's not wrapped in a scope that does not have access to $con.
Try this:
make a file called test.php, and in it, place
$var = 'this is my test var';
then make a file in the same folder called register.php, and put only
require('test.php');
echo $var;
If it works, your problem's either in scope or it just isn't loading the db.php.
I'm getting the error: No database selected
Here is the code:
<?php
$host = "localhost";
$user = "root";
$password = "";
$database_name = "Student";
mysql_connect($host, $user, $password);
mysql_select_db($database_name);
?>
This code is for php-connect.php class and for the form:
<!DOCTYPE html>
<html>
<body>
<?php
//load database connection
include("php-connect.php");
$id = "1";
$name = "Elena";
$city = "Lahore";
//Command to insert into table
$query = "INSERT INTO studentdata (id,name,city) VALUES ('$id','$name','$city')";
//run the query to insert the person.
$result = mysql_query($query) OR die(mysql_error());
//let them know the person has been added.
echo "Data successfully inserted into the database table ... ";
?>
</body>
</html>
This is the code for the form.. I've tried a lot of things to fix this error but it does not work. Is there any problem with my database?
Try this...
<?php
$host = "localhost";
$user = "root";
$password = "";
$database_name = "Student";
$mLink = mysql_connect($host, $user, $password) or die(mysql_error());
mysql_select_db($database_name , $mLink);
another file
<?php
require 'php-connect.php';
//...... etc..
but, read this first:
This extension is deprecated as of PHP 5.5.0, and will be removed in
the future. Instead, the MySQLi or PDO_MySQL extension should be used.
See also MySQL: choosing an API guide and related FAQ for more
information. Alternatives to this function include:
mysqli_select_db()
PDO::__construct() (part of dsn)
http://br.php.net/mysql_select_db
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'];
I am a PHP newbie and have been trying for sometime now to connect to MySQL database using PHP so I can insert data into a table I have created but I am unable to do this.
I suspect the problem is coming from my PHP .ini file,but that's just me.
Would be grateful if anyone can help me configure my PHP .ini file so I can connect to MySQL and insert data into my table. Here is my PHP script in case you are wondering.
Any help will be gratefully appreciated.
<?php
$host ="localhost";
$username = "username";
$password = "password";
$database = "database1";
$table ="users";
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect:'.mysql_error());
}
mysql_select_db("database1",$con);
$mysql = "INSERT INTO $table(name,email,password)
VALUES('$_POST[name]','$_POST[email]','$_POST[password]";
if(mysql_query($mysql)) die(mysql_error());
echo"Data inserted";
mysql_close();
?>
I revised some of your code this should work. You had a bunch of little errors. I suggest you read a couple tutorials on just connecting and the syntax of php.
Here is some really basic examples of connecting to a database:
http://www.w3schools.com/php/php_mysql_connect.asp
Also once you get the hang of it here is a really good tutorial to teach you the OOP way of creating a class for a database:
http://net.tutsplus.com/tutorials/php/real-world-oop-with-php-and-mysql/
As far as I see this is not an ini issue. I hope this helps.
<?php
//Set your variables
$host = "127.0.0.1";
$username = "username";
$password = "password";
$database = "database1";
$table = "users";
//Make your connection to database
$con = mysql_connect($host,$username,$password);
//Check your connection
if (!$con) {
die("Could not connect: " . mysql_error());
}
//Select your database
$db_selected = mysql_select_db($database, $con);
//Check to make sure the database is there
if (!$db_selected) {
die ('Can\'t use the db : ' . mysql_error());
}
//Run query
$result = mysql_query("INSERT INTO $table(name,email,password) VALUES('$_POST[name]','$_POST[email]','$_POST[password]'");
//Check Query
if (!$result) {
die("lid query: " . mysql_error());
}
echo "Data inserted";
mysql_close($con);
?>
First, why do you have <br/> in your PHP statements? Remove all those.
Also, you have to use PDO or mysqli_ instead of the mysql_ library, mysql_ is deprecated.