This is a web development course project. Basically, we're asked to implement a conference paper submission system using PHP and MySQL. We've already worked with databases before so that should be easy. So far, I have both the registration and login forms connected to a database. Everything works fine except that I still don't know if this is the best way to handle forms. I read a bunch articles but most of them were outdated, so.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Register</title>
</head>
<body>
<?php
if(isset($_POST['register']))
{
// Data entered by the user.
$username = $_POST[username];
$password = $_POST[password];
// Create a connection to the database.
$con = mysql_connect("localhost", "test", "test");
// Select the database.
if(!mysql_select_db("myReview", $con))
exit(mysql_error());
// Check if username already exists.
if(mysql_fetch_array(mysql_query("SELECT * FROM Users
WHERE username = '$username'", $con))) {}
// Display error.
else
{
// Add author to the database.
mysql_query("INSERT INTO Users (username, password, type)
VALUES ('$username', '$password', 3)", $con);
echo "You are now a registered author. Please wait while we redirect you to the main page.";
// Redirect user to main page.
}
mysql_close($con);
}
}
else
{
?>
<form method="post" action="">
Username <input type="text" name="username"> <br>
Password <input type="password" name="password"> <br>
Email <input type="text" name="email"> <br>
<input type="submit" name="register" value="Register">
</form>
<?php
}
>
</body>
</html>
Should I keep the form inside the same page (else part)? Or should I redirect the user to a different page after they submit the form? Same goes to the login page. What about after they log in? Do I just create a session and check for it at the top of each page? I still don't know which way to go when it comes to validation either.
The thing is, I already know the answer to most of these questions, I just need to know which way is the best.
Question
Bassem
This is a web development course project. Basically, we're asked to implement a conference paper submission system using PHP and MySQL. We've already worked with databases before so that should be easy. So far, I have both the registration and login forms connected to a database. Everything works fine except that I still don't know if this is the best way to handle forms. I read a bunch articles but most of them were outdated, so.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Register</title> </head> <body> <?php if(isset($_POST['register'])) { // Data entered by the user. $username = $_POST[username]; $password = $_POST[password]; // Create a connection to the database. $con = mysql_connect("localhost", "test", "test"); // Select the database. if(!mysql_select_db("myReview", $con)) exit(mysql_error()); // Check if username already exists. if(mysql_fetch_array(mysql_query("SELECT * FROM Users WHERE username = '$username'", $con))) {} // Display error. else { // Add author to the database. mysql_query("INSERT INTO Users (username, password, type) VALUES ('$username', '$password', 3)", $con); echo "You are now a registered author. Please wait while we redirect you to the main page."; // Redirect user to main page. } mysql_close($con); } } else { ?> <form method="post" action=""> Username <input type="text" name="username"> <br> Password <input type="password" name="password"> <br> Email <input type="text" name="email"> <br> <input type="submit" name="register" value="Register"> </form> <?php } > </body> </html>Should I keep the form inside the same page (else part)? Or should I redirect the user to a different page after they submit the form? Same goes to the login page. What about after they log in? Do I just create a session and check for it at the top of each page? I still don't know which way to go when it comes to validation either.
The thing is, I already know the answer to most of these questions, I just need to know which way is the best.
Link to comment
Share on other sites
6 answers to this question
Recommended Posts