Jump to content

Form help


mxpimp47

Recommended Posts

I have a complete registration setup. But I wanted to add the ability to have a form for when a user is logged in that it has the current logged in user filled in the form by default. And all they have to do is click signup and then it adds the username into a new table.So I have the part figured out on showing the current logged in user in the form. But I have been doing research on the best way to have the second step, which is the part upon clicking signup to be added to a new table.What I’m trying to do is have a weekly signup form so I can tell how many and which users are going to attend an event. Am I even going about it the correct way by creating a new table? Or should it just be a new row in the first table created in my DB?I have 12 events I will be having almost 12 weeks in a row. And after then event I will be entering some values per username that signed up. For at the end of the 12 events to be a tally.From what I have researched, it seems I need to use the SQL insert into function. But I am having problems getting this accomplished.Would some of you php experts please lead me in the correct direction. I have been searching online for 5 days. Cant seem to get any example to translate for me and be able to apply it to my project.Here is a pastebin of the first part of the functionality I have working for the signup page – http://pastebin.com/NeUG2×4b you’ll notice I do not have the it completed. And there is stuff that will be removed from that code. I am trying to tear apart the other form and insert the new lines of code that I need. I am new to php and writing things completely from scratch is difficult!

Link to comment
Share on other sites

  • Replies 51
  • Created
  • Last Reply

It makes sense to have a table store records for who is registered for what. If your events have IDs, and your users have IDs, then you can have a table with eventID/userID columns to store who is signed up for what event.Other than that, you probably don't want to have the username in the signup form. People could enter other people's usernames and sign up for them. It would probably be better to have the page which actually signs someone up (the form processing page) just get the username from the session and use that, so that it always signs up the logged-in user instead of looking for that information in the form.

Link to comment
Share on other sites

It makes sense to have a table store records for who is registered for what. If your events have IDs, and your users have IDs, then you can have a table with eventID/userID columns to store who is signed up for what event.Other than that, you probably don't want to have the username in the signup form. People could enter other people's usernames and sign up for them. It would probably be better to have the page which actually signs someone up (the form processing page) just get the username from the session and use that, so that it always signs up the logged-in user instead of looking for that information in the form.
Ok my problem is my lack of knowledge I don't know exactly how to utilize the table info and put it all together. I searched and searched for examples that I could understand. I couldn't find anything. Would you be willing to write some sort of an example? I have followed a lot of php tutorials and nothing has given me the knowledge to put the code together. I guess it may just be a little more advanced. Maybe you could tell me the kind of function it is like possibly. And that way I can look it up. I don't mind doing the work! And I like having to figure things out. I think I'm just stuck at this point! Thanks for your help.
Link to comment
Share on other sites

The lookup table is just used to record who is signed up for what. If that table is called "event_users", and has columns called "event" and "user" which hold the event and user IDs, and you want to look up information for all users from the users table who are signed up for a certain event, like event ID 10, you can do this:

SELECT * FROM users WHERE id IN (SELECT user FROM user_events WHERE event=10)

That will return all records from the users table that have signed up for the event with ID 10. The inserts are basic enough, if a user wants to register for a particular event you would first look up that record in the event_users table to make sure they aren't already registered, and if not you just insert a new record with the user ID and event ID.Other than that, if you want to post the code you're using and ask specific questions (take things one step at a time), we'll be able to give specific answers.

Link to comment
Share on other sites

The lookup table is just used to record who is signed up for what. If that table is called "event_users", and has columns called "event" and "user" which hold the event and user IDs, and you want to look up information for all users from the users table who are signed up for a certain event, like event ID 10, you can do this:
SELECT * FROM users WHERE id IN (SELECT user FROM user_events WHERE event=10)

That will return all records from the users table that have signed up for the event with ID 10. The inserts are basic enough, if a user wants to register for a particular event you would first look up that record in the event_users table to make sure they aren't already registered, and if not you just insert a new record with the user ID and event ID.Other than that, if you want to post the code you're using and ask specific questions (take things one step at a time), we'll be able to give specific answers.

