0

OK, so I have a entry form login page saved as a .php file, its content is displayed in html and css. It displays a few things and then a log in form with 2 fields: Username, Password and then a 'Log in' button.

I have created a separate .php file which looks like this:

<?php

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

if ($username && $password)
 {

 $connect = mysql_connect("localhost", "root", "root") or die ("couldn't connect");

 mysql_select_db("sportsday") or die ("couldn't find db");

 }

 else 
 die("Please enter a username and password");

?>

I believe this code is now connected to my other page (http://jsfiddle.net/6yw8a2ue/) It shows some errors next to the fields in browser: "Notice: Undefined indiex: Username in G:\xampp\htdocs\Sportsday\entryformlongon.php"...

and once i enter a username and password it goes through to blank page with the error: "Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in G:\xampp\htdocs\Sportsday\login.php on line 11"

Charley Baker
  • 47
  • 1
  • 1
  • 6
  • 2
    This `if ($username&&£$password)` - remove the `£` - `if ($username && $password)` that's where the parse error originates from. Now, what do you mean by *"How can I link this php file to the other one?"* ? – Funk Forty Niner Oct 23 '14 at 17:39
  • well i was following a log in tutorial (https://www.youtube.com/watch?v=y7ae_cZahPs) and they used two separate php files and it connected fine. but for me its not – Charley Baker Oct 23 '14 at 17:45
  • @Fred-ii- i need the code show in the screenshot to be used for my log in page basically – Charley Baker Oct 23 '14 at 17:45
  • So, why not show us what you're using then, or is that your entire code? – Funk Forty Niner Oct 23 '14 at 17:45
  • I don't view YouTube videos, sorry. Do you need a form? Is that what you need, to pass information from the form to your posted code? – Funk Forty Niner Oct 23 '14 at 17:46
  • http://jsfiddle.net/6yw8a2ue/ this is what i am trying to connect the code above to – Charley Baker Oct 23 '14 at 17:48
  • You're wanting to check if username and password match in DB, am I getting this right? – Funk Forty Niner Oct 23 '14 at 17:50
  • Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide. – Jay Blanchard Oct 23 '14 at 17:51
  • What's with all these *wrong* answers below? Read the question/comments `@ALL_people`. – Funk Forty Niner Oct 23 '14 at 17:53
  • [`This is the question where I need an answer for...`](http://stackoverflow.com/questions/26534018/link-php-code-to-a-html-page#comment41694453_26534018) @CharleyBaker or am I the one not grasping the question? – Funk Forty Niner Oct 23 '14 at 17:59
  • As per your edit, there you go; deprecated. Use `mysqli_` or PDO as the notice states. Besides that, I have no idea what you want to do. I wish you well with that, I am moving on. – Funk Forty Niner Oct 23 '14 at 18:03
  • ok I will use that instead.. I just trying to create users for a log in form which connects to a mysql database – Charley Baker Oct 23 '14 at 18:06
  • Here http://www.wikihow.com/Create-a-Secure-Login-Script-in-PHP-and-MySQL – Funk Forty Niner Oct 23 '14 at 18:24
  • 1
    You have completely changed the original question so that nearly all the answers you have received are no longer relevant (as @Fred-ii- pointed out). If you have a different question you need to ask a new question rather than updating this one to the point of being unrecognizable from what was originally asked which was how to include the files correctly. – Ian Oct 24 '14 at 05:16

7 Answers7

2

Please look over this documentation. http://php.net/manual/en/function.require-once.php

You can require the file in any page needed.

iamcutler
  • 41
  • 1
  • 1
0

point your form action atribute to the php file.

<form method="post" action="phpsubmit.php">
  form stuff
</form>
danielson317
  • 3,121
  • 3
  • 28
  • 43
0

Require at the top of the page to be included. require 'filename';

http://www.w3schools.com/php/php_includes.asp

0
header('Location: http://www.example.com/');
exit;

I assume by link to other php file, you mean forward to another page once complete.

0

You can redirect to other page when fields are empty or inputs are not matching.Can simply do

 $username=$row["username"];
 $password=$row["password"];
 header("location:page_to_link.php?u=".$username);
 }
 else{
  header("location:page you want to redirect");
 }

you have to start session on the page you redirecting(linking) them by session_start().

Hope that helps

Salim Khan
  • 53
  • 1
  • 12
0

There are 4 options to include the file into the main document.

include
include_once
require
require_once

include doc, include_once doc, require doc, require_once doc

Each of these will add the code to your main file and do essentially the same thing. include and require will include the file as many times as the line of code is called (which can cause issues if you just need it called once). include_once and require_once will import the file to the document only one time regardless of whether or not that line of code is called multiple times.

A big difference between include and require lies in the type of error you will get. include will return a warning error if the file isn't found but your code will continue to execute (with errors). require will produce a fatal error which will kill the program then and there.

So if you have form.php and want to include it it would look something like this for each option.

include 'form.php';
include_once 'form.php';
require 'form.php';
require_once 'form.php';

Depending on where the document lies that you are trying to include you will need to get the correct directory first (ie if you are trying to include from from /path/file.php from /path/path/file.php). Unlike normal HTML '/' does not result in your site root. It results in your server root.

So if you have a file that exists at website.com/path/file.php you couldn't just do include '/path/file.php' because that would actually look for the file at the root of the server which would not exist.

To get around this you will want to utilize $_SERVER['DOCUMENT_ROOT'] which will then grab the root of your document from the server which might look something like the following (depending on your setup):

/var/www/html/website.com

And so rather than having to figure that out, you can just add the document root line to the include statements

include $_SERVER['DOCUMENT_ROOT'] . '/path/file.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/path/file.php';
require $_SERVER['DOCUMENT_ROOT'] . '/path/file.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/path/file.php';
Ian
  • 3,266
  • 4
  • 29
  • 37
0

You have forgotten to check if the form is submitted add the floowing in the beginning of your code:

if(isset($_POST ['Username']) && isset($_POST ['Password'])){

and off course, don't forget to place a close bracket at the end

jsp
  • 1