[SOLVED] Php Forms problem

This is a discussion on "[SOLVED] Php Forms problem" within the PHP Forum section. This forum, and the thread "[SOLVED] Php Forms problem are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Closed Thread
 
LinkBack Thread Tools
  #1 (permalink)  
Old Oct 4th, 2007, 10:31
Reputable Member
Join Date: Oct 2007
Location: Liverpool UK
Age: 29
Posts: 216
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] Php Forms problem

Hey! I am creating a dating site and have created a php based website with appache server & a working mysql database for the information from my forms to go into the database. When i use dreamweavers record insertion wizerd the infomation from my forms goes directly into my database, but when i use list menu forms the data does not go in the data base. The internet states i need to hand code the php code to do a loop so the information goes into my database from my list menu forms. I have no idea how to peform such a task, i am really new to php code, is ther anyone who can give me a working example so i can compare mine and finnish my site???

So a quick run down of my problem: I need php code that will allow list menus from my php pages to submit data from the list menus directly into my mysql database. Here is my Email NeoReloaded@hotmail.co.uk If any can write this code form me and outline which peace of code does what i would be very gratefull!!! Below is my code.... I have attached my php file to this post

HTML: Select all
 <table width="750" height="501" border="0">
                        <tr>
                          <td width="355" height="497" valign="top"><div align="center">
                              <p class="style3">I Am\We Are</p>
                              <p>
                                <label>
                                <select name="select" id="select">
                                  <option value="">Man</option>
                                  <option value="">Women</option>
                                  <option value="">Couple_Man_Women</option>
                                  <option value="">Couple_2_Women</option>
                                  <option value="">Couple_2_Men</option>
                                  <option value="">Group</option>
                                </select>
                                </label>
                              </p>
                              <p class="style2">Interested In Meeting</p>
                              <p>
                                <label>
                                <select name="select2" id="select2">
                                  <option value="">A Man</option>
                                  <option value="">A Women</option>
                                  <option value="">A Group</option>
                                  <option value="">A Couple\Man&amp;Women</option>
                                  <option value="">A Couple\2 Women</option>
                                  <option value="">A Couple\2 Men</option>
                                </select>
                                </label>
                              </p>
                              <p class="style2">For</p>
                              <p>
                                <label>
                                <select name="select3" id="select3">
                                  <option value="">PhoneFantasies\Emal\Erotic-Chat</option>
                                  <option value="">Discreet Relationship (Sex Partner)</option>
                                  <option value="">1 on 1 Sex</option>
                                  <option value="">Group Sex</option>
                                </select>
                                </label>
                              </p>
                              <p class="style2">Enter Birth Date</p>
                              <p>
                                <label>
                                <select name="select4" id="select4">
                                  <option value="">January</option>
<option value="">Febuary</option>
                                  <option value="">March</option>
                                  <option value="">April</option>
                                  <option value="">May</option>
<option value="">June</option>
                                  <option value="">July</option>
                                  <option value="">August</option>
                                  <option value="">September</option>
                                  <option value="">October</option>
                                  <option value="">November</option>
                                  <option value="">December</option>
                                </select>
                                </label>
                                <label>
                                <select name="select5" id="select5">
                                  <option value="">1</option>
                                  <option value="">2</option>
                                  <option value="">3</option>
                                  <option value="">4</option>
                                  <option value="">5</option>
                                  <option value="">6</option>
                                  <option value="">7</option>
                                  <option value="">8</option>
                                  <option value="">9</option>
                                  <option value="">10</option>
                                  <option value="">11</option>
                                  <option value="">12</option>
<option value="">13</option>
                                  <option value="">14</option>
                                  <option value="">15</option>
                                  <option value="">16</option>
                                  <option value="">17</option>
                                  <option value="">18</option>
                                  <option value="">19</option>
                                  <option value="">20</option>
                                  <option value="">21</option>
<option value="">22</option>
                                  <option value="">23</option>
                                  <option value="">24</option>
                                  <option value="">25</option>
<option value="">26</option>
                                  <option value="">27</option>
                                  <option value="">28</option>
                                  <option value="">29</option>
                                  <option value="">30</option>
<option value="">31</option>
                                </select>
                                </label>
                                <label>
                                <select name="select6" id="select6">
                                  <option value="">2007</option>
                                  <option value="">2006</option>
                                  <option value="">2005</option>
                                  <option value="">2004</option>
                                  <option value="">2003</option>
                                  <option value="">2002</option>
                                  <option value="">2001</option>
                                  <option value="">2000</option>
                                  <option value="">1999</option>
                                  <option value="">1998</option>
                                  <option value="">1997</option>
                                  <option value="">1996</option>
                                  <option value="">1995</option>
                                  <option value="">1994</option>
                                  <option value="">1993</option>
                                  <option value="">1992</option>
                                  <option value="">1991</option>
                                  <option value="">1990</option>
