Jump to content

PHP/HTML Form confusing


kalzerd

Recommended Posts

Hello and greetings fellow programmers!

 

I am still new and learning PHP/HTML. This is my first project. What I have to do is to create a registration/login form and store the data in database then display the data stored. I created 3 pages which is registration form, connect.php and page3.php(use to display stored data).

 

Here is my registration form :

</head><body>    <div class="register-form"><h1>Register</h1><form action="connect.php" method="POST">    <p><label>User Name : </label>	<input id="username" type="text" name="Username" placeholder="username" /></p>		<p><label>E-Mail       : </label>	 <input id="password" type="email" name="Email" required placeholder="example@email.com" /></p>      <p><label>Password   : </label>	 <input id="password" type="password" name="Password" placeholder="password" /></p>	 	 <p><label>UserID     : </label>	 <input id="password" type="password" name="UserID" placeholder="ID" /></p>     <a class="btn" href="login.php">Login</a>    <input class="btn register" type="submit" name="submit" value="Register" />    </form></div></body></html>

here is my connect.php :

<html><body><?phpsession_start();$dbhost = "localhost";$dbuser = "root";$dbpass = "12345";$dbname = "student";$con = mysql_connect($dbhost,$dbuser,$dbpass);if (!$con){    die("Database Connection Failed" . mysql_error());}else{	$seldb = mysql_select_db($dbname);	if(!$seldb){		die("CANNOT SELECT Database");	}else{		header("Location: page3.php");	}}?></body></html>

and here is my page3.php :

<?php	require('connect.php');        if (isset($_POST['Username']) && isset($_POST['Password'])){        $username = $_POST['Username'];	$email = $_POST['Email'];        $password = $_POST['Password'];	$userid = $_POST['UserID'];         $query = "INSERT INTO `user` (Username, Email, Password, UserID) VALUES ('$username','$email','$password','$userid')";        $result = mysql_query($query);        if($result){            $msg = "User Created Successfully.";        }    }    ?>

Ignore the page3.php for a while. My question is when I click register, it should go to connect.php to connect to database mysql and then proceed to page3.php and display "User Created Successfully", BUT it does not. When i click register, it just go to connect.php and display nothing. I did check my codes couple of times but still could not figure out what's wrong(**noted that i have my latest version of xampp running). I hope someone can help me because I'm out of idea. Sorry and Thanks!

Link to comment
Share on other sites

header("Location: page3.php"); will fail because it is within outputted html

 

http://www.w3schools.com/php/func_http_header.asp first example

 

and wouldn't going to page3.php using header result in

 

form

connect.php -> page3.php(header()) -> connect.php (require()) -> page3.php

 

basically loading itself twice, don't know if $_POST would survive getting to two redirects.

 

Edit: in fact it should end up going in a continuous loop

Edited by dsonesuk
Link to comment
Share on other sites

Ignore the page3.php for a while. My question is when I click register, it should go to connect.php to connect to database mysql and then proceed to page3.php and display "User Created Successfully", BUT it does not.

Like dsonesuk mentioned, the reason the header redirect does not work is because you have HTML in your connect.php file. There's no reason to have that there at all.Secondly, PHP doesn't work like you think it does. Every request is separate. When you connect to a database, and then redirect, the page that you redirected to is not automatically connected to the database. It's a new request, separate from the previous one. When a request ends PHP closes things like database connections.Also, post data is never passed in a redirect. A redirect is a get request, not a post request. There is not a way to send a header and tell the browser to redirect with a post request with specific post data. All redirects are get requests, so when you redirect to page3 you lose all of the data in $_POST.Just have your form submit directly to page3. That page should include the connection file like you're already doing. The connection file should not redirect or contain any HTML or anything else outside of the PHP code.Also, if you're just learning you really need to avoid the mysql extension, it has been out of date for over a decade. Use either mysqli or PDO, and learn how to use prepared statements so that you can avoid SQL injection vulnerabilities. There's a short introduction to prepared statements using mysqli here:http://mattbango.com/notebook/code/prepared-statements-in-php-and-mysqli/
Link to comment
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
×
×
  • Create New...