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 bob@bo.com 1234 bob
2 carl@c.com asdf carl
3 jon@dsa.com 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 Code:
//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 "<br />";
echo "<input type='text' name='userid' id='userid' value='$row[userid]' readonly>";
}
include "sample.php";
}
?>
redirected to "sample.php" I have:
Code:
Welcome whomever<br /><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><br>
Got Milk?
<select name="gotmilk" id="gotmilk">
<option>Yes
<option>No
</select><br>
Like football?
<select name="likefootball" id="likefootball">
<option>Yes
<option>No
</select><br>
Drink beer?
<select name="drinkbeer" id="drinkbeer">
<option>Yes
<option>No
</select><br>
<input type="submit" name="submit" value="Submit">
</td>
</tr>
</table>
</form>
</body>
</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 Code:
//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')";
}
}
}
?>
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:
select * from users
INNER JOIN formdata on users.userid=formdata.userid
Can anyone help fill in the gaps?