Current location: Hot Scripts Forums » Programming Languages » PHP » drop down list to read from 2 fields in table and insert into 2nd table


drop down list to read from 2 fields in table and insert into 2nd table

Reply
  #1 (permalink)  
Old 11-30-08, 02:32 PM
lallen30 lallen30 is offline
New Member
 
Join Date: Nov 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
drop down list to read from 2 fields in table and insert into 2nd table

I'm new to php and I've created a registration page.

This a registration page for a garbage company that when submitted needs let the customer know what their pick up day is based on the name of their street.

I have a drop down list field called 'streetname' which reads from one table that has a list of street names in one column and days in another column. The user can select their street and when submitted it inserts that street name into another table which is called users. This part seems to work fine but what I also need is for the 'day' field in the same row in the "street_day_list" table to be inserted into the same row in the "users" table.

If anyone can please look at my code and let me know what I can add or change to make it work I would be extremely grateful.
Thank you, Larry

the drop down menu section starts on line 117

PHP Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<link href="style.css" rel="stylesheet" type="text/css" />
<link href="layout.css" rel="stylesheet" type="text/css" />
<script src="rollover.js" type="text/javascript"></script>
<style type="text/css">
.style1 {
    font-size: 24;
    color: #FFFFFF;
    font-weight: bold;
}
.style2 {font-size: 24px;
    color: #FFFFFF;
    font-weight: bold;
}
.style3 {font-size: 27px;
    color: #FFFFFF;
    font-weight: bold;
}
#apDiv1 {
    position:absolute;
    left:170px;
    top:14px;
    width:842px;
    height:80px;
    z-index:auto;
    padding: 10px;
}
#apDiv2 {
    position:absolute;
    left:328px;
    top:93px;
    width:572px;
    height:573px;
    z-index:2;
}
#apDiv3 {
    position:static;
    left:480px;
    top:448px;
    width:254px;
    height:45px;
    z-index:3;
    font-family: Arial, Helvetica, sans-serif;
    border-top-style: solid;
    border-right-style: solid;
    border-bottom-style: solid;
    border-left-style: solid;

}
</style>
<script src="../Scripts/AC_RunActiveContent.js" type="text/javascript"></script>
</head>

<body  id="page">
<div align="center" id="apDiv2">

<?php

include("header.inc.php");


    
    
?>
  <form method="POST" action="register.php">
      <table border="0" style="font-size: 15px; font-weight:bold; font-family: Arial">
             <tr>                
            <td width="176" align="right">
                        <div align="right">Username:                </div></td>
      <td width="635" align="left">
<input type="text" name="username" value="<?php echo $_POST['username'];?>" />                </td>
             </tr>
            <tr>                
                <td width="176" align="right">
                        <div align="right">Password:                </div></td>
                <td align="left">
                        <input type="password" name="password" value="<?php echo $_POST['password'];?>" />                </td>
              </tr>
            <tr>                
                <td width="176" align="right">
                        <div align="right">E-Mail:                </div></td>
                <td align="left">
                        <input name="email" type="text" value="<?php echo $_POST['email'];?>" size="40" />                </td>
              </tr>
            <tr>                
                <td width="176" align="right">
                        <div align="right">First Name:                </div></td>
                <td align="left">
                        <input type="text" name="firstname" value="<?php echo $_POST['firstname'];?>" />                </td>
              </tr>
            <tr>                
                <td width="176" align="right">
                        <div align="right">Last Name:                </div></td>
                <td align="left">
                        <input type="text" name="lastname" value="<?php echo $_POST['lastname'];?>" />                </td>
              </tr>
            <tr>
              
              <td>&nbsp;</td>
             </tr>
              <tr align="right">
              <td width="176"><div align="right"></div></td>
              </tr>
            <tr>                
                <td width="176" align="right"><div align="right">Street number:</div></td>
                <td align="left"><input name="streetnumber" type="text" value="<?php echo $_POST['streetnumber'];?>" size="11" /></td>
               </tr>
            <tr>                
              <td width="176" align="right">
                <div align="right">Street name:</div></td>
              <td align="left">
            
                <?php 
                        
// If submitted, check the value of "select". If its not blank value, get the value and put it into $select.
                        
if(isset($select)&&$select!=""){
                        
$select=$_GET['select'];
                        }
        
                    
?>
                <select name="streetname">
                  <option value="">Please choose your street</option>
                  <?PHP
                                
// Get records from database (table "street_day_list").
                                
$list=mysql_query("SELECT * FROM street_day_list");

                                
$row_list mysql_fetch_assoc($list);
                                
$day $row_list['day'];


                                
// Show records by while loop.
                                