<option value="">1989</option>
                                  <option value="">1988</option>
                                  <option value="">1987</option>
                                  <option value="">1986</option>
                                  <option value="">1985</option>
                                  <option value="">1984</option>
                                  <option value="">1983</option>
                                  <option value="">1982</option>
                                  <option value="">1981</option>
                                  <option value="">1980</option>
                                  <option value="">1979</option>
                                  <option value="">1978</option>
                                  <option value="">1977</option>
                                  <option value="">1976</option>
                                  <option value="">1975</option>
                                  <option value="">1974</option>
                                  <option value="">1973</option>
                                  <option value="">1972</option>
                                  <option value="">1971</option>
                                  <option value="">1970</option>
                                  <option value="">1969</option>
                                  <option value="">1968</option>
                                  <option value="">1967</option>
                                  <option value="">1966</option>
                                  <option value="">1965</option>
                                  <option value="">1964</option>
                                  <option value="">1963</option>
                                  <option value="">1962</option>
                                  <option value="">1961</option>
                                  <option value="">1960</option>
                                  <option value="">1959</option>
                                  <option value="">1958</option>
                                  <option value="">1957</option>
                                  <option value="">1956</option>
                                  <option value="">1955</option>
                                  <option value="">1954</option>
                                  <option value="">1953</option>
                                  <option value="">1952</option>
                                  <option value="">1951</option>
                                  <option value="">1950</option>
                                  <option value="">1949</option>
                                  <option value="">1948</option>
                                  <option value="">1947</option>
                                  <option value="">1946</option>
                                  <option value="">1945</option>
                                  <option value="">1944</option>
                                  <option value="">1943</option>
                                  <option value="">1942</option>
                                  <option value="">1941</option>
                                  <option value="">1940</option>
                                  <option value="">1939</option>
                                  <option value="">1938</option>
                                  <option value="">1937</option>
                                  <option value="">1936</option>
                                  <option value="">1935</option>
                                  <option value="">1934</option>
                                  <option value="">1932</option>
                                  <option value="">1931</option>
<option value="">1930</option>
                                </select>
                                </label>
                              </p>
                              <p class="style2">Post Code</p>
                              <p class="style2">
                                <label>
                                <input name="textfield" type="text" id="textfield" />
                                </label>
                              </p>
                              <p class="style2">UK Area</p>
                              <p class="style2">
                                <select name="select7" id="select7">
                                  <option value="">Any</option>
                                  <option value="">England-Bath Avon</option>
                                  <option value="">England-Bedforshire</option>
                                  <option value="">England-Berkshire</option>
                                  <option value="">England-Brighton</option>
                                  <option value="">England-Bristol</option>
                                  <option value="">England-Buckinghamshire</option>
                                  <option value="">England-Cambridgeshire</option>
<option value="">England-Channel Isles</option>
                                  <option value="">England-Cheshire</option>
                                  <option value="">England-Cornwall</option>
                                  <option value="">England-County Durham</option>
                                  <option value="">England-Cumbria</option>
                                  <option value="">England-Derbyshire</option>
                                  <option value="">England-Devon</option>
                                  <option value="">England-Dorset</option>
                                  <option value="">England-Essex</option>
                                  <option value="">England-Gloucestershire</option>
                                  <option value="">England-Hampshire</option>
                                  <option value="">England-Herefordshire</option>
                                  <option value="">England-Huntingdonshire</option>
                                  <option value="">England-Isle Of White</option>
                                  <option value="">England-Isle Of Sily</option>
                                  <option value="">England-Kent</option>
                                  <option value="">England-Lancashire</option>
                                  <option value="">England-Leicestershire</option>
                                  <option value="">England-Lincolnshire</option>
                                  <option value="">England-London</option>
                                  <option value="">England-Manchester</option>
                                  <option value="">England-Merseyside</option>
                                  <option value="">England-Middlesex</option>
                                  <option value="">England-Norfolk</option>
