9 Replies - 1882 Views - Last Post: 25 May 2010 - 12:54 AM Rate Topic: -----

#1 Lydon  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 87
  • Joined: 22-May 08

Dropdown list not populating database

Posted 24 May 2010 - 04:59 AM

Hi all, I'm hoping some kind soul can point me in the right direction with this one.

I've got a mysql DB set up, and am pulling one of the fields in tableA to populate a form, which when submitted, will then be added to tableB.

So far I've got the dropdown list on the form looking good, and it is pulling all the correct data from tableA just as it should do.
Problem is, when I'm submitting the form, nothing from the dropdown list is getting entered in tableB.
All other data from the form is fine, just this dropdown.

Here's the code:

<?php
	require ('db_connection.php');
		
	// Create Query
	$query =	"SELECT company FROM add_client
				ORDER BY company";
			
	// Execute query 
	$result = mysql_query($query) or die ("Error in query");
	
	$options="";
				
	while ($row=mysql_fetch_array($result))
	{ 
		$id=$row["id"]; 
		$company=$row["company"]; 
		$options.="<OPTION VALUE=\"$id\">".$company.'</option>'; 
	} 
					
	// free result set memory 
	mysql_free_result($result); 
	
	// close connection 
	mysql_close($connection);
?>



Then later on in the page, in the form, this is how I'm displaying the list:
<p class="company"> <!-- Pull list of companies from database and display in drop down list --> 
                <SELECT NAME="company" id="company"> 
					<OPTION VALUE=<?php $options; ?>>Choose... <?php echo $options; ?> 
				</SELECT>  
                <label for="company">Company</label>  
            </p> 



Anyone any ideas what I'm doing wrong here?

Cheers

Is This A Good Question/Topic? 0
  • +

Replies To: Dropdown list not populating database

#2 moopet  Icon User is offline

  • binary decision maker
  • member icon

Reputation: 339
  • View blog
  • Posts: 1,185
  • Joined: 02-April 09

Re: Dropdown list not populating database

Posted 24 May 2010 - 05:31 AM

Yeah.
You're compiling a string full of option tags and then setting the value of your only option tag to that string.
Actually you're not even doing that because you're not echoing the string to the browser at all.
<SELECT NAME="company" id="company"> 
                                        <OPTION VALUE=<?php $options; ?>>Choose... <?php echo $options; ?> 
                                </SELECT>  


should be
<SELECT NAME="company" id="company"> 
                                        <?php echo $options; ?>
                                </SELECT>  



No idea if anything else is b0rked, but that's what you're doing wrong with the options string.
Was This Post Helpful? 0
  • +
  • -

#3 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 144
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Re: Dropdown list not populating database

Posted 24 May 2010 - 05:36 AM

As you have said everything is working rather than data is not inserting into the tableB, then you need to check the processing script of form which is holding drop-down box. First make sure you are getting data from this form by checking the value of $_POST["company"] in processing script. If you get the desired result, then you need to check the code responsible for inserting data into tableB.
Was This Post Helpful? 0
  • +
  • -

#4 Lydon  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 87
  • Joined: 22-May 08

Re: Dropdown list not populating database

Posted 24 May 2010 - 05:44 AM

OK, cheers guys.
1. moopet, when I change to what you said, there was nothing at all being displayed in thh dropdown, so that can't be right. Unless I'm wrong somewhere else.

2. atik97, good thinking! I echo'd out the variable which is assigned to $_POST["company"] to see what was being held, and there is nothing. Guess that's where I gotta be looking
Was This Post Helpful? 0
  • +
  • -

#5 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 144
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Re: Dropdown list not populating database

Posted 24 May 2010 - 05:56 AM

You are getting the options from the variable $options, isn't it? So why are you using <option> tag in the form? Try with this in the form-

<select name="company" id="company">
<?php echo $options; ?>
</select> 



Now try echo $_POST["company"] in processing script.

This post has been edited by atik97: 24 May 2010 - 05:57 AM

Was This Post Helpful? 0
  • +
  • -

#6 Lydon  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 87
  • Joined: 22-May 08

Re: Dropdown list not populating database

Posted 24 May 2010 - 06:01 AM

