Jump to content
Html

Mysql db data doesn't register

Recommended Posts

I did view a different tutorial, and was able to get a copy of the actual php files. It doesn't work on the paid host.

The tutorial user did the php work on the localhost. I did fill in the details to connect to a db. Nothing displays. This is from the config.php

<?php 
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$dbh = new PDO('mysql:host=localhost;dbname=securelogin', 'root', '');
 ?>

So localhost will be fine on the paid host, then root and what ever after should be, a username and a password right?

Share this post


Link to post
Share on other sites
1 hour ago, Html said:

So localhost will be fine on the paid host

Not! necessarily, they will most likely have a specific host name to your account, also specific possible database name , and username , these settings should be provided on creation of the domain and database on that domain.

Share this post


Link to post
Share on other sites

Your username on a shared host will also not be root, not unless you have a dedicated server.

Share this post


Link to post
Share on other sites

Well these settings are on the current php files.

<?php
/* Database connection settings */
$host = 'localhost';
$user = 'username';
$pass = 'pass';
$db = 'db';
$mysqli = new mysqli($host,$user,$pass,$db) or die($mysqli->error);

So what do I do to get the other files to display the login page.

Share this post


Link to post
Share on other sites

That's kind of a vague question, but just look at the code you posted with PDO.  When you create the PDO object you pass a connection string that contains both the host name and the database name, then you pass it your database username, then you pass your database password.  So you can build that connection string for PDO with your host name and database name, and pass the username and password in the other 2 parameters to the new PDO object.

You need to get all of that information from your host.  Maybe it's your responsibility to create your own databases, users, and passwords.  Your host probably has some sort of documentation or instructions for doing that.  That's the information PHP needs to connect to the database.

Share this post


Link to post
Share on other sites

Sure, all that is there, the db has been there for months.

I viewed the book for any info on database connection, but what I read was similar in the code there i've posted.

I did try

$dbh = new PDO('mysql:host=localhost;dbname=securelogin', 'root', '');

  username, password, and database after the root.

