Jump to content

How would I create multiple classes and differenciate between them on user sign


Inter3st

Recommended Posts

Ok, so I want to create a user sign up/regsitration, on it I want to contain 6 options to choose from, for this case, i'll call them A,B,C,D,E,F. User can only select one, and depending on which one they select, different attributes will go down into their account. So if they choose A, It'll mark down in their database as 1, for one of the fields, and then it will change a couple of the other fields instead of the default of 1, it'll change some of them to maybe 10. Now if they selected B, it would do the same thing except mark down Id number 2 (for B), and then in some of the other fields mark down 10 (in different fields than A did).

 

So basically if that was really confusing,

 

I have 6 Classes

 

A-F right?

 

Well, now this is what the database would kinda look like

 

Username Class Field1 Field2 Field3 Field 4

Testing1  1      1      10      1      1

Testing2  2      1      1        10      1

Testing3  3      10    1        1      1

Testing4  4      1      1      1        10

Testing5  5      10    10      1      1

Testing6  6      1      1        1      1

 

I know how to make databases and such, I just don't know how to enter certain information into a database depending on which class the user picks. Of course there will be more fields such as password and such on sign up, but I hope you kind of get what I mean by that.

Switch/case?

switch($class) {
  case "1":
    $result = mysql_query("UPDATE table SET field1='10'");
  case "2":
    $result = mysql_query("UPDATE table SET field3='10'");
...
etc

 

Does that help or did I completely miss the point?

 

Can't seem to edit my previous post? but how would I integrate this with radio buttons? would it look something like this?

 

<form action="script.php" method="post">

<input type="radio" name="class" value="A" /> A

<br />

<input type="radio" name="class" value="B" /> A

<form>

 

switch($class) {

  case "1":

    $result = mysql_query("UPDATE table SET field1='10'");

  case "2":

    $result = mysql_query("UPDATE table SET field3='10'");

 

etc?

<form action="script.php" method="post">
<input type="radio" name="class" value="A" selected="selected" /> A
<br />
<input type="radio" name="class" value="B" /> B
<br />

<input tupe="submit" name="submit" /><!-- Blahhhh-->

</form>

 

 

<?php

if ( isSet($_POST['submit']) )
{
   switch( strToLower($_POST['class']) )
   {

      case "a":
         // query a
          break;
      case "b":
         // query b
         break;
      default:
         //default action? maybe Header("Location: blah.php");
   }
}
?>

     

I tried putting it in, I mean I have a bigger script then what i have put here for the script.php, but I mean, where should I put it? Basically I have in the script, checks to make sure username isn't double, email isn't double, to make sure the 2 password fields match, etc, but where should i put this part of the script? They don't choose the class until the last part so is that where i should put it? I'm not getting errors, but then again it's not editing the database like I would like it to.

 

  case "1":

    $result = mysql_query("UPDATE users SET field1='10'");

    $result = mysql_query("UPDATE users SET field3='10'");

 

is that wrong? That is what I have for my cases..

Can you show us the script so we have a better idea?

 

In the mean time, you can update multiple queries doing:

$result = mysql_query("UPDATE users SET field1='10',field3='10'");

This:

case "1":
    $result = mysql_query("UPDATE users SET field1='10'");
    $result = mysql_query("UPDATE users SET field3='10'");

Requires a "switch" statement in order for it to work. So, assuming you called the variable "class" on the switch statement, it would be:

switch($class) {
  case "1":
  $result = mysql_query("UPDATE users SET field1='10',field3='10'");
  break;
  case "2": 
  //etc.
  break;
  default:
  //default statement in case something is not defined
}

(I apologise, I forgot to put the breaks in my first post.)

<?
// confirm.php

$mode = $_POST['mode']; // Make sure a mode is specified.
if ($mode == "") {
print("No mode specified.  Please contact the webmaster to have this problem fixed.");
die();
}
require("vars.php"); // Get mysql info.
mysql_connect($host, $user, $pass) or die("MySQL connection error.");
mysql_select_db($db) or die("No such database."); // Connect to the right database
switch ($mode) {
case "register";
// First, check to make sure that all the data is filled in.
$username = $_POST['username']; // Min 4, Max 16
$password = $_POST['password']; // Min 4, Max 10
$cpassword = $_POST['cpassword']; // Min 4, Max 10
$email = $_POST['email']; // Min 4, Max 40
	if ($username == "") {
	header("Location: register.php?error=1&email=$email");
	die();
	}
	if ($password == "") {
	header("Location: register.php?error=2&email=$email&username=$username");
	die();
	}
	if ($cpassword == "") {
	header("Location: register.php?error=3&email=$email&username=$username");
	die();
	}
	if ($email == "") {
	header("Location: register.php?error=4&username=$username");
	die();
	}
	// Check to see if the passwords match
	if ($password != $cpassword) {
	header("Location: register.php?error=5&username=$username&email=$email");
	die();
	}
	// Now check the minimum lengths
	// Check the maximum lengths
	if (strlen($username) > 15) {
	header("Location: register.php?error=9&username=$username&email=$email");
	die();
	}
	if (strlen($password) > 50) {
	header("Location: register.php?error=10&username=$username&email=$email");
	die();
	}
	if (strlen($email) > 40) {
	header("Location: register.php?error=11&username=$username&email=$email");
	die();
	}
// Now check to make sure that we don't have the same username.
$temp = strtolower($username);
$query = mysql_query("SELECT COUNT(LOWER(username)) FROM users WHERE username='$temp'");
$result = mysql_fetch_array($query, MYSQL_NUM);
	if ($result[0] != 0) {
	die();
	}
// Check for duplicate email addresses.
$temp = strtolower($email);
$query = mysql_query("SELECT COUNT(LOWER(email)) FROM users where email='$email'");
$result = mysql_fetch_array($query, MYSQL_NUM);
	if ($result[0] != 0) {
	die();
	}

if ( isSet($_POST['submit']) )
{
   switch( strToLower($_POST['class']) )
{
case "1":
    $result = mysql_query("UPDATE users SET field1='10'");
    $result = mysql_query("UPDATE users SET field3='10'");
}
break;
      default:
   }

}
break;
case "login":
// Get user data
$username = $_POST['username'];
$password = $_POST['password'];
	if ($username == "") {
	header("Location: main.php?error=1");
	die();
	}
	if ($password == "") {
	header("Location: main.php?error=2");
	die();
	}
// MD5 the password.  Lower the username
$password = md5($password);
$username1 = strtolower($username);
$query = mysql_query("SELECT COUNT(LOWER(username)), id FROM users where username='$username' GROUP BY username");
$result = mysql_fetch_array($query, MYSQL_NUM);
	if ($result[0] != 1) {
	header("Location: main.php?error=3");
	die();
	}
$query = mysql_query("select password from users where id=$result[1]");
$id1 = $result[1];
$result = mysql_fetch_array($query, MYSQL_NUM);
	if ($result[0] == $password) {
	session_start();
	$_SESSION['id'] = $id1;
	// Get the actual user name
	$query = mysql_query("SELECT username FROM users where id=$id1");
	$result = mysql_fetch_array($query, MYSQL_NUM);
	$username = $result[0];
	$_SESSION['username'] = $username;
	$_SESSION['password'] = $password;
	// Check to see if they want to be logged in for a week.
	$week = $_POST['week'];
		if ($week == "yes") {
		// add the cookie data
		// find out the time a week from now
		$time = time();
		$time1 = 7 * 24;
		$time1 = $time1 * 60 *60;
		$time = $time + $time1;
		setcookie("username", $username, $time,"/");
		setcookie("password", $password, $time,"/");
		setcookie("id", $id1,$time,"/");
		setcookie("signedin","true",$time,"/");
		}
	header("Location: index.php");
	die();
	} else {
	header("Location: main.php?error=3");
	die();
	}
break;
}
?>

 

that would be my "script.php" (its actually process.php)

<html>
<head>
<title>Register</title>
</head>

<body>
<?
require("function.php");
$error = mysqlconnect();
if ($error === FALSE) {
die("Error connecting to MySQL.");
}

if (isset($_GET['error'])) {
print("<b><font color=Red>");
switch ($_GET['error']) {
	case 1:
	print("You forgot to enter your username.");
	break;
	case 2:
	print("You forgot to enter your password.");
	break;
	case 3:
	print("You forgot to confirm your password.");
	break;
	case 4:
	print("You forgot to enter your e-mail.");
	break;
	case 5:
	print("Your passwords do not match.");
	break;
	case 6:
	print("Your password must be greater than 4 characters long.");
	break;
	case 7:
	print("Your username must be greater than 4 characters long.");
	break;
	case 8:
	print("Your e-mail must be greater than 4 characters long.");
	break;
	case 9:
	print("Your username may not be more than 15 characters long.");
	break;
	case 10:
	print("Your password may not be more than 50 characters long.");
	break;
	case 11:
	print("Your e-mail cannot be longer than 40 characters.");
	break;
	case 12:
	print("The username, $username, is already in use.");
	break;
	case 13:
	print("The email, $email, is already in use.");
	break;
	default:
	print("Error information not understood.  Please contact the webmaster.");
	break;
	}
print("</font><br><hr>");
}
?>
<form action='process.php' method='post'>
<input type='hidden' name='mode' value='register'>
Username:  <input type='text' name='username' size=16 maxlength=15 value='<? print $_GET['username']; ?>'><br>
Password:  <input type='password' name='password' size=10 maxlength=50><br>
Confirm Password:  <input type='password' name='cpassword' size=10 maxlength=50><br>
E-mail:  <input type='text' name='email' size=40 maxlength=40 value='<? print $_GET['email']; ?>'><br>
<input type="radio" name="class" value="A" selected="selected" /> A
<br />
<input type="radio" name="class" value="B" /> B
<br />

<input tupe="submit" name="submit" /><!-- Blahhhh-->

</form>

I'm wondering maybe is it the placement like where I have it? I can't seem to see any other explanation as to why it isn't working. I did try moving it around, and no matter where I moved it besides the place where I have it now, it would make everything else not be added to the database (user info and whatnot)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.