while($row_list=mysql_fetch_assoc($list)){
                                
                                
?>
                  <option value="<?PHP 
                            
echo $row_list['id']; ?><?PHP if($row_list['id']==$select){ echo "streetname""day"; }?>><?PHP echo $row_list['streetname'];
//                                  $day = $row_list['day']==$select;

                                  
                                  
                                   
?> </option>
                  <?PHP
                                
// End while loop.
                                
}
                                
?>
                </select></td>
</tr>
            <tr>                
                <td width="176" align="right">
                        <div align="right">Phone Number:                </div></td>
                <td align="left">
                        <input type="text" name="phonenumber" value="<?php echo $_POST['phonenumber'];?>" />                </td>
              </tr>
            <tr>                
                <td width="176" align="right"><div align="right">Other Number:                </div></td>
                <td align="left">
                        <input type="text" name="othernumber" value="<?php echo $_POST['othernumber'];?>" />                </td>
              </tr>
            <tr>
              <td align="right">&nbsp;</td>
              <td align="left"><input type="submit" name="submit" value="Submit" /></td>
            </tr>
               <tr colspan="2" align="right">
                  <td width="176"> 
                     <div align="right"></div></td>
            </tr> 
    </table>
</form>
    <p>
  <?php
        
        
if($_POST['submit']){
        
            
//error checking
            
            
$username $_POST['username'];
            
$password $_POST['password'];
            
$email $_POST['email'];
            
$firstname $_POST['firstname'];
            
$lastname $_POST['lastname'];
            
$streetnumber $_POST['streetnumber'];
            
$streetname $_POST['streetname'];
            
$phonenumber $_POST['phonenumber'];
            
$othernumber $_POST['othernumber'];
            
            
$curnum 0;
            
            if(!
$username) {
                
$curnum ++;
                echo 
"<font color='red'>"$curnum ". Please enter a Username name</font><br>\n"?><br><?php
            
}
            
            if(!
$password) {
                
$curnum ++;
                echo 
"<font color='red'>"$curnum ". Please enter a Password name</font><br>\n"?><br><?php
            
}

            if(!
$email) {
                
$curnum ++;
                echo 
"<font color='red'>"$curnum ". Please enter a Password name</font><br>\n"?><br><?php
            
}
            if(!
$firstname) {
                
$curnum ++;
                echo 
"<font color='red'>"$curnum ". Please enter a First name</font><br>\n"?><br><?php
            
}
            if(!
$lastname) {
                
$curnum ++;
                echo 
"<font color='red'>"$curnum ". Please enter a Last name</font><br>\n"?><br><?php
            
}
            if(!
$streetnumber) {
                
$curnum ++;
                echo 
"<font color='red'>"$curnum ". Please enter a Street Number</font><br>\n"?><br><?php
            
}
            if(!
$streetname) {
                
$curnum ++;
                echo 
"<font color='red'>"$curnum ". Please enter a Street name</font><br>\n"?><br><?php
            
}
            if(!
$phonenumber) {
                
$curnum ++;
                echo 
"<font color='red'>"$curnum ". Please enter a Phone Number</font><br>\n";
            }


            
            
$sql "SELECT * FROM `users` WHERE `username`='".$username."'";

            
$res mysql_query($sql) or die(mysql_error());
            
            If(
mysql_num_rows($res) > 0){
            
                
$curnum ++;
                echo 
$curnum ". We apologize but the username <b>".$username."</b> has already been taken.<br>&nbsp; &nbsp; Please try another Username.<br>";
            }
    
            
                if(
$curnum == 0){
 
//                              if($day == Tuesday){
                               
//                                if($row_list['id']==$select){ echo 'name'; }

                               
                    
mysql_query("INSERT INTO `users` VALUES(`id`,'".$username."','".$password."','".$email."','".$firstname."','".$lastname."','".$streetnumber."','".$streetname."','".$phonenumber."','".$othernumber."', '".$day."')") or die(mysql_error());

            
                    echo 
"<font color='green'><b>Thank you ".$firstname." for registering.<br><br>
                                                Your username is "
.$username.". <br><br>
                    <?php ?>                            Your password is "
.$password.". <br><br>
                                                Please note that your pick up day is </b></font>"
;

//                                if($streetname == 5){
                                
                                
echo $day;

//                            }
//                    } else {
//                        echo "We appoligize but our registration is temporarily down please click here to continue.";
//                    }
                
                    
?><br><br><div id="apDiv3">
  <p>&nbsp;</p>
  <p><a href="login.php">Click here to Login</a></p>
</div>
  <?php

            
}

            }
        
