Sponsored by NuSphere - PHP Software for PHP Application Developers - On Sale This Week for $100



Go Back   PHP-Editors > Programming Help > PHP Programming Help

PHP Programming Help Post any question relating to PHP Programming here and hopefully someone can help.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 2008-06-12, 01:43 PM
Junior Member
 
Join Date: Jun 2008
Posts: 3
jodarecode is on a distinguished road
Default 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    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?
Reply With Quote
Must read Review for Serious PHP Developers


NuSphere PhpED 5.5 : The Staff of php-editors.com recently spent a few days working with NuSphere PhpED 5.5 (a popular PHP IDE) and NuCoder 2.0 (a PHP Encoding Utility), read up on all the details.

Sponsored Links
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 09:25 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.1.0
© Copyright 2003-2008 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.