<option value="">England-Northamptonshire</option>
                                  <option value="">England-Northumberland</option>
                                  <option value="">England-Nottinghamshire</option>
                                  <option value="">England-Oxfordshire</option>
                                  <option value="">England-Rutland</option>
                                  <option value="">England-Shropshire</option>
                                  <option value="">England-Somerset</option>
                                  <option value="">England-Staffordshire</option>
                                  <option value="">England-Suffolk</option>
                                  <option value="">England-Surrey</option>
                                  <option value="">England-Sussex</option>
                                  <option value="">England-Tyne &amp; Wear</option>
                                  <option value="">England-Warwickshire</option>
                                  <option value="">England-West midlands</option>
                                  <option value="">England-Westmorland</option>
                                  <option value="">England-Wiltshire</option>
                                  <option value="">England-Worcestershire</option>
                                  <option value="">England-Yorkshire</option>
                                  <option value="">Isle Of Man</option>
                                  <option value="">N Ireland-Artrim</option>
                                  <option value="">N Ireland-Amagh</option>
                                  <option value="">N Ireland-Bellfast</option>
                                  <option value="">N Ireland-Down</option>
                                  <option value="">N-Ireland-Femanagh</option>
                                  <option value="">N-Ireland-Londondery</option>
                                  <option value="">N-Ireland-Tyrone</option>
                                  <option value="">Scotland-Aberdeenshire</option>
                                  <option value="">Scotland-Angus</option>
                                  <option value="">Scotland-Argylshire</option>
                                  <option value="">Scotland-Ayrshire</option>
                                  <option value="">Scotland-Banffshire</option>
                                  <option value="">Scotland-Berwicshire</option>
                                  <option value="">Scotland-Buteshire</option>
                                  <option value="">Scotland-Caithness</option>
                                  <option value="">Scotland-Clackmannanshire</option>
                                  <option value="">Scotland-Dumfriesshire</option>
                                  <option value="">Scotland-Dundee city</option>
                                  <option value="">Scotland-Edinburgh</option>
                                  <option value="">Scotland-Falkirk</option>
                                  <option value="">Scotland-Fife</option>
                                  <option value="">Scotland-Highland</option>
                                  <option value="">Scotland-Inverness-shire</option>
                                  <option value="">Scotland-Isle of Islay</option>
<option value="">Scotland-Isle of Lewis</option>
                                  <option value="">Scotland-Isle of Skye</option>
                                  <option value="">Scotland-Kincarineshire</option>
                                  <option value="">Scotland-Kinross-shire</option>
                                  <option value="">Scotland-Kirkcudbrightshire</option>
                                  <option value="">Scotland-Lanarkshire</option>
                                  <option value="">Scotland-Midlothian</option>
                                  <option value="">Scotland-Morayshire</option>
                                  <option value="">Scotland-Naimshire</option>
                                  <option value="">Scotland-Orkney</option>
<option value="">Scotland-Peeblesshire</option>
                                  <option value="">Scotland-Perthshire</option>
                                  <option value="">Scotland-Renfrewshire</option>
                                  <option value="">Scotland-Ross-shire</option>
                                  <option value="">Scotland-Roxburghshire</option>
                                  <option value="">Scotland-Selkirkshire</option>
                                  <option value="">Scotland-Shetland</option>
                                  <option value="">Scotland-Sterlingshire</option>
                                  <option value="">Scotland-Sutherland</option>
                                  <option value="">Scotland-West Lothiann</option>
                                  <option value="">Scotland-Western Isles</option>
                                  <option value="">Scotland-Wigtownshire</option>
                                  <option value="">Wales-Anglesey</option>
                                  <option value="">Wales-Brecknockshire</option>
                                  <option value="">Wales-Caemarfonshire</option>
                                  <option value="">Wales-Cardiff</option>
                                  <option value="">Wales-Cardiganshire</option>
                                  <option value="">Wales-Carmarthenshire</option>
                                  <option value="">Wales-Ceredigion</option>
                                  <option value="">Wales-Denbighshire</option>
                                  <option value="">Wales-Flintshire</option>
                                  <option value="">Wales-Glamorgan</option>
                                  <option value="">Wales-Gwynedd</option>
                                  <option value="">Wales-Merioneth</option>
                                  <option value="">Wales-Monmouthshire</option>
                                  <option value="">Wales-Montgomeryshire</option>
                                  <option value="">Wales-Newport</option>
                                  <option value="">Wales-Pembrokeshire</option>
                                  <option value="">Wales-Radnorshire</option>
                                  <option value="">Wales-Swansea</option>
                                </select>
                              </p>
                              <p class="style2">&nbsp;</p>
                              <p class="style2">&nbsp;</p>
                          </div></td>
                          <td width="385" valign="top"><p align="center" class="style2">Enter A Valud Email Address</p>
                            <p align="center">
                              <label>
                              <input name="textfield2" type="text" id="textfield2" />
                              </label>
                            </p>
                            <p align="center" class="style1">(Your Email Address Must Be Accurate &amp; True As Your Password Neccessery To Login Will be Sent To This Address)</p>
                            <p align="center" class="style2">Please Choose A User Name</p>
                            <p align="center" class="style2">
                              <label>
                              <input name="textfield3" type="text" id="textfield3" />
                              </label>
                            </p>
                            <p align="center" class="style1">Please Keep Your Username Inbetween 4 To 16 Characters long With No Spaces Or Special Characters</p>
                            <p align="center" class="style2">Please Choose A password</p>
                            <p align="center" class="style1">
                              <label>
                              <input name="textfield4" type="text" id="textfield4" />
                              </label>
                            </p>
                            <p align="center" class="style2">(Please Note Anyone Using There Real Name As There User Name Is Not Recommended)</p>
                            <p align="center" class="style2">
                              <label>
                              <input type="submit" name="button" id="button" value="Submit" />
                              </label>
                          </p></td>
                        </tr>
                      </table>
                      <input type="hidden" name="MM_insert" value="form1" />
                    </form>
                    <p align="left">&nbsp;</p>
                
                    