The code I posted the paste bin link to was the signup page I was working on. What do I need to do to get those users signed up for an event? Maybe I'm not following you exactly. Would you like to see what table structure I currently have? I kinda understand what your saying but dont know what needs to come first. Like I said I'm green when it comes to php and all the "simple tutorials" I get. My mind hasnt "got it" yet where I can just start creating all kinds of rad stuff! Oh a question that came to mind to ask was, any time your trying to pull something from the database you have to query the database correct?On this particular project, what would this be called or classified as? How advanced, if at all, is what I am trying to accomplish? I'm trying to get more familiar with the proper terms so I can research things more efficiently. And better explain my self when requesting help! I have looked at the php tutorials from this forum and many other sources as well. Do you have any recommendations for some more examples vs just the basic entry level php ingredients that is so common? That way in the mean time I can be further educating my self with this stuff. I wish it would just click in my head already! Thanks for your time justsomeguy, I look forward to your reply so you can help me help you help me with this lil project haha!
Link to comment
Share on other sites

What comes first is creating the database tables. If you don't know how to do that, let me know. Once you have the tables set up, everything is either putting data into the tables or getting it out. You're either adding information to the events and users tables, or you're reading it and displaying it or whatever. It's really not any more complicated than that.In terms of how you get the data to add to the database, you have several options. One of them is a form, you could have a form where you submit both IDs. You could do the same thing with a URL, e.g.:add_event.php?event=10&user=123You can also use the session. If your user is logged in, then you wouldn't pass the user ID, just the event ID, e.g.:add_event.php?event=10On the add_event page, you would look up the user's information in the session and add them to the event that was passed in the URL. The w3schools site has a tutorial about using PHP to process forms, and there's another description here:http://w3schools.invisionzone.com/index.php?showtopic=12509So you could get the event ID from $_GET or $_POST, look up the user's ID from the session, and insert a record into the event_users table to record that the user is signed up for the event.

Oh a question that came to mind to ask was, any time your trying to pull something from the database you have to query the database correct?
Right, a database query is a command you send to the database. The query could be retrieving data, or it could be adding, updating, deleting, etc. The w3schools site has tutorials in the PHP and SQL sections about using various database queries.
On this particular project, what would this be called or classified as?
It's pretty vague at this point, so far it's an application where you have users and events, and users signing up for events. I'm not really sure if that has a formal name.
How advanced, if at all, is what I am trying to accomplish?
In terms of PHP and databases, it's fairly basic. Remember, it's either just adding data to the database, changing it, or looking it up.
Link to comment
Share on other sites

What comes first is creating the database tables. If you don't know how to do that, let me know. Once you have the tables set up, everything is either putting data into the tables or getting it out. You're either adding information to the events and users tables, or you're reading it and displaying it or whatever. It's really not any more complicated than that.In terms of how you get the data to add to the database, you have several options. One of them is a form, you could have a form where you submit both IDs. You could do the same thing with a URL, e.g.:add_event.php?event=10&user=123You can also use the session. If your user is logged in, then you wouldn't pass the user ID, just the event ID, e.g.:add_event.php?event=10On the add_event page, you would look up the user's information in the session and add them to the event that was passed in the URL. The w3schools site has a tutorial about using PHP to process forms, and there's another description here:http://w3schools.invisionzone.com/index.php?showtopic=12509So you could get the event ID from $_GET or $_POST, look up the user's ID from the session, and insert a record into the event_users table to record that the user is signed up for the event.Right, a database query is a command you send to the database. The query could be retrieving data, or it could be adding, updating, deleting, etc. The w3schools site has tutorials in the PHP and SQL sections about using various database queries.It's pretty vague at this point, so far it's an application where you have users and events, and users signing up for events. I'm not really sure if that has a formal name.In terms of PHP and databases, it's fairly basic. Remember, it's either just adding data to the database, changing it, or looking it up.
Thank you for the break down. I do know how to create the database side of it all. So I have that part dialed, now I just have to be able to add and pull the data like you said. I do know how to do the basic form. But it sounds like I need to do the session possibly? I am going to review over some of the links you provided for me. And see if I can get the next step going. One part I'm still a bit unclear is the session ID. Are you saying that every time a user signs up it will give it an event ID for that user (once that is created in the DB first) when using the session method?
Link to comment
Share on other sites

