Jump to content
xisto Community
nol

Probably A Dumb Question But...

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 :(, 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 ClassesA-F right?Well, now this is what the database would kinda look likeUsername Class Field1 Field2 Field3 Field 4Testing1 1 1 10 1 1Testing2 2 1 1 10 1Testing3 3 10 1 1 1Testing4 4 1 1 1 10Testing5 5 10 10 1 1Testing6 6 1 1 1 1I 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.

Edited by nol (see edit history)

Share this post


Link to post
Share on other sites

You use radio buttons/dropdown menu in html form. After the info is sent to processing script, you could use if and elseif statements to check which options are chosen.

This is the html form:

<form action="script.php" method="post"><select name="testing1"><option>Select</option><option>option1</option><option>option2</option><option>option3</option></select><input type="Submit" /></form>

<?phpif ( $_POST['testing1'] == "option1" ) {/* insert data to database */ }elseif ( $_POST['testing1'] == "option2" ) {/* insert data to database */ }elseif ( $_POST['testing1'] == "option3" ) {/* insert data to database */ } else {echo "Don't be boring, just choose one...";?>

That's how I would do, but note that I'm not a genious when it comes to php. You can try this one out on a local server to see if it works(put whatever you want on the "/* insert data to database */"), I can't try it out right now, sorry.
Edited by Baniboy (see edit history)

Share this post


Link to post
Share on other sites

well the problem with this, is that I can't use testing1, testing2, as those are gonna be different depending on the person? so it wouldn't always be testing1, it could be something c ompletely random, but ty for that.

Share this post


Link to post
Share on other sites

So, the testing1 and testing2 are text fields? If yes then I understood you wrong, sorry. So what you need to do is change testing1 to field1 in the script. So first you insert the textbox contents to the cell, then you insert data to the next cells depending on the selection field values using the method in the script I wrote.

Edited by Baniboy (see edit history)

Share this post


Link to post
Share on other sites

well the way i was previously thinking is this:

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


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


only problem is idk if that'll work?

Edit: Ya it doesn't work, any idea why? registration form still works, it just won't update the information I want it to, infact it doesn't update anything at all
Edited by nol (see edit history)

Share this post


Link to post
Share on other sites

Have a look at the "Permissions" tutorial I sent a link for. If there are 6 Radio buttons to select from, define a Group for each User to belong to, then define the 6 User groups.

Share this post


Link to post
Share on other sites
<form action="script.php" method="post"><input type="radio" name="class" value="1" /> A<br /><input type="radio" name="class" value="2" /> A<form>
Notice the different value for the second radio button.
Are you getting any errors?
Is error-reporting set to "on"?
Is the db connection made properly?
var_dump the POST array to see what is in there.

Also, the variable you named $class should be checked as $_POST['class'] in the switch statement, unless there is some code you are not showing us.

Share this post


Link to post
Share on other sites

I did have a few errors with my script itself before, but they were just headers being sent twice, and I fixed it, so I'm thinking that it is fine. Here is what I am experiencing:Basically, the user is trying to register, so he enters username, password, and a confirm password, and email, and then picks a class. Depending on the class it changes other fields, which otherwise are being automatically set as '1' when created. I think when I do that I'm having the problem. The database connection is fine, I get no errors. I see all the information in the databases, such as username, password (in md5 hash), and email, and all the other fields set as 1. So I don't exactly know what that means, as to what is going on anyways. Here is exactly the script I'm working with.this is script.php (its actually called process.php on my webhost, but i called it script.php because thats what i called it above)

<?// 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,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;   }?>

I only added one case to test out, so ya...here is the actual form page:

<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>

(again I only have 2 radio form on there and will add more to it later)So do you think you know what could be wrong? I hope I explained what's going on well enough. Should I make class and the other fields in it's own table? Or is it fine in the table of users?

Notice from truefusion:
For big code, please use CODEBOX instead of CODE
Edited by truefusion (see edit history)

Share this post


Link to post
Share on other sites

change this code segment:

if ( isSet($_POST['submit']) ){echo '<pre>';print_r($_POST);echo '</pre>';   switch( strToLower($_POST['class']) ){case "1":	$result = mysql_query("UPDATE users SET field1='10,field3='10'");}break;
This will display the values in the POST array to see what is there.

Share this post


Link to post
Share on other sites

i did that, and it still doesn't seem to do much good. doesn't display anything on the process.php page or anything, and the database still acts same way.

<?// 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();		}	// We have completed all the checks.	// MD5 the password.	$password = md5($password);	mysql_query("INSERT INTO users (username, password, email) VALUES ('$username','$password','$email)");	mysql_query("INSERT INTO other (current,possible) VALUES ($current,$energypossible)");	mysql_query("INSERT INTO item2 (current,bank23) VALUES ('$current2','$bank23')");if ( isSet($_POST['submit']) ){echo '<pre>';print_r($_POST);echo '</pre>';{   switch( strToLower($_POST['class']) )   {    case "1":	$result = mysql_query("UPDATE users SET field1='10,field3='12'");break;  case "2":	$result = mysql_query("UPDATE users SET field1='3,field3='10'");break;    case "3":	$result = mysql_query("UPDATE users SET field1='6,field3='14'");break;    case "4":	$result = mysql_query("UPDATE users SET field1='2,field3='8'");break;    case "5":	$result = mysql_query("UPDATE users SET field1='9,field3='12'");break;  case "6":	$result = mysql_query("UPDATE users SET field1='5,field3='10'");	break;}break;   }}}	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: home.php");		die();		} else {		header("Location: main.php?error=3");		die();		}	break;	}

is what i have now if that makes a difference, and the case "login" part is considered an unexpected T_STRING? it wasn't before?

Edited by truefusion (see edit history)

Share this post


Link to post
Share on other sites

is what i have now if that makes a difference, and the case "login" part is considered an unexpected T_STRING? it wasn't before?

You have your switches messed up. I can barely make sense out of it. The switches end before they were supposed to but the code kept going, assuming that they were never finished. You have to recheck the code blocks for each switch and fix them to end when they are supposed to. I think the confusion started when you started a switch within a switch.

Share this post


Link to post
Share on other sites

i tried that before, and it wouldn't put anything else into the database before then. Tried it in multiple places, still get the unexpected T_STRING, it's something to do with that new line of code because it keeps giving me unexpected T_STRING in same spot, but if i take it away, it's fine (but it doesn't add to mysql databse :()edit: Oh alright I didn't see your post before truefusion, I tried what I think you meant? I checked the code blocks, I only saw 1 other switch, but I did see what you mean as me having a switch within a switch, so this is what I did:

<?// 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();		}	// We have completed all the checks.	// MD5 the password.	$password = md5($password);	// Insert the data into the user page.        mysql_query("INSERT INTO users (username, password, email) VALUES ('$username','$password','$email)");        mysql_query("INSERT INTO other (current,possible) VALUES ($current,$energypossible)");        mysql_query("INSERT INTO item2 (current,bank23) VALUES ('$current2','$bank23')");	// Redirect to login page	}if ( isSet($_POST['submit']) ){echo '<pre>';print_r($_POST);echo '</pre>';{switch( strToLower($_POST['class']) ){ case "1":$result = mysql_query("UPDATE users SET field1='10,field3='12' where id=$id");break;case "2":$result = mysql_query("UPDATE users SET field1='3,field3='10'where id=$id");break;case "3":$result = mysql_query("UPDATE users SET field1='6,field3='14' where id=$id");break;case "4":$result = mysql_query("UPDATE users SET field1='2,field3='8' where id=$id");break;case "5":$result = mysql_query("UPDATE users SET field1='9,field3='12' where id=$id");break;case "6":$result = mysql_query("UPDATE users SET field1='5,field3='10' where id=$id");}}	switch ($mode) {	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: home.php");		die();		} else {		header("Location: main.php?error=3");		}		}

still same thing is happening, and I think at least that I ended the switches where I'm supposed to? The thing is this script is my login/register processes script, so that is why I have the mode switch at the top. Should I just take out those switches and just make a seperate login and register script?

Edited by nol (see edit history)

Share this post


Link to post
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...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.