Inserting drop down list items into mysql database.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mani123
Forum Newbie
Posts: 8
Joined: Thu Mar 03, 2011 6:48 pm

Inserting drop down list items into mysql database.

Post by mani123 »

I have a drop down list that I have populated from DB.

Code: Select all

<?php 
session_start(); 
$d= $_SESSION['txt_id'];
include ('dbinfo.inc');
echo mysql_error();
?> 
 

<body  bgcolor="#cfad5f"text="#1f180a">

<form action="req_logic.php" method="post">

</br></br></br>

   <p height="10">
   <font face="times new roman" size="5"><b><big></font><center>
     <font color="#532900" size="5" face="times new roman">Request Form</font>
   </center>
   </P>

<table align = "center" style="text-align:Left" width = "600" frame = "box" bordercolor="black" >
  <tr>
       <td width="252" font size="3"><font color="#532900" size="5">Department:</font></td>
       <td width="336"><div align="center">
         <p>
           <label>


          <?php 		 
		 $query="SELECT emp_department FROM login";
/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */
           $result = mysql_query ($query);
           echo "<select name='menu_dept'>";
// printing the list box select command
           while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
            echo "<option value=$nt[emp_department]>$nt[emp_department]</option>";
/* Option values are added by looping through the array */
}
             echo "</select>";// Closing of list box		 
?>
agter taking these values from my database table I want to reinsert these into another table.

Code: Select all

$dept=mysql_real_escape_string($_POST['menu_dept']);
$dept=mysql_real_escape_string($_POST['menu_dept'][$row]);
mysql_query("INSERT INTO request(,req_for_dept,) Values ('$dept')");
but the code gives an error as"Undefined variable $dept on line 17"
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: Inserting drop down list items into mysql database.

Post by Weiry »

If you are getting an "Undefined" error, it usually means that the variable you tried to access was never initialized.
This is usually caused by:
1. Not defining the variable before accessing it.
2. The variable was defined, but no value was ever assigned to it.

So in your case, without knowing exactly what you want to get out of your select fields, whether your inserting a single value or multiple values from your select box, leads me to where you are trying to access the data.

Code: Select all

$dept=mysql_real_escape_string($_POST['menu_dept']);
$dept=mysql_real_escape_string($_POST['menu_dept'][$row]);
mysql_query("INSERT INTO request(,req_for_dept,) Values ('$dept')");
First thing i would do is confirm that you actually are receiving output from the form, then i would format your $dept declarations into some sort of an if/else statement if required.
Currently this section of code makes no sense with what you are trying to accomplish because full code was not provided.

Code: Select all

$dept=mysql_real_escape_string($_POST['menu_dept'][$row]);
So what i have done, is added a little debugging to your current script.

Code: Select all

<?php
    print "<pre>";print_r($_POST);print "</pre>";
    
    if(isset($_POST['menu_dept'])){
        $dept = mysql_real_escape_string($_POST['menu_dept']);
    }else{
        die("Post values not detected.");
        exit;
    }
    $q = "INSERT INTO `request` (`req_for_dept`) VALUES ('$dept')";
    if(mysql_query($q)){
        print "Query Success";
    }else{
        print "Query Error: ".mysql_error();
    }
?>
This should give you an indications where your problem lies.
mani123
Forum Newbie
Posts: 8
Joined: Thu Mar 03, 2011 6:48 pm

Re: Inserting drop down list items into mysql database.

Post by mani123 »

Thanks alot for your reply.
I am inserting

Code: Select all

<?php 
session_start();

$d= $_SESSION['txt_id'];
include ('dbinfo.inc');


           //**********************Assigning the login id's level to $level*******************************//
$query=mysql_query("SELECT * FROM `login` WHERE `login_id` LIKE '$d'");
if(mysql_num_rows($query) >0)
{while ($far=mysql_fetch_array($query))
{$level=$far[4];}}


          //*************************Assign each array to a variable************************************//
		  