I'm just using the term events for the things that people sign up for in your application. When a user logs in you can create a session for them to store the username for the logged-in user which you can access on other pages. The events would get added to the database however you want to do that, you could create an admin page where you set up events, or let each user set up events, or however you want to do that. But once the events are in the database then you can get their IDs from the table and use those to have people sign up for them.

Link to comment
Share on other sites

I'm just using the term events for the things that people sign up for in your application. When a user logs in you can create a session for them to store the username for the logged-in user which you can access on other pages. The events would get added to the database however you want to do that, you could create an admin page where you set up events, or let each user set up events, or however you want to do that. But once the events are in the database then you can get their IDs from the table and use those to have people sign up for them.
I understand that, what I would like to do is have a form with the logged in user already displayed so they cant make any typo's and just click signup to submit the form. I dont need to create a table in my DB for sessions do I? Sessions just work when they are logged in correct? But I do need to create a table in my DB for when they click signup for the result to be stored so I can keep track.
Link to comment
Share on other sites

Right, you don't need to support sessions in the database, PHP handles those. Your signup link can just be something like this:signup.php?event=10On the signup.php page, you would look up the user in something like $_SESSION['username'], or wherever you store it, and use that instead of having it submitted through the form. The event ID gets passed in the URL, so you would get that in $_GET['event'], and then add the event ID and user ID to the signup table.

Link to comment
Share on other sites

I dont need to create a table in my DB for sessions do I?
You dont need to create table in DB unless you want to use custom session handling.
Sessions just work when they are logged in correct?
You need to use session to determine that user is logged in or not. as justsomeguys elaborated you need to assighn the session variables after being authenticated a user.Then you can use that information across the page till session is alive.You can use session anytime you want its not dependent to user logged in or not.More details here http://w3schools.com/php/php_sessions.asp
Link to comment
Share on other sites

I understand that, what I would like to do is have a form with the logged in user already displayed so they cant make any typo's and just click signup to submit the form. I dont need to create a table in my DB for sessions do I? Sessions just work when they are logged in correct? But I do need to create a table in my DB for when they click signup for the result to be stored so I can keep track.
I am trying to work on displaying the username in the form with the session. I dont have the form code set to do anything like its suppost to yet (I actually dont know how to type the code to store the session ID with the post method). But I can work on that after I get the first part accomplished. I dont know why the session that I have specified for the username
$_SESSION['user_name'] = row$['username']

in the table wont work.