<form method="post" id="form3">
</form>
<p>&nbsp;</p>  
<form method="post" id="form2">
</form>
<p>&nbsp;</p>
                </div></td>
              </tr>
            </table>
        </div></td>
      </tr>
    </table>
 <p align="center">&nbsp;</p>
        <form method="post" id="form4">
        </form>
    <p>&nbsp;</p>
   </div>
Attached Files
File Type: php Sign Up.php (30.5 KB, 5 views)

Last edited by Rakuli; Oct 4th, 2007 at 11:02. Reason: Please use [html][/html] BBC when posting HTML

  #2 (permalink)  
Old Oct 4th, 2007, 11:06
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Php Forms problem

Hi Longstand and welcome to Webforumz,

You don't have any values assigned to your <option value="">'s.

When processing, PHP won't see the what is between the <option> tags, it will only see the value. In the case of these drop down boxes, the value is empty for all of them.

I am looking at your code but without any values it won't work at all
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
  #3 (permalink)  
Old Oct 4th, 2007, 11:13
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Php Forms problem

Okay, I've had a look at your code and I'm happy to write an example script but I need to know what your MySQL database setup is. ie. What table are these values going into and what are the column names?

Let me know.

Cheers
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
  #4 (permalink)  
Old Oct 4th, 2007, 12:09
Reputable Member
Join Date: Oct 2007
Location: Liverpool UK
Age: 29
Posts: 216
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Php Forms problem

Quote:
Originally Posted by Rakuli View Post
Okay, I've had a look at your code and I'm happy to write an example script but I need to know what your MySQL database setup is. ie. What table are these values going into and what are the column names?

Let me know.

Cheers
Cheers bud Really glad your helping me!!

My mysql database is called: sexy
My table is called: details

My columns are called: 1) i_am

2) interested_in

3) here_for

4) m

5) d

6) y

7) postal

8) uk_area

9) web_address

10) u

11) p

The connection type is: conndb

Each drop down list in my code is in the exact order i have listed the columns above. The code i sent u has the exact order they go in and which options to choose. Basically for instance the top column listed above says i_am so i need the user to choose a option from the Iam\We are list menu and have the choice inputted into the i_am column of my mysql database.

My code has a drop down menu saying: Interested In Meeting - i would like the choice from that list menu to go into interested_in column of my database.

I have a list menu in my code saying: For - i would like the choice from this menu to go directly into the here_for column of my database.

My code has a: enter birth date section, i would like the month chosen from the list menu input into the m cloumn of my database. The day chosen from the list menu input into the d column of my database. And the year chosen from my list menu input into the y column of my database.

I have a section in my code saying: post code. Iwould like the user to input the post code into this section (Textfield) i have already managed this part myslef but i am getting multible entries in my mysql database. basically it is creating about 5 rows every time i enter just one text field data entry.

I have a UK Area section in my code and would like the choice from the list menu to go into the uk_area column of my database.

I have a: enter a valid Email Address part of my code (Textfield) and would like the user to input there email address into the textfield and the input go directlty into the web_address column of my database.

I have a: Please Choose a user name section of of my code (Textfield) and would like the user to input a desired username into the Textfield, and the input go directly into the database.

I have a: Please choose a password section of my code (textfield) and would like the user to input a desired password into the textfield, and the input go into my database.