?>
</p>




        </p>






</div>
<div class="w">
    <div id="header">
          <div id="apDiv1">
          <p align="center" class="style3">Gwinnett Clean and Beautiful Service, Inc.</p>
            <p align="center" class="style2">&nbsp;</p>
            <p align="center"><span class="style1">Services provided exclusivly by:WASTEPRO of Georgia, Inc</span><br />
          </p>
      </div>
      <a href="index.html"><img alt="" src="images/logo.gif" class="logo" /></a><br />
      <br />
    </div>
  <div id="content"><br />
  </div>     
  <div id="footer">Waste Pro </div>
</div>

</body>
</html>
Reply With Quote
  #2 (permalink)  
Old 12-03-08, 01:04 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Hello lallen30,

How are you today?

I kinda wish you would have add all the CSS for the code you presented.
But I did the best I could with what you gave.

I took your code and re-wrote it so that you have all the error messages display on the right side of the form.
I think once you have a look at it, you will like the way I laid it out.
Unfortunately it only display well in IE.
So you will have to fiddle around with the CSS to get it to display better in all browsers.

I did not have a copy of header.inc.php, so I don't know what it will do to the page once it's included.

If you wish to have me help you with the CSS then you will have to show me all your CSS.
And a link to the web site would be helpful.
Or if you don't have one setup yet, then the dimensions of your images would help.

I put all the error messages in $error_array.
This way you can easily change the error message text.

If you have any questions then feel free to ask.

Ok, here is the modified code:

PHP Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title></title>
<style type="text/css">
.style1
{
 font-size: 24;
 color: #000000;
 font-weight: bold;
 }
.style2
{
 font-size: 24px;
 color: #000000;
 font-weight: bold;
 }
.style3
{
 font-size: 27px;
 color: #000000;
 font-weight: bold;
 }
#apDiv1
{
 position:absolute;
 left:170px;
 top:14px;
 width:842px;
 height:80px;
 padding: 10px;
 text-align:center;
 }
#apDiv3
{
 position:static;
 left:480px;
 top:448px;
 width:254px;
 height:45px;
 font-family: Arial, Helvetica, sans-serif;
 border-top-style: solid;
 border-right-style: solid;
 border-bottom-style: solid;
 border-left-style: solid;
 }
#form_container
{
 margin: 100px 0px 0px 328px;
 height:350px;
 }
#login
{
 margin: 100px 0px 0px 328px;
 height:350px;
 color:green;
 font-weight:bold;
 }
.label
{
 font-family: Arial, Helvetica, sans-serif;
 font-size: 24;
 font-weight: bold;
 color: #000000;
 width:125px;
 text-align:right;
 padding-top:2px;
 padding-right:10px;
 float:left;
 }
.inputdiv
{
 float:left;
 }
.submitdiv
{
 float:right;
 }
.error_sub
{
 color:#f00;
 font-weight:bold;
 margin-left:10px;
 margin-right:10px;
 }
.error_user
{
 color:#f00;
 font-weight:bold;
 margin-left:10px;
 margin-right:10px;
 margin-bottom:10px;
 padding:5px;
 border:3px solid #f00;
 }
#link
{
 width:254px;
 height:45px;
 font-family: Arial, Helvetica, sans-serif;
 border:2px solid #000;
 }
</style>
</head>
<body  id="page">
<div class="w">
 <div id="header">
  <div>
   <a href="index.html"><img alt="" src="images/logo.gif" class="logo" /></a>
  </div>
  <div id="apDiv1">
   <p class="style3">Gwinnett Clean and Beautiful Service, Inc.</p>
   <p class="style2">&nbsp;</p>
   <div class="style1">Services provided exclusivly by:WASTEPRO of Georgia, Inc</div><br /></p>
  </div>
  <br /><br />
 </div>
<?php
include("header.inc.php");
function 
validate_email($v)
{
 return 
eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$",$v);
 }
function 
validate_phonenumber($v)
{
 
$pattern '/^[\(]?(\d{0,1})[\.]?[\)]?[\s]?[\-]?[\(]?(\d{0,3})[\.]?[\)]?[\s]?[\-]?(\d{3})[\.]?[\s]?[\-]?(\d{4})[\s]?[x]?(\d*)$/';
 if(
preg_match($pattern,$v,$matches))
 {
  if(
$matches[0]){return true;}
  }
 return 
false;
 }