<!DOCTYPE html><?php//allow sessions to be passed so we can see if the user is logged insession_start();//connect to the database so we can check, edit, or insert data to our users table$con = mysql_connect('localhost', 'gaming', 'tournaments') or die(mysql_error());$db = mysql_select_db('tournaments', $con) or die(mysql_error());//include out functions file giving us access to the protect() function made earlierinclude "functions.php";?><html lang="en"><head>	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">	<link rel="stylesheet" type="text/css" href="style.css">	<title>signup</title></head><body>	<?php		//If the user has submitted the form	if($_POST['submit']){		//protect the posted value then store them to variables		$username = protect($_POST['username']);		$password = protect($_POST['password']);				//Check if the username or password boxes were not filled in		if(!$username || !$password){			//if not display an error message			echo "<center>You need to fill in a <b>Username</b> and a <b>Password</b>!</center>";		}else{			//if the were continue checking					//select all rows from the table where the username matches the one entered by the user					$res = mysql_query("SELECT * FROM `users` WHERE `username` = '".$username."'");					$num = mysql_num_rows($res);					//check if there was not a match					if($num == 0){						//if not display an error message						echo "<center>The <b>Username</b> you supplied does not exist!</center>";					}else{						//if there was a match continue checking						//select all rows where the username and password match the ones submitted by the user						$res = mysql_query("SELECT * FROM `users` WHERE `username` = '".$username."' AND `password` = '".$password."'");						$num = mysql_num_rows($res);						//check if there was not a match						if($num == 0){							//if not display error message							echo "<center>The <b>Password</b> you supplied does not match the one for that username!</center>";						}else{							//if there was continue checking							//split all fields fom the correct row into an associative array							$row = mysql_fetch_assoc($res);							//check to see if the user has not activated their account yet							if($row['active'] != 1){								//if not display error message								echo "<center>You have not yet <b>Activated</b> your account!</center>";							}else{								//if they have log them in								//set the login session storing there id - we use this to see if they are logged in or not								$_SESSION['uid'] = $row['id'];								//show message								echo "<center>You have successfully logged in!</center>";																//set the session username to call their name when logged in								$_SESSION['user_name'] = $row['username'];								echo $_SESSION['user_name'];								//update the online field to 50 seconds into the future								$time = date('U')+50;								mysql_query("UPDATE `users` SET `online` = '".$time."' WHERE `id` = '".$_SESSION['uid']."'");								//redirect them to the usersonline page								header('Location: usersOnline.php');							}						}					}				}			}			?>			<form action="signup.php" method="post">				<div id="border">					<table border="0" cellpadding="2" cellspacing="0">						<tbody><tr>							<td>Username:</td>							<td><?php												if (isset($_SESSION['uid']))																		?>Hello,						<?php echo $_SESSION['user_name']; ?></td>						</tr>						<tr>							<td colspan="2" align="center"><input name="submit" value="SignUp" type="submit"></td>						</tr>					</tbody></table>			  </div></form></body></html>

Link to comment
Share on other sites

<!DOCTYPE html><?php//allow sessions to be passed so we can see if the user is logged insession_start();
You need to start the session before you send any output to browser.and probably a typo here too...
$_SESSION['user_name'] = row$['username']
$_SESSION['user_name'] = $row['username']

Link to comment
Share on other sites

You dont need to create table in DB unless you want to use custom session handling.You need to use session to determine that user is logged in or not. as justsomeguys elaborated you need to assighn the session variables after being authenticated a user.Then you can use that information across the page till session is alive.You can use session anytime you want its not dependent to user logged in or not.More details here http://w3schools.com/php/php_sessions.asp
I just had posted with a problem. And no sooner did I read your reply I realized I did not create the session in the login page! So I added that and now once logged in the form I have is showing the current logged in username. Now where can I read about posting session data into a table. I do need to be able to have a record of people that have signed up for an event once logged in. So then I can turn around and display that data so they know who all is attending. Thank you for your help!
Link to comment
Share on other sites

There's a description in the tutorial:http://www.w3schools.com/php/php_get.aspThat page calls it a function, but it's not a function, just an array.
I see how that works in the example. But what about the "event ID" and "user ID" you mentioned, where do I get those values? Im not following exactly what your saying how to obtain the data and store it to my DB.
Link to comment
Share on other sites

Those records should be stored in the database. You should have a table for users in the database which holds all of your user records. One of the columns in the table should be ID, so that's the user ID. Similarly, you should have a table of events in your database (or whatever you want to call the things people can sign up for), and along with all of the details about the event you should have an ID column. That is the event ID. So you'll need some way to add users and events to the database, and for the signup pages you can get the event details (including the ID), and print the ID in the URL like above.

Link to comment
Share on other sites

Those records should be stored in the database. You should have a table for users in the database which holds all of your user records. One of the columns in the table should be ID, so that's the user ID. Similarly, you should have a table of events in your database (or whatever you want to call the things people can sign up for), and along with all of the details about the event you should have an ID column. That is the event ID. So you'll need some way to add users and events to the database, and for the signup pages you can get the event details (including the ID), and print the ID in the URL like above.
I'm struggling to visualize what you are telling me. Please dont give up on me, I'm trying very hard to grasp what your saying. I understand I should create 12 events in my "events" table with an "ID" for each event. There is already a "user" ID so I have that part. I was able to achieve several ways of displaying the current user logged in pre populated in the form. But can I POSTWhat would an example of markup be for a form with these variable's? I read what you guys are telling me, and then further google to find more documentation or examples. I'm just a lil lost at the moment.
Link to comment
Share on other sites

