0

I am trying to populate a select box from my database. The value chosen must be the id of the item, and the text shown the name
of the item so that once selected it will insert the id into the table column and not the name so that the relationships work.

Will the below do the job?

<?php
if(isset($_POST['add']))
{
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbbase = 'database';
$conn = mysql_connect($dbhost, $dbuser, $dbpass, $dbbase);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}

$sql = "SELECT company_id, company_name FROM company ORDER BY company_name DESC";
$retval = mysql_query( $sql, $conn );

$company_id = $_POST['company_id'];
$company_name = $_POST['company_name'];

}
else
{
?>
<form method="post" action="<?php $_PHP_SELF ?>">
<table width="100%" align="left" border="0" cellspacing="1" cellpadding="2">
<tr>
<select name='company'>
<option value="">--- Select ---</option>
<?php
while($row_list=mysql_fetch_assoc($retval)){
?>
<option name="'.$company_name.'" id="'.$company_id.'">'."\n";
</option>
<?php
// The rest of the form code goes in here.

</form>
<?php
}
?>

I need to then use the company_id value from the form to be saved into the database using my insert sql. but when I need to reference the selected value of the select box in my sql, how do I do that?

I am slowly, very slowly, getting there with this little project of mine. Might be up and running in about two decades or so!!

3
Contributors
18
Replies
72
Views
3 Years
Discussion Span
Last Post by joshuajames.delacruz
-1

Something is not right in your logic.

$company_id = $_POST['company_id'];
$company_name = $_POST['company_name'];

// What do you do with these post`s ?

You do not close your <select> form

A simple logic is this. Also try using PDO classes instead of the deprecated MySQL.

<?php

echo '<select name="yourName">';

while($arr = mysql_fetch_array($sql)) {
  echo '<option value="'.$arr['yourField1].'">'.$arr['yourField2'].'</option>';
}

echo '</select>';

?>
-1

ive added some code
here try this:

<form method="post" action="<?php $_PHP_SELF ?>">
<table width="100%" align="left" border="0" cellspacing="1"cellpadding="2">
<tr>
<select name='company'>
<option value="">--- Select ---</option>
<?php
while($get_data = $row_list=mysql_fetch_assoc($retval)){
?>
<option name="'.$company_name.'" id="'.$company_id.'">
<?php echo $get_data; ?>
</option>
<?php
// The rest of the form code goes in here.
</form>
<?php
}
?>

hope this helps

0

Hi guys,

Really battling here. Could you please debug this code? The problem is the select box area I think?

joshuajames.delacruz, I tried your code as well but just could not figure out how to get it working? I am missing something but for the life of me can't figure out what it might be......probably my old brain!!

<!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>Teaching Int Limited - Home</title>
    <meta name="title" content="Teaching Int Limited">
    <link rel="shortcut icon" href="home/images/favicon.ico">
    <meta http-equiv="Window-target" content="_top">
    <meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
    <meta http-equiv="Content-Language" content="en-gb">
    <meta name="MSSmartTagsPreventParsing" content="TRUE"><!--Providing Quality English Teachers to Schools and Organisations-->
    <meta name="description" content="Providing Quality English Teachers to Schools and Organisations">
    <meta name="Abstract" content="Providing Quality English Teachers to Schools and Organisations">
    <meta name="keywords" content="TEFL,english,teacher">
    <meta name="Robots" content="index, follow">
    <meta name="Distribution" content="Global">
    <meta name="Revisit-After" content="30 days">
    <meta name="Rating" content="General">
    <meta name="Reply-to" content="info@teaching-int.com">
    <meta name="Owner" content="Teaching Int Limited">
    <meta name="Author" content="Carter Langley">
    <meta name="copyright" content="Teaching Int Limited - 2013">
    <link href='http://fonts.googleapis.com/css?family=Great+Vibes' rel='stylesheet' type='text/css'>
    <link rel="stylesheet" type="text/css" href="form.css">
    <link rel="stylesheet" type="text/css" href="header.css">
    <link rel="stylesheet" type="text/css" href="site.css">
</head>
<body>
<?php include("header.php"); ?>
<?php
if(isset($_POST['add']))
{
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbbase = 'database';
$conn = mysql_connect($dbhost, $dbuser, $dbpass, $dbbase);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}

$campus_id = $_POST['campus_id'];
$company_id = $_POST['company_id'];
$campus_name = $_POST['campus_name'];
$address_street = $_POST['address_street'];
$address_city = $_POST['address_city'];
$address_province = $_POST['address_province'];
$address_postalcode = $_POST['address_postalcode'];
$address_country = $_POST['address_country'];
$main_tel = $_POST['main_tel'];
$second_tel = $_POST['second_tel'];
$main_email = $_POST['main_email'];
$second_email = $_POST['second_email'];

$sql = "INSERT INTO `teaching`.`campus` (`campus_id`, `company_id`,'campus_name','address_street','address_city','address_province','address_postalcode','address_country','main_tel','second_tel','main_email','second_email') 
VALUES ('NULL', '$campus_id', '$company_id','$campus_name','$address_street','$address_city','$address_province','$address_postalcode','$address_country','$main_tel','$second_tel','$main_email','$second_email')";
$retval = mysql_query( $sql, $conn );

$sql_selectcompany = "SELECT company_name FROM company ORDER BY company_name DESC";
$retval_selectcompany = mysql_query( $sql_selectcompany, $conn );

if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully. Redirecting in 2 seconds.\n";
mysql_close($conn);
header('Refresh: 2; URL=edit.php');
}
else
{
?>
<form method="post" action="<?php $_PHP_SELF ?>">
<table width="100%" align="left" border="0" cellspacing="1" cellpadding="2">
<tr>
<td><label for="company_id">Company ID</label></td>
<td>
<select name="company_name">
<option value="">--- Select a Company ---</option>
<?php
while($row = mysql_fetch_assoc($retval_selectcompany)) {
echo "<option name="$row["company_name"]";>" }
?>
</option>
</td>
</tr>
<tr>
<td><label for="campus_name">Campus Name</label></td>
<td><input name="campus_name" type="text" id="campus_name"></td>
</tr>
<tr>
<td><label for="address_street">Street Address</label></td>
<td><input name="address_street" type="text" id="address_street"></td>
</tr>
<tr>
<td><label for="address_city">City</label></td>
<td><input name="address_city" type="text" id="address_city"></td>
</tr>
<tr>
<td><label for="address_province">Province</label></td>
<td><input name="address_province" type="text" id="address_province"></td>
</tr>
<tr>
<td><label for="address_postalcode">Postal Code</label></td>
<td><input name="address_postalcode" type="text" id="address_postalcode"></td>
</tr>
<tr>
<td><label for="address_country">Country</label></td>
<td><input name="address_country" type="text" id="address_country"></td>
</tr>
<tr>
<td><label for="main_tel">Main Tel</label></td>
<td><input name="main_tel" type="text" id="main_tel"></td>
</tr>
<tr>
<td><label for="second_tel">Alt Tel</label></td>
<td><input name="second_tel" type="text" id="second_tel"></td>
</tr>
<tr>
<td><label for="main_email">Main Email</label></td>
<td><input name="main_email" type="text" id="main_email"></td>
</tr>
<tr>
<td><label for="second_email">Second Email</label></td>
<td><input name="second_email" type="text" id="second_email"></td>
</tr>
<tr>
<td></td>
<td>
<input name="add" type="submit" id="add" value="Add Campus">
</td>
</tr>
</table>
</form>
<?php
}
?>
0

i would like you to replace line 78 to 86 with this code

<td>
<select name="company_name">
<option value="">--- Select a Company ---</option>
<?php
while($row = mysql_fetch_assoc($retval_selectcompany)) {
echo "<option value=".$row['company_name'].">".$row['company_name']."</option>";
}
?>
</td>

that should do it.

please mark it solve if it really helped you.
you can also vote up the things that help you here.

Edited by joshuajames.delacruz: edited codes

0

Hi joshuajames.delacruz

Nope, nothing happening at all. I just get a blank screen when I load that page. Really baffled with this one. Probably something very very simple I am missing out on? Could it possibly be a database thing? Is there anyway to check if it is a database problem with error handling?

0

I am using Geany for Ubuntu 13.10 to do this. It doesn't provide a way to go step by step through the code. Is there a code debugger I can use for Ubuntu?

0

Put these two lines at the top of your file and tell us what errors does it give to you.

<?php

error_reporting(E_ALL);
ini_set("display_errors", 1);

?>

Again read my first post because I gave you the solution.

You are doing this wrong.

<select name="company_name">
<option value="">--- Select a Company ---</option>
<?php
while($row = mysql_fetch_assoc($retval_selectcompany)) {
echo "<option name="$row["company_name"]";>" }
?>
</option>

Wrong HTML usage and wrong PHP usage also.
I rewrote it to you in a correct way.

<select name="company_name">
<option value="">--- Select a Company ---</option>
<?php

while($row = mysql_fetch_assoc($retval_selectcompany)) {
    echo '<option value='.$row["company_name"].'>'.$row["company_name"].'</option>'; 
}

?>

</select>

Edited by Szabi Zsoldos

0

I put that code at the top of the page and I am still getting a blank page? Also copied your code and inserted it, but still blank page.

0

Now got to the stage that the page itself is displaying correctly. But the select box is not populating from the database. Getting there!!

0

This is what I have to populate the select box

<tr>
<td><label for="company_id">Company</label></td>
<td>
<select name="company_name">
<option value="">--- Select a Company ---</option>
<?php
$sql_select = "SELECT * FROM `company`";
$retval_selectcompany = mysql_query( $sql_select, $conn );
while($row = mysql_fetch_assoc($retval_selectcompany)) {
echo '<option value='.$row["company_name"].'>'.$row["company_name"].'</option>';
}
?>
</select>
</td>
</tr>

This should, in theory, do the job?

0

that should do the job.
but then again please check your database.
the fact that is has a very case sensitive basis

    1. do the company_name is really inside the database table?
    2. maybe you spelled the $row["company_name"] wrong?
    3. do you already have a data inside that particular table?
0

try this.
i know it is already correct but,
i'm still thinking why cant it populate the drop down

<tr>
<td><label for="company_id">Company</label></td>
<td>
<select name="company_name">
<option value="">--- Select a Company ---</option>
<?php
$sql_select = "SELECT * FROM `company`";
// i have removed the $conn
$retval_selectcompany = mysql_query($sql_select);
while($row = mysql_fetch_assoc($retval_selectcompany)) {
echo '<option value='.$row["company_name"].'>'.$row["company_name"].'</option>';
}
?>
</select>
</td>
</tr>
0

To everyone that has been helping me on this, I can only say a huge thankyou. This is now working!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.