$error_msg="";
$error_set=9;
$error_array=array(
                   
"Please enter a Username",
                   
"Please enter a Password",
                   
"Please enter an E-mail address",
                   
"Please enter a valid email address",
                   
"Please enter a First name",
                   
"Please enter a Last name",
                   
"Please enter a Street Number",
                   
"Please enter a Street name",
                   
"Please enter a Phone Number",
                   
"Please enter a valid Phone Number"
                   
);
$select=empty($_GET['select'])?"2":$_GET['select'];
if(isset(
$_POST['submit']))
{
 
//error checking
 
$username = empty($_POST['username'])?"":$_POST['username'];
 
$password = empty($_POST['password'])?"":$_POST['password'];
 
$email = empty($_POST['email'])?"":$_POST['email'];
 
$firstname = empty($_POST['firstname'])?"":$_POST['firstname'];
 
$lastname = empty($_POST['lastname'])?"":$_POST['lastname'];
 
$streetnumber = empty($_POST['streetnumber'])?"":$_POST['streetnumber'];
 
$streetname = empty($_POST['streetname'])?"":explode(",",$_POST['streetname']);
 if(
is_array($streetname))
 {
  
$day $streetname[1];
  
$streetname $streetname[0];
  }
 else{
$day "";}
 
$phonenumber = empty($_POST['phonenumber'])?"":$_POST['phonenumber'];
 
$othernumber = empty($_POST['othernumber'])?"":$_POST['othernumber'];
 if(!
$username)
 {
  
$error_msg1 "<div class='error_sub'>< ".$error_array[0]."</div>";
  }
 else
 {
  
$error_msg1 "<div class='error_sub'>&nbsp;</div>";
  
$error_set--;
  }
 if(!
$password)
 {
  
$error_msg2 "<div class='error_sub'>< ".$error_array[1]."</div>";
  }
 else
 {
  
$error_msg2 "<div class='error_sub'>&nbsp;</div>";
  
$error_set--;
  }
 if(!
$email)
 {
  
$error_msg3 "<div class='error_sub'>< ".$error_array[2]."</div>";
  }
 elseif(!
validate_email($email))
 {
  
$error_msg3 "<div class='error_sub'>< ".$error_array[3]."</div>";
  }
 else
 {
  
$error_msg3 "<div class='error_sub'>&nbsp;</div>";
  
$error_set--;
  }
 if(!
$firstname)
 {
  
$error_msg4 "<div class='error_sub'>< ".$error_array[4]."</div>";
  }
 else
 {
  
$error_msg4 "<div class='error_sub'>&nbsp;</div>";
  
$error_set--;
  }
 if(!
$lastname)
 {
  
$error_msg5 "<div class='error_sub'>< ".$error_array[5]."</div>";
  }
 else
 {
  
$error_msg5 "<div class='error_sub'>&nbsp;</div>";
  
$error_set--;
  }
 if(!
$streetnumber)
 {
  
$error_msg6 "<div class='error_sub'>< ".$error_array[6]."</div>";
  }
 else
 {
  
$error_msg6 "<div class='error_sub'>&nbsp;</div>";
  
$error_set--;
  }
 if(!
$streetname)
 {
  
$error_msg7 "<div class='error_sub'>< ".$error_array[7]."</div>";
  }
 else
 {
  
$error_msg7 "<div class='error_sub'>&nbsp;</div>";
  
$error_set--;
  }
 if(!
$phonenumber)
 {
  
$error_msg8 "<div class='error_sub'>< ".$error_array[8]."</div>";
  }
 elseif(!
validate_phonenumber($phonenumber))
 {
  
$error_msg8 "<div class='error_sub'>< ".$error_array[9]."</div>";
  }
 else
 {
  
$error_msg8 "<div class='error_sub'>&nbsp;</div>";
  
$error_set--;
  }
 if(
$othernumber)
 {
  if(!
validate_phonenumber($othernumber))
  {
   
$error_msg9 "<div class='error_sub'>< ".$error_array[9]."</div>";
   }
  else
  {
   
$error_msg9 "<div class='error_sub'>&nbsp;</div>";
   }
  }
 
$sql "SELECT * FROM `user` WHERE `username`='$username'";
 
$res mysql_query($sql) or die(mysql_error());
 If(
mysql_num_rows($res))
 {
  
$error_msg10 "<div class='error_user'>We apologize but the username <b>".$username."</b> has already been taken.<br />Please try another Username.</div>";
  }
 else
 {
  
$error_msg10 "";
  
$error_set--;
  }
 }
if(!
$error_set)
{
 
mysql_query("INSERT INTO `user` VALUES('','".$username."','".$password."','".$email."','".$firstname."','".$lastname."','".$streetnumber."','".$streetname."','".$phonenumber."','".$othernumber."', '".$day."')") or die(mysql_error());
 echo 
"<div id='login'>
        Thank you "
.$firstname." for registering.
        <br />
        Your username is "
.$username.".
        <br />
        Your password is "
.$password.".
        <br />
        Please note that your pick up day is "
.$day.".
        <p><a href='login.php'>Click here to Login</a></p>
       </div>"
;
 }