Nope, echo returns nothing at all again :( , but at least this time I can see options in the dropdown list .
Was This Post Helpful? 0
  • +
  • -

#7 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 144
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Re: Dropdown list not populating database

Posted 24 May 2010 - 06:15 AM

Are you getting correct options in you form? Then there is no reason to pass the data to the processing script unless there is some mistake in variable name or you are not using appropriate method.
Was This Post Helpful? 0
  • +
  • -

#8 Lydon  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 87
  • Joined: 22-May 08

Re: Dropdown list not populating database

Posted 24 May 2010 - 10:18 AM

I can't see where I'm going wrong with this, so here is the entire two files, containing the form itself, and the action page once the form is submitted.
Might be able to see what I've done wrong easier with the whole thing, rather than just the section I posted earlier.

*NOTE*
Where the following has been suggested...
<select name="company" id="company">
<?php echo $options; ?>
</select>


...I've already done this, and still the DB was not being populated with whatever option was chosen from the dropdown.


Here is the page which holds the form (add_new_invoice):
<?php
	require ('db_connection.php');
		
	// Create Query
	$query =	"SELECT company FROM add_client
				ORDER BY company";
			
	// Execute query 
	$result = mysql_query($query) or die ("Error in query");
	
	$options="";
				
	while ($row=mysql_fetch_array($result))
	{ 
		$id=$row["id"]; 
		$company=$row["company"]; 
		$options.="<OPTION VALUE=\"$id\">".$company.'</option>'; 
	} 
					
	// free result set memory 
	mysql_free_result($result); 
	
	// close connection 
	mysql_close($connection);
?>
<?php include 'header.php'; ?>

<body>
<div id="container">    
	  
      
      
    <div id="main_navigation">  
        <?php include 'main_navigation.php'; ?>
    </div><!-- ********** END NAVIGATION ********** -->
        
      
      
    <div id="main_content">  
        <form class="form" method="post" action="add_new_invoice_action.php">  
  
            <p class="company"> <!-- Pull list of companies from database and display in drop down list --> 
                <SELECT NAME="company" id="company"> 
					<?php echo $options; ?> 
				</SELECT>  
                <label for="company">Company</label>  
            </p>  
          
            <p class="project_start">  
                <input type="text" name="project_start" id="project_start" value="<?php echo date('Y-m-d'); ?>" />  
                <label for="project_start">Project Start</label>  
            </p>  
            
            <p class="project_handover">  
                <input type="text" name="project_handover" id="project_handover" value="<?php echo date('Y-m-d'); ?>" />  
                <label for="project_handover">Project Handover</label>  
            </p>
            
            <p class="amount_payable">  
                <input type="text" name="amount_payable" id="amount_payable" />  
                <label for="amount_payable">Amount Payable</label>  
            </p>
            
            <p class="date_invoiced">  
                <input type="text" name="date_invoiced" id="date_invoiced" value="<?php echo date('Y-m-d'); ?>" />  
                <label for="date_invoiced">Date Invoiced</label>  
            </p>
            
            <p class="date_due">  
                <input type="text" name="date_due" id="date_due" value="<?php echo date("Y-m-d", strtotime("+1 months")); ?>" />  
                <label for="date_due">Date Due</label>  
            </p>
          
            <p class="text">  
                <textarea name="text"></textarea>  
            </p>  
          
            <p class="submit">  
                <input type="submit" value="Create New Invoice" />  
            </p>  
  
		</form>
    </div><!-- ********** END MAIN CONTENT ********** -->  
      
      
       
    <div id="footer">  
    	<hr />
        
        <div id="footer_information">
    		<?php include 'footer.php'; ?>
    	</div>
        
    </div><!-- ********** END FOOTER ********** -->    
</div><!-- ********** END CONTAINER ********** -->
</body>
</html>



And here is the
action="add_new_invoice_action.php"
page.
<?php
	require 'db_connection.php';

	$company = trim(stripslashes(htmlspecialchars($_POST['company'])));
	$project_start = trim(stripslashes(htmlspecialchars($_POST['project_start'])));
	$project_handover = trim(stripslashes(htmlspecialchars($_POST['project_handover'])));
	$amount_payable = trim(stripslashes(htmlspecialchars($_POST['amount_payable'])));
	$date_invoiced = trim(stripslashes(htmlspecialchars($_POST['date_invoiced'])));
	$date_due = trim(stripslashes(htmlspecialchars($_POST['date_due'])));
	$text = trim(stripslashes(htmlspecialchars($_POST['text'])));

	
	// Create Query
	$query = "INSERT INTO add_invoice (company, project_start, project_handover, amount_payable, date_invoiced, date_due, text)
			  VALUES ('$company', '$project_start', '$project_handover', '$amount_payable', '$date_invoiced', '$date_due', '$text')";
			  
	// Run query through connection
	$result = mysql_query ($query, $connection);
	
	// Print message with ID of inserted record
	header("Location: view_invoices.php");
	
	// Close Connection
	mysql_close($connection);
?>



With any luck, this might be a bit clearer as to where I'm going wrong with this.
Every other field is being added to the DB correctly, just the dropdown containing 'company' is nto working.
Cheers :)
Was This Post Helpful? 0
  • +
  • -

#9 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 144
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Re: Dropdown list not populating database

Posted 24 May 2010 - 06:17 PM

The problem is in the code-
// Create Query
        $query =        "SELECT company FROM add_client
                                ORDER BY company";



Look, you are only pulling the company name from the table, thus
 while ($row=mysql_fetch_array($result))
        { 
                $id=$row["id"]; 
                $company=$row["company"]; 
                $options.="<OPTION VALUE=\"$id\">".$company.'</option>'; 
        }



in the while loop $row["id"] is generating no value. So the value attribute of <option> tag is being set at blank and thus you are getting no value in the processing script of $_POST['company'].

So make change to your select statement-
// Create Query
        $query =        "SELECT id,company FROM add_client
                                ORDER BY company";


Was This Post Helpful? 1
  • +
  • -

#10 Lydon  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 87
  • Joined: 22-May 08

Re: Dropdown list not populating database

Posted 25 May 2010 - 12:54 AM

Ahhh, now that was certainly a start! By changing it to what you suggested, the DB was being populated by the id number, NOT the company name.

After taking a closer look at the code (for the millionth time), I changed it to this, which now works perfectly:
while ($row=mysql_fetch_array($result))
	{  
		$company=$row["company"]; 
		$options.="<OPTION VALUE=\"$company\">".$company.'</option>'; 
	}



I didn't even need $id anywhere.

Thanks SO much for your help. That was driving me nuts!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1