-4

I am making a webpage. Its user's form basically. I am taking username, password n email ID n checking if they already exist in the database or not.
If they exist i want to move to a next page showing 'username already exists' in case username is dere in DB or it will show 'You already have account on this id' in case email id matches.
this line will be followed by form. So can you please help me how to do it?
here's my code

<?php
$host = "localhost";
$user = "root";
$password = "";
$database = "signin";
$table = "userdetails";
$userName = $_POST['userName'];
$emailId = $_POST['emailID'];
$password = $_POST['password'];
$cnfPassword = $_POST['cnfPassword'];
$con = mysql_connect($host, $user) or die(mysql_error());
mysql_select_db($database, $con) or die(mysql_error());
if ($password == $cnfPassword && strlen($password) != 0) {
    echo "Your Password Matched\n";
    echo "<br />";
} else {
    echo "Please enter a valid password \n";
    echo "<br />";
}
$result = mysql_query("SELECT * FROM `userdetails` WHERE `UserName`=\"$userName\"");
$count = 0;
while ($row = mysql_fetch_array($result)) {
    $count++;
    echo "UserID ".$row['UserID']."<br />UserName: ".$row['UserName']."<br />EmailID: ".$row['emailID']."\n";
}
if ($count == 0 && $password == $cnfPassword && strlen($password) != 0) {
    $query = "INSERT INTO userdetails (UserName,emailID,Password) VALUES('$userName','$emailId','$password')";
    $result = mysql_query($query);
    echo $result;
    echo "<br />";
    echo "You ar welcomed\n";       
    echo "<br />";
} else {
    action("sign_up.php");
    echo "<br />Username already exists\n";
    echo "<br />";
}
?>
aayush
  • 24
  • 1
  • 9
  • Googled "redirect from one page to another using php": 85.600.000 results (0,27 seconds). Downvoted. – STT LCU Jun 20 '13 at 07:54
  • [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://www.brightmeup.info/article.php?a_id=2). – michaelb958--GoFundMonica Jun 20 '13 at 07:57
  • @michaelb958 use the markup in this page for your disclaimers next time ^^ http://brightmeup.info/comment.html – STT LCU Jun 20 '13 at 08:00
  • 1
    @STTLCU Thanks. I always wondered where the official template was... – michaelb958--GoFundMonica Jun 20 '13 at 08:02
  • thnx @michaelb958.. i tried to use mysqli queries but my syntax wasn't proper. Next thing i wanna ask is about header function..is there any way i can send username or emailId variable through it? – aayush Jun 20 '13 at 08:55

2 Answers2

2

You should use the php header function, you can direct a page directly like this:

header('Location: yourpage.php')

Or you could redirect with a delay of a couple of second like this:

header("Refresh: 5;url=yourpage.php");

When using the header function make sure no other output is made before the header, for more info on this look here

Community
  • 1
  • 1
Daanvn
  • 1,254
  • 6
  • 27
  • 42
  • i used the header function n i wanna know can i send data like username or email id showing me like which parameter already exists in the database along the header function? – aayush Jun 20 '13 at 07:42
0

You can use header('Location: URLTOTHEPAGE'); if no header is sent before in your script.

herrhansen
  • 2,413
  • 1
  • 14
  • 15