else
{
 echo 
'<div id="form_container">
        <form method="POST" action="#">
         <table>
          <tr>
           <td colspan="2" align="center">'
.$error_msg10.'</td>
          </tr>
          <tr>
           <td width="318px">
            <div id="_Username" class="label">Username:</div>
            <div id="UsernameInput" class="inputdiv"><input type="text" name="username" value="'
.$username.'" size="26" /></div>
           </td>
           <td>'
.$error_msg1.'</td>
          </tr>
          <tr>
           <td>
            <div id="_Password" class="label">Password:</div>
            <div id="PasswordInput" class="inputdiv"><input type="password" name="password" value="'
.$password.'" size="26" /></div>
           </td>
           <td>'
.$error_msg2.'</td>
          </tr>
          <tr>
           <td>
            <div id="_E-Mail" class="label">E-Mail:</div>
            <div id="E-MailInput" class="inputdiv"><input name="email" type="text" value="'
.$email.'" size="26" /></div>
           </td>
           <td>'
.$error_msg3.'</td>
          </tr>
          <tr>
           <td>
            <div id="_FirstName" class="label">First Name:</div>
            <div id="FirstNameInput" class="inputdiv"><input type="text" name="firstname" value="'
.$firstname.'" size="26" /></div>
           </td>
           <td>'
.$error_msg4.'</td>
          </tr>
          <tr>
           <td>
            <div id="_LastName" class="label">Last Name:</div>
            <div id="LastNameInput" class="inputdiv"><input type="text" name="lastname" value="'
.$lastname.'" size="26" /></div>
           </td>
           <td>'
.$error_msg5.'</td>
          </tr>
          <tr>
           <td colspan="2">&nbsp</td>
          </tr>
          <tr>
           <td>
            <div id="_Streetnumber" class="label">Street number:</div>
            <div id="StreetnumberInput" class="inputdiv"><input name="streetnumber" type="text" value="'
.$streetnumber.'" size="11" /></div>
           </td>
           <td>'
.$error_msg6.'</td>
          </tr>
          <tr>
           <td>
            <div id="_Streetname" class="label">Street name:</div>
            <div id="StreetnameSelect" class="inputdiv">
             <select name="streetname">
              <option value="">Please choose your street</option>'
;
              
$list=mysql_query("SELECT * FROM street_day_list");
              
// Show records by id.
              
while($row_list=mysql_fetch_assoc($list))
              {
               
$day $row_list['day'];
               
$selected $row_list['streetname']==$streetname?"SELECTED":"";
               echo 
"<option value='".$row_list['streetname'].",".$row_list['day']."' $selected>".$row_list['streetname']."</option>";
               }
       echo 
'</select>
            </div>
           </td>
           <td>'
.$error_msg7.'</td>
          </tr>
          <tr>
           <td>
            <div id="_PhoneNumber" class="label">Phone Number:</div>
            <div id="PhoneNumberInput" class="inputdiv"><input type="text" name="phonenumber" value="'
.$phonenumber.'" size="26" /></div>
           </td>
           <td>'
.$error_msg8.'</td>
          </tr>
          <tr>
           <td>
            <div id="_OtherNumber" class="label">Other Number:</div>
            <div id="OtherNumberInput" class="inputdiv"><input type="text" name="othernumber" value="'
.$othernumber.'" size="26" /></div>
           </td>
           <td>'
.$error_msg9.'</td>
          </tr>
          <tr>
           <td>
            <div class="label"></div>
            <div id="_submit" class="submitdiv"><input type="submit" name="submit" value="Submit" /></div>
           </td>
           <td>&nbsp;</td>
          </tr>
         </table>
        </form>
        <p>Please fill out the form to register or <a href="login.php">Click here to Login</a></p>
       </div>'
;
 }
?>
<div id="footer">Waste Pro </div>
</body>
</html>
__________________
Jerry Broughton
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Auto Dealer Script nuzzle PHP 17 04-14-10 08:34 PM
retrieve information from MYSQL through drop down list minglou PHP 3 04-03-09 09:43 PM
Why won't select list item insert into database? gavacho PHP 2 12-05-08 06:38 PM
MYSQL database countll Database 2 06-19-07 04:20 PM
Drop down list change PHP ancdy PHP 5 02-10-05 05:33 AM


All times are GMT -5. The time now is 08:24 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.