I have attached a screen image of my database to this post in the form of a html file for you to look at... Cheers for your help i am really gratfull.... Would you be able to create the code/put it were it needs to go & put explanations on why its there so i can try and learn it myself.... I am very new to php so i am not enterly confident i will no were to put the code myself.... thanks again
Attached Files
File Type: html Untitled-1.html (420 Bytes, 3 views)
  #5 (permalink)  
Old Oct 4th, 2007, 12:23
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Php Forms problem

Hi again Longstand,

You have a fairly length form so I have written the below code to handle it.

I stress though, you *must* add values to your <option>'s. PHP will not receive any useful values until you do.

eg.
<option value="man">Man</option>
<option value="woman">Women</option>
<option value="Coupke_man_woman">Couple_Man_Women</option>
<option value="etc..">Couple_2_Women</option>
<option value="">Couple_2_Men</option>
<option value="">Group</option>

The below code is fairly simple but will do as you wish..

Now please note it is heavily commented and I have not included all your form elements, you will need to add these yourself but it should give you the gist.

PHP: Select all

<?php 

require_once('../Connections/conndb.php');

// Okay, your submit button is named "button" so we check that this has been clicked, if it has we can start the form processing

if (!empty($_POST['button'])) // !empty is a double check, checking for existence AND a value other than false, NULL, 0 or ""
{
    
// I am unsure about your magic quotes setting so I'll use this to strip slashes and escape correctly
    
    
$needStrip get_magic_quotes_gpc();
    
    
// Okay, let's assume that all form values are going into the same mySQL table
    // Create an array of the form fields -- using the form code you have below, it looks like this
    
    
$fields = array();
    
    
// The array key is the name of the form element
    
    
$fields['select'] = array('type'        's',         // The type of value - we'll use s for string or n for number (hard for a form to be anything else)
                               
'required'    true,    // Is the field *required*, we will stop the script if it's not set if this is true
                               
'column'        'i_am'    // The mysql data column where the value will go
                               
);
    
$fields['select2'] = array('type'        's',
                               
'required'    true,
                               
'column'        'interested_in'
                               
);
                               
    
// Etc.. and so on with the field names (excluding the submit of course)
    
    //Now, we will loop through each of the fields as listed above ^ and create the database query
    
    // To keep track of errors, we will have an error array
    
    
$errors = array();
    
    
// The query bits which will be built on below
    
$cols '';
    
$vals '';
    
    foreach (
$fields as $field => $dets// $field will hold the field name and $dets an array of the details
    
{
        
// remove preceding and trailing white space
        
$field trim($field);
        
        
// first check, is the value required AND empty?
        
if ($dets['required'] && empty($_POST[$field])) {
            
$errors[] = $field ' is a required field!'// Add to the errors
            
continue; // stop this loop and start with the next
        
}
        
        
// next check, data type (only check numbers as all post variables are strings
        
if ($dets['type'] == 'n' && !is_numeric($_POST[$field])) {
            
$errors[] = $field ' should be a number!';
            continue;
        }
        
        
// Don't bother adding this to the query if we have errors already of if there is no value
        
if (empty($_POST['field']) || count($errors))
            continue;
            
        
// Now add the SQL for the insert
        
        // Remove slashes if they exist
        
$_POST[$field] = $needStrip stripslashes($_POST[$field]) : $_POST[$field];
        
        
// Escape the data for safe SQL insertion
        
$_POST[$field] = mysql_real_escape_string($_POST[$field]);
        
        
// Add a comma to separate the values if there is one there already
        
if (!empty($cols)) {
        
$cols .= ', ';
        
$vals .= ', ';
        }
        
        
$cols .= $dets['column']; // the columns as in INSERT into Table (col1, col2)
        
$vals .= $dets['type'] == 'n' && is_numeric($_POST[$field]) ? $_POST[$field] : "'{$_POST[$field]}'"// the values as in VALUES ('this', 'that', 1)
    
    
}
    
    
// Now we have one of two things 1) A list of columns and values 2) errors
    
    
if (!count($errors) && !empty($cols) && !empty($vals)) {// No errors? and some values and columns? Continue to add the query        
    
        // contruct the query
        
$query "INSERT INTO details($cols) VALUES($vals)";
        
        
// run the query
         
mysql_select_db($database_conndb$conndb);
        
$result mysql_query($query) or die(mysql_error());
        
        
// success?
        
        
$message "Yay, you have been inserted as " mysql_insert_id(); // The insert ID
        
    
}  else { // Oh oh! errors
    
        // create an error string
        
$message '<strong>The following errors are ruining my feng shui man!</strong>
                    <ol>'
;
        
        foreach (
$errors as $err// $err will have the error message from above
            
$message .= '<li>' $err