php form to save to db for user to add more info later to be emailed when finished
Heres what I want to do:
User creates new form using php with new file name "php/html".
This form already populated with form elements once created... is opened.
User enters information and needs to save to db(mysql) and came back later to finish and or correct earlier mistakes and email to me.
Heres what I already know:
I created a form using php to call up whats in db to display in text fields for editing. Saving the info to db is not the issue. Once I changed what needs to be changed, I submit/update db. I have another page which displays the resulting info. Emailing is also not the issue as I know how to do that as well.
Here is the issue's in ():
(User creating a new form with id/password with new name.)
that new form must have the fields I require for user to fill in.
once info is entered by user, they save to db.
(User to come back later to enter a id and password to pull up earlier saved file for editing)
After completing the form, user to save and email to me.
if user wants, still can comback to update and re email to me using that same id/password.
I know this can be accomplished, moreover user creating and retrieving forms to be send when ready.
Can some one can point me in the right direction or show an example of how this would work?
(User creating a new form with id/password with new name.)
What are you trying to do? I'm totally lost, you want your users to create forms, not just fill in the information?
that new form must have the fields I require for user to fill in.
If the forms must have information that you require, why don't you create the forms?
once info is entered by user, they save to db.
OK that's a normal thing to do.
(User to come back later to enter a id and password to pull up earlier saved file for editing)
What saved file? The form they created? I thought you saved the info to the database. This would be data not a file. But are you editing in a user's custom form?
After completing the form, user to save and email to me.
What save the form contents to the database or the currently unknown file? Or send you a form via email or just the info they entered into the form they created.
Can some one can point me in the right direction or show an example of how this would work?
Basically a login script in which the user will create, I dont know the code, everything they save goes under there new login to comback to later.
Once login is comfirmed it redirects them to my form, which will have there login tied to it so they can save info specific to there login, I was thinking earlier they use a php form to create a new form, the php code on that form will create another form, but I guess it would be better to have just one premade form ready to the user tied to there login.
Im just trying to make it so when multiple user's come to my site, they create there own login/registration, once logged in a form will appear, they fill in the form, lets say they dont complete it.....they want to come back to it later. So they save the <i>data</i> and log out. Then when any specific user comes back , they log in and select the form they were working on and complete it. Once completed, they have the option to save it again, print, and or email.
I'm new to the whole login scripts, and tying the data entered on the forms to the users login. everything else I can do myself.
The easiest way to manage this is with database. MySQL an MSSQL work well with PHP and MySQL is free and comes on most Linux Apache hosting plans.
There's only one shortcut to getting this working and that's paying someone to do it for you. If you want to learn it'll take some effort on your part.
The very first thing to do is read and try some tutorials. google 'php tutorial' and 'php mysql tutorial' for something that makes sense to you. read them do them, and spend the next couple of weeks learning the basics. Post your questions here. Lots of people to help out.
Read the tutorials and come back to this thread. Then it'll make much more sense.
//
two weeks later continue reading
//
To achieve what you want you simply have uses register which creates a unique userid for them. Then after that they can login with their registered details. Upon successful login you display your form and retrieve the data from the database for the unique userid. so you can store data like this
SQL table users
(data entered using a registration form)
fieldid label inputname
1 Like red? red
2 Got milk? gotmilk
3 Like football? likefootball
4 Drink beer? drinkbeer
5 Red or blue? colorchoice
The label/inputname values can be used to display your form and the ids used to store the data collected
SQL table formdata
//store any complete fields i this table
Code:
userid fieldid value
1 1 no
1 2 yes
2 1 yes
2 2 yes
1 3 yes
1 4 yes
2 3 yes
2 4 no
2 5 blue
3 1 yes
So now when you display your form you can get any data that was previously stored and display it in your form. If the users adds more data then you add it to the table when they save it. Or email it when you choose to send it.
Using the data in the formdata table you can use the ids to refer to the other tables and get all the data.
so the first row of data in table formdata:
Code:
userid fieldid value
1 1 no
userid 1 = bob
field id 1 = like red
value = no
so bob doesn't like red
and the last line of table formdata in the example
Code:
userid fieldid value
3 1 yes
userid 3 = jon
field id 1 = like red?
value = yes
jon likes red.
Using the ids from users and the ids from the form fields you can store repeated data and non repeated data ( the form answers) you can save them in a seperate table and use the ids to link it all together.
// lets check to see if the username already exists
$checkuser = mysql_query("SELECT username FROM users WHERE username='$username'");
$username_exist = mysql_num_rows($checkuser);
if($username_exist > 0){
echo "I'm sorry but the username you specified has already been taken. Please pick another one.";
unset($username);
include 'register.html';
exit();
}
// lf no errors present with the username
// use a query to insert the data into the database.
$query = "INSERT INTO users (name, email, username, password)
VALUES('$name', '$email', '$username', '$password')";
mysql_query($query) or die(mysql_error());
mysql_close();
$subject = "You have successfully registered at $yoursite...";
$message = "Dear $name, you are now registered at our web site.
To login, simply go to our web page and enter in the following details in the login form:
Username: $username Password: $password
Please print this information out and store it for future reference.
How do I direct the users answers that are save to mysql to there unique username to be saved for later editing. I can create the edit page no problem, its connecting the edit page and form to there unique login. I hope this makes sense, I do want to do this on my own so I will post what I have found as I go, any help much appreciated tho.
If your users have a unique id and your form fields have a unique id then a 3rd table can store both. One row in table 3 for each userid/formfieldid combination.
I spent a consideable amount of time writing the above response. Then you've asked the question I just answered.
If you've read it and don't understand it then please be more specific, otherwise please read the my response again.
and then from here I need to save, I was reading on JOIN but I still cant figure out how to input the fieldid and value in the table formdata using the formfield table.
mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());
PHP Code:
$query = "SELECT formfield.fieldid, users.userid "." FROM userid, fieldid "." WHERE inputname.fieldid = userid.fieldid";
//I figure this is as close as I can get for now, I know it may be wrong somewhere "but where?"
first thing is name="likefootball" id="likefootball1"
name and id should be the same.
You'll need to build a dynamic query. It's pretty simple when you understand concatenation. Concatenation is simple adding data to an existing variable. So first add some data to the $query var
PHP Code:
$query = "INSERT INTO formdata (userid, fieldid, value) VALUES";
now using .= (dot equals) you can add more data (concatenate) to the variable $query.
so for what ever condition calls a loop you can insert as many values as you need. Just build the query dynamically and insert the data in a single statement. It's really easy to get syntax errors doing this so you need to echo the query out to check it.
So you have a POST array. It consists of the key as the field name and the value. Have a look to see what is contains using print_r($_POST) after submitting the form.
You have some choices. You could have an array of filed names and only dynamically build the query if the $_POST key is in your array or prepend the fileds you want to submit with a common name like insertme_filedname. You can then preg teh key for insertme and explode the key on the _ and take the second half as the key name to insert. Either works as well as the other. I'll use the array method in this example.
You could build the array form the form fields table if you have one.
PHP Code:
//build fileds to insert array $fieldstoinsert=array('red','gotmilk','likefootball','drinkbeer');
//start building query $query = "INSERT INTO formdata (userid, fieldid, value) VALUES "
//loop through the post array foreach($_POST as $fieldid => $value){ if(in_array($fieldid,$fieldstoinsert)){ if(trim($value)!=""){//only basic validation here $q.="('$userid', '$fieldid', '$value')"; } } }
You'll now have multiple records in formdata.
You're right you'll need to join to get all the data in a single query. You dont have to join though. You could select the user info then select the formdata, build the array and compare the field names and echo the data if it exists.
if you wanted to join you could do this
PHP Code:
select * from users INNER JOIN formdata on users.userid=formdata.userid
Your right, I keep getting syntax errors for the foreach statement. I understand most how everything is supposed to work but being a newbie in the php/mysql area is a little convoluting to say the least. I'm just having a problem putting everything together, I'd say another week of learning, and I should beable to figure it out. till then any other help or ideas to help me understand would be great!!
When you get error it would help a lot if you copy them in here. When you do there's usually a line number associated with the error so point out which line has the error in the script. The line numbers wont tally if you've posted an extract from the script.
If you have problems the write a test script to deal with just that issue untill you get the concept. I do that all the time trying to work stuff out.
Lot of short specific questions are much better learning tools then questions like 'here's my script why doesn't it work?'. I rarely get involved in threads like that.
ok, I found the syntax error, it was missing ";" at the end of:
Code:
$query = "INSERT INTO formdata (userid, fieldid, value) VALUES ";
k, now I added an echo saying "data entered!!", it echos 4 times and nothing is put in the db. Im thinking somehow I need to match to the formfields table to use the fieldid.
mysql_query($query) or die(mysql_error());
mysql_close();
?>
now if it runs I get:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
mysql_query($q) or die(mysql_error());
mysql_close();
$q instead of $query right?
I still get:
Code:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''2', 'red', 'Yes')('2', 'gotmilk', 'Yes')('2', 'likefootball', 'Yes')('2', 'drin' at line 1
Bookmarks