foreach($_POST['Request'] as $row=>$req)
{
$dept=mysql_real_escape_string($_POST['menu_dept']);
//$cat=mysql_real_escape_string($_POST['menu_cat'])or DIE('kindly select Item Category');
$itmname=mysql_real_escape_string($_POST['txt_itmname'])or DIE('Item name is missing');
$quantity=mysql_real_escape_string($_POST['txt_quantty']);
$des=mysql_real_escape_string($_POST['txt_Des']);
$status=mysql_real_escape_string($_POST['menu_status']);
$delivery=mysql_real_escape_string($_POST['delivery']);
}

        //****************************enter rows into database*****************************************//

foreach($_POST['Request'] as $row=>$req)
{
$dept=mysql_real_escape_string($_POST['menu_dept'][$row]);
//$cat=mysql_real_escape_string($_POST['txt_cat'][$row])or DIE('kindly select Item Category');
$itmname=mysql_real_escape_string($_POST['txt_itmname'][$row])or DIE('Item name is missing');
$quantity=mysql_real_escape_string($_POST['txt_quantty'][$row]);
$des=mysql_real_escape_string($_POST['txt_Des'][$row]);
//$status=mysql_real_escape_string($_POST['menu_status'][$row]);
$delivery=mysql_real_escape_string($_POST['delivery'][$row]);

}
mysql_query("INSERT INTO request(emp_id,req_for_dept,cat,item_name,quantity,description, level, Delivery) Values ('$d','$dept','$itmname','$quantty','$des','$level','$delvery')");
				
        $_SESSION['txt_id'] = $_POST['txt_id'];		
   
        header('Location: sucful.php');
?>
</body>
</html>
and yes the form is getting the values from the database.
the form is

Code: Select all

   <html>
   <head>
   <title>Assets Management System</title>
</head>
<?php 
session_start(); 
$d= $_SESSION['txt_id'];
include ('dbinfo.inc');
echo mysql_error();
?> 
 
<body  bgcolor="#cfad5f"text="#1f180a">

<form action="req_logic.php" method="post">

</br></br></br>

   <p height="10">
   <font face="times new roman" size="5"><b><big></font><center>
     <font color="#532900" size="5" face="times new roman">Request Form</font>
   </center>
   </P>







<table align = "center" style="text-align:Left" width = "600" frame = "box" bordercolor="black" >
  <tr>
       <td width="252" font size="3"><font color="#532900" size="5">Department:</font></td>
       <td width="336"><div align="center">
         <p>
           <label>
          <?php 		 
		 $query="SELECT emp_department FROM login";
/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */
           $result = mysql_query ($query);
           echo "<select name='menu_dept'>";
// printing the list box select command
           while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
            echo "<option value=$nt[emp_department]>$nt[emp_department]</option>";
/* Option values are added by looping through the array */
}
             echo "</select>";// Closing of list box		 
?>
           </label>
         </p>
       </div></td>
     </tr>
     <tr>
       <td height="29" font size="3"><font color="#532900" size="5">Date:</font></td>
       <td bordercolor="#663300"><div align="center">
          <font color="#663300" size="4">
          <!-- #BeginDate format:En2 -->05-Mar-2011<!-- #EndDate -->
       </font></div></td>
    </tr>
     <tr>
       <td font size="3"><font color="#532900" size="5">Delivery</font></div></td>
       <td><div align="center"><font color="#532900" size="5"> Normal:
         <input type="radio" checked="checked"
   name="delivery" value="Normal">
               <br>
         Immediate:
  <input type="radio"
   name="delivery" value="Immediate">
       </font> </div></td>
     </tr>
     <tr>
       <td font size="3"><font color="#532900" size="5">Description</font></td>
       <td><div align="center"><textarea name="txt_rep" id="txt_rep" cols="45" rows="5"></textarea>&nbsp;</td>
     </tr>
  </table>


 


<p>&nbsp;</p>
<table align = "center" style="text-align:Left" width = "601" frame = "box" bordercolor="black" >

<tr>
     
       <td width="150" font size="3"><font color="#532900" size="5">Item number</font></td>
       
      <td width="83"><font color="#532900" size="5">Item category</font></td>
       <td width="240"><div align="center"><font color="#532900" size="5">Item name</font></div></td>
       <td width="100"><div align="center"><font color="#532900" size="5">Quantity</font></div></td>
    </tr>
    
     <tr>
     
       <td width="150" font size="3"><font color="#532900" size="5">Item 1 name:</font></td>
       <td width="83"><?php $query="SELECT item_cat FROM category";