I still think you should just get the username from the session when they register instead of submitting the username through the form where it can be changed, like I described in post 10. If you really want it in the form, you can print it wherever you want in the HTML markup.<input type="text" name="username" value="<?php echo $_SESSION['user_name']; ?>">

Link to comment
Share on other sites

Those records should be stored in the database. You should have a table for users in the database which holds all of your user records. One of the columns in the table should be ID, so that's the user ID. Similarly, you should have a table of events in your database (or whatever you want to call the things people can sign up for), and along with all of the details about the event you should have an ID column. That is the event ID. So you'll need some way to add users and events to the database, and for the signup pages you can get the event details (including the ID), and print the ID in the URL like above.
I'm struggling to visualize what you are telling me. Please dont give up on me, I'm trying very hard to grasp what your saying. I understand I should create 12 events in my "events" table with an "ID" for each event. There is already a "user" ID so I have that part. I was able to achieve several ways of displaying the current user logged in pre populated in the form. I haven't been able to send the data to the specified table. Can I POST data that is being pre populated from a session. I know you said sessions aren't stored in a database. But can I use the session info that is pre populated in a form? What would an example of markup be for a form with these variable's? I read what you guys are telling me, and then further google to find more documentation or examples. I'm just a lil lost at the moment. It's just a form, but with pre populated info (just a username) going to a specific table. I am struggling to get the same thing I already have but with a lil extra dynamic functionality.
Link to comment
Share on other sites

I still think you should just get the username from the session when they register instead of submitting the username through the form where it can be changed, like I described in post 10. If you really want it in the form, you can print it wherever you want in the HTML markup.<input type="text" name="username" value="<?php echo $_SESSION['user_name']; ?>">
I did that. But I couldn't post the data to the table yet...
Link to comment
Share on other sites

Here is what I have so far from changing up so much stuff back and forth. not knowing what I'm doing really....

<!DOCTYPE html><?php//allow sessions to be passed so we can see if the user is logged insession_start();//connect to the database so we can check, edit, or insert data to our users table$con = mysql_connect('localhost', 'gaming', 'tournaments') or die(mysql_error());$db = mysql_select_db('tournaments', $con) or die(mysql_error());//include out functions file giving us access to the protect() function made earlierinclude "functions.php";?><html lang="en"><head>	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">	<link rel="stylesheet" type="text/css" href="style.css">	<title>signup</title></head><body>	<?php		//If the user has submitted the form	if($_POST['submit']){		//protect the posted value then store them to variables		$user_name = protect($_POST['gamertag']);					//select all rows from the table where the username matches the one entered by the user					$res = mysql_query("SELECT * FROM `entries` WHERE `user_name` = '".$gamertag."'");					$num = mysql_num_rows($res);					//check if there was not a match					if($num == 0){						//if not display an error message						echo "<center>The <b>Username</b> you supplied does not exist!</center>";					}								//update the online field to 50 seconds into the future								$time = date('U')+50;								mysql_query("UPDATE `users` SET `online` = '".$time."' WHERE `id` = '".$_SESSION['uid']."'");								//redirect them to the usersonline page								header('Location: usersOnline.php');										}			?>			<form action="signup.php" method="post">				<div id="border">					<table border="0" cellpadding="2" cellspacing="0">						<tbody><tr>							<td>Username:</td>							<td><input type="text" name="user_name" value="<?php echo $_SESSION['user_name']; ?>"></td>						</tr>						<tr>							<td colspan="2" align="center"><input name="submit" value="SignUp" type="submit"></td>						</tr>					</tbody></table>			  </div></form></body></html>

Link to comment
Share on other sites

Archived

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


×
×
  • Create New...