View unanswered posts | View active topics It is currently Mon May 21, 2012 4:07 am



Reply to topic  [ 6 posts ] 
 Saving, retrieving form data to db with unique user id login 
Author Message
Junior Member

Joined: Thu Jun 12, 2008 10:56 am
Posts: 3
Post Saving, retrieving form data to db with unique user id login
I have three tables in mysql db

users
formfields
formdata

table "users":
(data entered using a registration form) I already made this work
Code:
userid   email      password   name
1   [EMAIL="bob@bo.com"]bob@bo.com[/EMAIL]   1234      bob
2   [EMAIL="carl@c.com"]carl@c.com[/EMAIL]   asdf      carl
3   [EMAIL="jon@dsa.com"]jon@dsa.com[/EMAIL]   qwert      jon


table "formfields":
Code:
fieldid      label      inputname   
1      Like red?   red
2      Got milk?   gotmilk
3      Like football?   likefootball   
4      Drink beer?   drinkbeer



The label/inputname values can be used to display my form and the ids used to store the data collected.

table "formdata":
//store any complete fields in 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
3   1      yes


once logged on
I print a hidden, in this case a read only field displaying the unique userid from the registration on to my form

login.php:
[PHP]//Connect to database

mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());

session_start();
$username = $_POST['username'];
$password = md5($_POST['password']);

$query = "select * from users where username='$username' and password='$password'";

$result = mysql_query($query);

if (mysql_num_rows($result) != 1) {
$error = "Bad Login";
echo "<font color='red'>Error, Please re-enter or register!</font>";
include "login.html";

} else {
$_SESSION['username'] = "$username";

while($row = mysql_fetch_array($result))
{
echo Welcome . " " . $row['name'];
echo "
";
echo "<input type='text' name='userid' id='userid' value='$row[userid]' readonly>";
}

include "sample.php";

}

?>[/PHP]

redirected to "sample.php" I have:
[HTML]
Welcome whomever
<input type='text' name='userid' id='userid' value='Users unique id' readonly><html>
<head>
</head>
<body>

<form name="sample" method="post" action="save1.php">
<table>
<tr>
<td>

Like Red?
<select name="red" id="red">
<option>Yes
<option>No
</select>


Got Milk?
<select name="gotmilk" id="gotmilk">
<option>Yes
<option>No
</select>



Like football?
<select name="likefootball" id="likefootball">
<option>Yes
<option>No
</select>



Drink beer?
<select name="drinkbeer" id="drinkbeer">
<option>Yes
<option>No
</select>



<input type="submit" name="submit" value="Submit">

</td>
</tr>
</table>
</form>


</body>
</html>[/HTML]

Now what I want to do is once I submit the form "save1.php"
I want it to save to table "formdata" like shown above with the userid, fieldid and value of the fields.

I have looked all over the internet and I know the table set up is the best way to do it because I also later need the user to retrieve the data for later editing on the same form.

send1.php:
[PHP]//Connect to database

mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());

$fieldstoinsert=array('red','gotmilk','likefootball','drinkbeer');

$query = "INSERT INTO formdata (userid, fieldid, value) VALUES "

foreach($_POST as $fieldid => $value){
if(in_array($fieldid,$fieldstoinsert)){
if(trim($value)!=""){
$q.="('$userid', '$fieldid', '$value')";
}
}
}

?>[/PHP]
this is the bit of info I have found but the foreach is not working
What am I missing, Im still learning and any help would be appreciated.
I know I could use a join but still not familiar how to get it to work.

Code:
[color=#000000][color=#0000bb]select [/color][color=#007700]* [/color][color=#0000bb]from users
INNER JOIN formdata on users[/color][color=#007700].[/color][color=#0000bb]userid[/color][color=#007700]=[/color][color=#0000bb]formdata[/color][color=#007700].[/color][color=#0000bb]userid[/color][/color]


Can anyone help fill in the gaps?


Thu Jun 12, 2008 11:31 am
Report this post
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 6 posts ] 

Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
© Copyright 2003-2008 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.