/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */

$result = mysql_query ($query);
echo "<select name='menu_cat'></option>";
// printing the list box select command

while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[item_cat]>$nt[item_cat]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box		 
?>&nbsp;</td>
       <td width="240"><div align="center"><font color="#532900" size="5">
         <input type="text" name="txt_itmname2" size="30"MAXLENGTH="25">
       </font></div></td>
       <td width="100"><font color="#532900" size="5">
       <div align="center"><font color="#532900" size="5">
         <input name="txt_q1" type="text" id="txt_q1" size="5"MAXLENGTH="5">
       </font></td>
     </tr>
     <tr>
       <td font size="3"><font color="#532900" size="5">Item 2 name:</font></td>
       <td><?php $query="SELECT item_cat FROM category";


$result = mysql_query ($query);
echo "<select name='menu_cat'></option>";

while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[item_cat]>$nt[item_cat]</option>";
}
echo "</select>";	 
?>&nbsp;</td>
       <td><div align="center"><font color="#532900" size="5">
           <input type="text" name="txt_itmname" size="30"MAXLENGTH="25">
       </font></div></td>
       <td><font color="#532900" size="5"><div align="center"><font color="#532900" size="5">
        <input name="txt_q2" type="text" id="txt_q2" size="5"MAXLENGTH="5">
       </font></td>
     </tr>
     <tr>
       <td font size="3"><font color="#532900" size="5">Item 3 name:</font></td>
       <td><?php $query="SELECT item_cat FROM category";



$result = mysql_query ($query);
echo "<select name='menu_cat'></option>";


while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[item_cat]>$nt[item_cat]</option>";
}
echo "</select>"; 
?>&nbsp;</td>
       <td><div align="center"><font color="#532900" size="5">
           <input type="text" name="txt_itmname" size="30"MAXLENGTH="25">
       </font></div></td>
       <td><font color="#532900" size="5"><div align="center"><font color="#532900" size="5">
         <input name="txt_q3" type="text" id="txt_q3" size="5"MAXLENGTH="5">
       </font></td>
     </tr>
     <tr>
       <td font size="3"><font color="#532900" size="5">Item 4 name:</font></td>
       <td><?php $query="SELECT item_cat FROM category";


$result = mysql_query ($query);
echo "<select name='menu_cat'></option>";

while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[item_cat]>$nt[item_cat]</option>";
}
echo "</select>";	 
?>&nbsp;</td>
       <td><label><div align="center">
         <input name="txt_4" type="text" id="txt_4" size="30"MAXLENGTH="25">
       </label></td>
       <td><font color="#532900" size="5"><div align="center"><font color="#532900" size="5">
         <input name="txt_q4" type="text" id="txt_q4" size="5"MAXLENGTH="5">
       </font></td>
     </tr>
     <tr>
       <td height="29" font size="3"><font color="#532900" size="5">Item 5 name:</font></td>
       <td><?php $query="SELECT item_cat FROM category";


$result = mysql_query ($query);
echo "<select name='menu_cat'></option>";


while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[item_cat]>$nt[item_cat]</option>";

}
echo "</select>";		 
?>&nbsp;</td>
       <td><label><div align="center">
         <input name="txt_5" type="text" id="txt_5" size="30"MAXLENGTH="25">
       </label></td>
       <td><font color="#532900" size="5"><div align="center"><font color="#532900" size="5">
         <input name="txt_q5" type="text" id="txt_q5" size="5"MAXLENGTH="5">
       </font></td>
     </tr>
  </table>
  <p>&nbsp;</p>
     <label></label>
     <div align="center">
       <input type="submit" name="des" id="des" value="Submit">
       <input name="Reset" type="reset" value="Reset">
     </div>
  
 <br>
   <br>
   <br><br><br>
</form>

   </body>
</html>

and I dint understand when I am definfing the variable and assigning it a variable then why its not working.
Post Reply