naihr Posted May 15, 2012 Share Posted May 15, 2012 Hi? im just a beginner in php i just want to ask how to insert a data into a table from a dropdown list. I have concatenate the itemid and description to form the dropdown list. But when i viewed my item_table the itemid and description columns are null. can you help me with this.. this is my php code for the dropdown list... <?php $query = "SELECT CONCAT(itemid,' ', '-',' ', description) AS Item FROM item_table"; $result = mysql_query($query) or die(mysql_error()); $dropdown = "<SELECT CONCAT(itemid,' ' '-',' ', description) AS Item FROM item_table>"; while($row = mysql_fetch_assoc($result)) { $dropdown .= "\r\n<option value='{$row['Item']}'>{$row['Item']}</option>"; } $dropdown .= "\r\n</select>"; echo $dropdown; ?> this my code for inserting data into the item_table... <?php if(isset($_POST ['submit'])) { $itemid = $_POST['itemid']; $description = $_POST['description']; $datein = $_POST['datein']; $qtyin = $_POST['qtyin']; $unitprice = $_POST['unitprice']; $unit = $_POST['unit']; $category = $_POST['category']; $empid = $_POST['empid']; $message =''; if(($itemid && $description == "")||($itemid && $description == null)) { header("location:IncomingEntry.php?msg=Incorrect"); exit(); } else { $link = mysql_connect('localhost', 'root', '') or die(mysql_error()); $db_selected = mysql_select_db('inventory', $link); $message=''; $query = "INSERT INTO incoming_table (itemid , description, datein, qtyin, unitprice, unit, category, empid) VALUES ('".$itemid."', '".$description."', '".$datein."', '".$qtyin."', '".$unitprice."', '".$unit."', '".$category."', '".$empid."')"; if (!mysql_query($query,$link)) { die('Error: ' . mysql_error()); } header("location: IncomingEntry.php?msg=1 record added"); } } ?> :'( Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 15, 2012 Share Posted May 15, 2012 It seems you have the concepts of "SELECT" of a query and for a form input confused. This makes no sense: $dropdown = "<SELECT CONCAT(itemid,' ' '-',' ', description) AS Item FROM item_table>"; A select list in HTML should look something like this <select name="field_name"> <option value="0">First Option</option> <option value="1">Second Option</option> <option value="2">Third Option</option> </select> Also, you would not typically concatenate the id and description. You would use one for the value of the options and one as the label for the options. Here is an example of the code I would use to build the select input field $query = "SELECT itemid, description FROM item_table"; $result = mysql_query($query) or die(mysql_error()); $item_options = ''; while($row = mysql_fetch_assoc($result)) { $item_options .= "<option value='{$row['itemid']}'>{$row['description']}</option>\n"; } ?> <select name="itemid"> <?php echo $item_options; ?> </select> In your processing page you are trying to retrieve values for both an itemid field and a description field. You only need the itemid - unless the description is for something other than the item. Quote Link to comment Share on other sites More sharing options...
naihr Posted May 15, 2012 Author Share Posted May 15, 2012 thank you for your help... but we really have to concatenate the itemid and description because it is the desire output of the end user.... pls help... the first image is the dropdown list i made.. the second image is the viewing area which contains null value on the itemid and description field... (note: the values that i concatenate is from the item_table and i used this values for the entry form of the incoming_table and will be saved to the incoming_table),,....... Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 15, 2012 Share Posted May 15, 2012 If you have a table that stores item IDs and their corresponding descriptions then you should ONLY store the item ID as a foreign key in any associated tables. Go ahead and make the "labels" of the select options the concatenated value of the ID and the description, but the "values" should still only be the ID. And, you should ONLY be storing the ID in the incoming_table. That is how a relational database works. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.