$dbh = new PDO('mysql:host=localhost;dbname= 'root', user', 'pass', 'db' ');

So the table is users, it is all done, just how to get this all linked to get that login system displaying.

Share this post


Link to post
Share on other sites

The DSN does not contain the username and password, only the host and database name, and some other possible options like port, character set, etc:

http://php.net/manual/en/pdo.construct.php

The username and password are passed in the second and third parameters.  The first is the DSN.

Again, you need to get the database connection details from your host.  Your username on your host is not "root", and your database probably has your account name prefixed.

Share this post


Link to post
Share on other sites

Then it must be localhost, that is what I have used for the other file for the past five months.

Share this post


Link to post
Share on other sites

The host name is often localhost.  So, that's one out of four that you've got if you can confirm that works on your host.

Share this post


Link to post
Share on other sites

Right, so these PDO code is the new code for connecting to a db.

Which example should I use, 1 or 2?  1 requires an ip address, from the looks of it.

And then 2 states the following;

Quote

The following example assumes that the file /usr/local/dbconnect exists with file permissions that enable PHP to read the file. The file contains the PDO DSN to connect to a DB2 database through the PDO_ODBC driver:

 

Share this post


Link to post
Share on other sites

Which example should I use, 1 or 2?

Those are just showing different methods.  If your DSN is in a file, then you use the scheme in the second example.  Most often the first one is used.  The third one would probably be used in larger organizations.

1 requires an ip address, from the looks of it.

You can use any valid host name or address.  You only need to tell it where the database server is.

And then 2 states the following;

Right.  The manual contains several examples to use the various functions and other things.  They are only examples.  They will probably not work on your system without modification.  They are just examples to show how someone might use it in a certain situation.  It's still up to you to figure out what your situation is and how to use it in your environment.

The first example on that manual pages matches your situation the closest.  Compare it with the code you posted in the first post on this page.

Share this post


Link to post
Share on other sites

Sorry, there is an ip address right next to the listed db, in the mysql section. I forgot about that.

 

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

<?php
/* Connect to a MySQL database using driver invocation */
$dsn = 'mysql:dbname=db;host=IP';
$user = 'usr';
$password = 'pass';
 
try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

?>

I tried the following, nothing will display.

Edited by Html
Added code example #1

Share this post


Link to post
Share on other sites

That code has a syntax error, you have multiple PHP start tags.  You're also not displaying anything if the connection worked.

Share this post


Link to post
Share on other sites

You mean the config.php file from the files must have something more, what is in the code I posted is what it is from the tutorial, the one just above there is the code  took from the php manual, nothing was working as you pointed out I needed to use one of the examples.

Well, yes I notice I used another php tag.

 

Okay now the login.php does display, but there is no login function. From index.php it goes to login.php

 

<?php 
include_once("header.php");

 ?>

<section class="parent">
	<div class="child">
		
		<?php 

			if (!func::checkLoginState($dbh))
			{
				header("location:login.php");
				exit();
			}

			echo 'Welcome ' . $_SESSION['username'] . '!';
			
		 ?>
		 
	</div>

</section>

<?php 
include_once("footer.php");
 ?>

index.php

 

And login.php

<?php include_once("header.php"); ?>

<section class="parent">
	
	<div class="child">
		
		<?php 

			if (func::checkLoginState($dbh))
			{
				header("location:index.php");
			}

			if (isset($_POST['username']) && isset($_POST['password']))
			{
				$query = "SELECT * FROM users WHERE user_username = :username AND user_password = :password";

				$username = $_POST['username'];
				$password = $_POST['password'];

				$stmt = $dbh->prepare($query);
				$stmt->execute(array(':username' => $username,
									 ':password' => $password));

				$row = $stmt->fetch(PDO::FETCH_ASSOC);

				if ($row['user_id'] > 0)
				{
					func::createRecord($dbh, $row['user_username'], $row['user_id']);
					header("location:index.php");
				}
			}
			else
			{
				echo '
					<form action="login.php" method="post">
						<label>Username</label><br />
						<input type="text" name="username" /><br />
						<label>Password</label><br />
						<input type="password" name="password" /><br />
						<input type="submit" value="login" />
					</form>
				';
			}


		 ?>

	</div>
</section>

<?php include_once('footer.php'); ?>

 

Edited by Html
Something is displaying

Share this post


Link to post
Share on other sites

if (!func::checkLoginState($dbh) )
	func::createRecord($dbh, $row['user_username'], $row['user_id']);

You're trying to use static methods of a class called func.  If that class and those methods are not defined, then that's a fatal error.

Share this post


Link to post
Share on other sites

Hmm, I'm only using the files from the site of the youtube user, his video showed the examples working on his machine. So I'm going on the whim with this.

 

Chapter 5 in the book goes into functions, but nothing like this. More a basic intro, nothing like this.

Edited by Html
Learning php

Share this post


Link to post
Share on other sites

Yeah, that's a class with static methods.  That's what the :: operator is for.

Share this post


Link to post
Share on other sites

So what do I need to get this to work properly then? I thought these files would work as is.

Share this post


Link to post
Share on other sites

If you want to use that particular code then you need to define that class.  Maybe it was written for you in some tutorial, or maybe it's up to you to write the details of the methods that the code is trying to use.

Share this post


Link to post
Share on other sites

I honestly don't know, I got this from the website of the video creator.

How would I get this to work with some sort of function?

Share this post


Link to post
Share on other sites

I guess so.

I emailed somebody who knows some website design, may be they can produce something that I need for free if possible. See how it goes with it. I'm going nowhere with this, I'm going to keep being out of the loop with this.

 

Share this post


Link to post
Share on other sites
On 9/4/2018 at 10:55 PM, justsomeguy said:

You need to write the functions to do what you need them to do.

Not sure if you can,  but would you mind giving ago at creating the basic login setup? You could use the free host infinityfree to setup a working system. Thank you.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×