Jump to content

Recommended Posts

Hi php people!

I'm very new to php and am working on a web application which manages data and information about a ready-made apparel company.

I have a few forms to enter data into a mySql database. Following the procedural coding style (as I'm a beginner), I have after a great struggle,

been able to even dynamically generate drop-down menus from database records. I have been struggling with a problem for over a week now

and have searched many posts and have not been able to solve my problem which seems very simple- but it beats me!

 

My problem is that I am able to retrieve records from the database to populate a single recordset for edit / update BUT I DO NOT GET THE DROP-DOWN

MENU SELECTED (with the record of the existing data from the database). FOR GOD' SAKE CAN ANYONE PLEASE HELP ME AND TELL ME WHAT I'M DOING WRONG? I'M GOING CRAZY!!!!! I NEED A MENTOR!!!!.....A GURU!!!!!! PLEASE!!!!!!!!

 

I created a simple search through a drop down menu listing the types of apparel.

I have two tables (I have blanked out irrelevant fields in the 'product' table for the sake of clarity) in the database relevant to my problem and they are as follows.

 

mysql> describe apparel_type;

+--------------+----------------------+------+-----+---------+----------------+

| Field        | Type                | Null | Key | Default | Extra          |

+--------------+----------------------+------+-----+---------+----------------+

| apparel_type_id  | smallint(5) unsigned | NO  | PRI | NULL    | auto_increment |

| apparel_type_name | varchar(50)          | NO  |    |        |                |

+--------------+----------------------+------+-----+---------+----------------+

2 rows in set (0.01 sec)

 

mysql> describe product;

+----------------+-----------------------+------+-----+---------+---------------

-+

| Field          | Type                  | Null | Key | Default | Extra

|

+----------------+-----------------------+------+-----+---------+---------------

-+

| product_id          | int(50)              | NO  | PRI | NULL    | auto_increment

|

| xxxxxxxx| varchar(255)          | YES  |    | NULL    |

|

| xxxxxxxx| date                  | YES  |    | NULL    |

|

| consignment_no  | varchar(255)          | NO  |    |        |

|

|apparel_type        | char(25)              | NO  |    |        |

|

|  xxxxxxxxxx        |char(25)              | NO  |    |        |

|

| xxxxxxxxxx| char(25)              | NO  |    |        |

|

| xxxxxxxx| char(25)              | NO  |    |        |

|

| xxxxxxxx| decimal(6,2)          | NO  |    |        |

|

| xxxxxxxx| smallint(10) unsigned | NO  |    |        |

|

| xxxxxxxxxx| decimal(3,1)          | YES  |    | NULL    |

|

+----------------+-----------------------+------+-----+---------+---------------

-+

11 rows in set (0.02 sec)

 

<?php session_start();

 

$id=$_GET['product_id'];

 

// Make a MySQL Connection

mysql_connect("localhost", "username", "password") or die(mysql_error());

mysql_select_db("db") or die(mysql_error());

 

 

if (isset($_GET['product_id'])) {

 

$query = "SELECT * FROM ls WHERE product_id = '". $product_id ."' ";

 

$result = mysql_query($query)

  or die (mysql_error());

$num = mysql_num_rows($result);

 

// I've blanked out irrelevant data of different fields.

$i=0;

while ($i < $num) {

$consignment_no=mysql_result($result,$i,"consignment_no");

$apparel_type=mysql_result($result,$i,"apparel_type");

$xxxxx=mysql_result($result,$i,"xxxxx");

$xxxxx=mysql_result($result,$i,"xxxxx");

$xxxxx=mysql_result($result,$i,"xxxxx");

$xxxxx=mysql_result($result,$i,"xxxxx");

$xxxxx=mysql_result($result,$i,"xxxxx");

$xxxxx=mysql_result($result,$i,"xxxxx");

 

++$i;

}

}

$arrApparel = array();

$arrProducts = array();

 

$result = mysql_query('SELECT * FROM apparel_type ORDER BY apparel_type ASC');

if (mysql_num_rows($result) > 0) {

while ($myRow = mysql_fetch_array($result)) {

 

array_push($arrApparel , $myRow);

}

mysql_free_result($result);

}

 

 

if (isset($_GET['consignment_no'])) {

$result = mysql_query('SELECT * FROM product WHERE consignment_no = "' . mysql_real_escape_string(urldecode($_GET['consignment_no'])) . '"');

if (mysql_num_rows($result) > 0) {

while ($myRow = mysql_fetch_array($result)) {

$consignment_no = $myRow['consignment_no'];

$apparel_type = $myRow['apparel_type'];

$xxxxx= $myRow['xxxxx'];

$xxxxx= $myRow['xxxxx'];

$xxxxx= $myRow['xxxxx'];

$xxxxx= $myRow['xxxxx'];

$xxxxx= $myRow['xxxxx'];

$xxxxx= $myRow['xxxxx'];

 

$result1 = mysql_query('SELECT * FROM product, apparel_type WHERE apparel_type.apparel_type = product.apparel_type AND consignment_no = "' . $myRow['consignment_no'] . '" ORDER BY apparel_type.apparel_type');

if (mysql_num_rows($result1) > 0) {

while ($myRow1 = mysql_fetch_array($result1)) {

array_push($arrProducts, $myRow1);

}

mysql_free_result($result1);

}

}

mysql_free_result($result);

}

        }

?>

<form id="update" name="update" method="post" action="update_records.php">

<input type="hidden" name="product_id" value="<? echo $product_id; ?>">

  <table width="391" border="1" align="center" cellpadding="1" cellspacing="1" bordercolor="#333333" bgcolor="#CFB8C0">

    <tr>

      <td width="232"><span class="style17">Consignment No.</span></td>

      <td width="146">

        <div align="left">

          <input name="consignment_no " type="text" id="consignment_no " value="<? echo $consignment_no ; ?>" />

        </div></td>

    </tr>

 

<select name="apparel_name" id="apparel_name">

<option value=""></option>

<?php

foreach ($arrApparel as $item) {

?>

<option value="<?php echo $item['apparel_id']; ?>"<?php if ($_POST['apparel_id'] == $item['apparel_id']) echo "selected"; ?>><?php echo $item['apparel_name']; ?></option>

<?php

}

?>

      </select>

</form>

</body>

</html>

Link to comment
https://forums.phpfreaks.com/topic/88240-solved-i-do-not-get-the-drop-down/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.