dartfrogs Posted June 5, 2008 Share Posted June 5, 2008 I have looked on the posts and I see several post about this subject but none that have really helped. My problem is inserting the value from my dynamitc drop down. The dropdown menu works great. (See function below) But the post value is null. //category_lists function retrieves data from mysql function BuildCategory() { $category_lists = ' <select name="category_lists">'; $category_lists .= ('category_lists'); $q = "SELECT id, category FROM category"; $l = connectbg(); $rs = @mysql_query($q, $l); while ($mt = @mysql_fetch_array($rs)) { $category_lists.= "<option value=$mt[id]>$mt[category]</option>"; } disconnectbg($l); return $category_lists. ' </select>'; } When the person hits the submit button all of the other input boxes work great and things are entered into the database. This is the only one that will not work. //insert code function BuildInputForm() { //code before not shown...... //calls to functions to create page $category_lists ='Category List' .BuildCategory(); $countrylist = 'Country' . BuildCountry(); //code after not shown... } //insert function function Input() { $product_name=$_POST['product_name']; $catalog=$_POST['catalog_id']; $price=$_POST['price']; $description=$_POST['description']; $keywords=$_POST['keywords']; $imageName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $imageSize = $_FILES['userfile']['size']; $imageType = $_FILES['userfile']['filetype']; $filePath = $uploadDir . $imageName; $country = $_POST['countrylist']; $category_lists = $_POST['category_lists']; $result = move_uploaded_file($tmpName, $filePath); if (!$result) { echo "Error uploading file"; exit; } if(!get_magic_quotes_gpc()) { $imageName = addslashes($imageName); $filePath = addslashes($filePath); } $r = connectbg(); $sql="insert into inventory (category_id, country_id, product_name, catalog_id, price, description, keywords, image) values ('$category_lists','$country','$product_name', '$catalog', '$price', '$description','$keywords','$filePath')"; $res=mysql_query($sql, $r) or die (mysql_error()); disconnectbg(); } If anyone can see why I can't get the value of the category_lists drop down menu please let me know. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/108908-solved-insert-value-from-drop-down-into-mysql/ Share on other sites More sharing options...
hansford Posted June 5, 2008 Share Posted June 5, 2008 $category_lists = $_POST['category_lists'][0]; this will be what was selected Quote Link to comment https://forums.phpfreaks.com/topic/108908-solved-insert-value-from-drop-down-into-mysql/#findComment-558794 Share on other sites More sharing options...
Buddski Posted June 6, 2008 Share Posted June 6, 2008 $_POST['category_lists'] should be working fine adding a [0] will make no difference.. What you need to to is check what variables are being posted across put this at the top of the page that is getting the $_POST data; <?php echo "<pre>"; print_r($_POST); echo "</pre>"; ?> That will show you every $_POST variable being sent to the script..I am thinking that the $_POST['category_lists'] isnt being sent across.. Quote Link to comment https://forums.phpfreaks.com/topic/108908-solved-insert-value-from-drop-down-into-mysql/#findComment-558908 Share on other sites More sharing options...
hansford Posted June 6, 2008 Share Posted June 6, 2008 But it does make a difference. Consider a drop down list named 'year' $_POST['year'] will return --> Array if we selected from that dropdown list '2008' then $_POST['year'] will return --> Array and $_POST['year'][0] will return --> 2008 Quote Link to comment https://forums.phpfreaks.com/topic/108908-solved-insert-value-from-drop-down-into-mysql/#findComment-559083 Share on other sites More sharing options...
MatthewJ Posted June 6, 2008 Share Posted June 6, 2008 A select menu only returns one value... you may be confusing checkboxes or radio buttons. $_POST['year']; Will return the selected item from a dropdown called "year" $_POST['year'][0]; will also work, but is not needed. Quote Link to comment https://forums.phpfreaks.com/topic/108908-solved-insert-value-from-drop-down-into-mysql/#findComment-559098 Share on other sites More sharing options...
hansford Posted June 6, 2008 Share Posted June 6, 2008 I stand corrected. I tested it and I'll be D...! my appologies. Quote Link to comment https://forums.phpfreaks.com/topic/108908-solved-insert-value-from-drop-down-into-mysql/#findComment-559163 Share on other sites More sharing options...
dartfrogs Posted June 6, 2008 Author Share Posted June 6, 2008 Okay I made a couple of changes yet I am still not getting the value to pass through. At a complete loss here. I have posted all of the code I have to see if maybe someone can see where I am missing the passing of the value for catagory_lists. <?php include("lib/common.lib.php"); $pagetitle = "Battlegear Toys"; $uploadDir = "../upload/"; if (isset($_POST['submit'])) { Input(); echo BuildInputForm(); exit; } echo BuildInputForm(); exit; //functions function Getflags() { $flags = '<table>'; $r = connectbg(); $sql="SELECT country_name, flag_image FROM country"; $res=mysql_query($sql, $r); while ($resultset = @mysql_fetch_array($res)) { // $flags .= ($mt['flag_image']); $flags .= '<tr> <td>' . $resultset['country_name'] . '</td> <td ><img src="' .$resultset['flag_image'].'" /></td> </tr>'; $i += 1; } $flags .= '</table>'; disconnectbg($r); return $flags; } //works for showing info function BuildCategory() { $category_lists = ' <select name="category_lists">'; // $category_lists .= ('category_lists'); $q = "SELECT id, category FROM category"; $l = connectbg(); $rs = @mysql_query($q, $l); while ($mt = @mysql_fetch_array($rs)) { // $content .= '<option value="'.$mt[id].'">'.$mt['name'].' - '.$mt['description'].'</option>'; $category_lists.= '<option value="'.$mt[id].'">'.$mt['category'].'</option>'; } disconnectbg($l); return $category_lists. ' </select>'; } //works but no value posted either. Figured I would fix the first and then duplicate it to make this one work. function BuildCountry() { $countrylist = ' <select name="countrylist"> '; $ountrylist .= BuildOption('', 'countrylist'); $r = connectbg(); $sql="SELECT id, country_name FROM country"; $res=mysql_query($sql, $r); while ($mt = @mysql_fetch_array($res)) { $countrylist .= BuildOption($mt['country_name'], 'countrylist', $mt['id']); } disconnectbg($r); return $countrylist . ' </select>'; } function BuildInputForm() { $flags = Getflags(); if (isset($_POST['reset'])) { // user clicked the clear button, so blow off all the search criteria // and redraw the search page $_REQUEST['product_name'] = null; $_POST['catalog_id'] = null; $_POST['price']= null; $_POST['description'] = null; $_POST['keywords'] = null; $_POST['catagory_list'] = null; $_POST['countrylist'] = null; } $category_lists ='Category List' .BuildCategory(); $countrylist = 'Country' . BuildCountry(); $content .= '<form name="form" method="post" ENCTYPE="multipart/form-data" action="input.php"> <table> <tr> <td>Product Name: <input type="text" name="product_name"></td> </tr> <tr> <td>Catalog ID: <input type="text" name="catalog_id" ></td> </tr> <tr> <td>Price: $<input type="text" name="price" ></td> </tr> <tr> <td>Description: <input type="text" name="description" ></td> </tr> <tr> <td>In Stock: <input type="text" name="in_stock" ></td> </tr> <tr> <td>Keyword: <input type="text" name="keywords" ></td> </tr> <tr> <td > <input type="hidden" name="MAX_FILE_SIZE" value="2000000"> Upload image: <input type="file" name="userfile" id="userfile"> </td> </tr> <tr> <td> <input name="submit" value="submit" type="submit"> </td> <td> <input name="reset" value="reset" type="submit"> </td> </tr> </table> </form>'; return use_template("templates/page.html", array("#pagetitle#", "#content#", "#category_lists#", "#countrylist#" , "#flags#"), array($pagetitle, $content, $category_lists, $countrylist, $flags) ); } function Input() { $product_name=$_POST['product_name']; $catalog=$_POST['catalog_id']; $price=$_POST['price']; $description=$_POST['description']; $keywords=$_POST['keywords']; $imageName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $imageSize = $_FILES['userfile']['size']; $imageType = $_FILES['userfile']['filetype']; $filePath = $uploadDir . $imageName; $country = $_GET['countrylist']; $category_lists = $_POST['category_lists']; echo "<pre>"; print_r($_POST); echo "</pre>"; //$category = ; $result = move_uploaded_file($tmpName, $filePath); if (!$result) { echo "Error uploading file"; exit; } if(!get_magic_quotes_gpc()) { $imageName = addslashes($imageName); $filePath = addslashes($filePath); } $r = connectbg(); $sql="insert into inventory (category_id, country_id, product_name, catalog_id, price, description, keywords, image) values ('$category_lists','$country','$product_name', '$catalog', '$price', '$description','$keywords','$filePath')"; $res=mysql_query($sql, $r) or die (mysql_error()); disconnectbg(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/108908-solved-insert-value-from-drop-down-into-mysql/#findComment-559330 Share on other sites More sharing options...
KenDRhyD Posted June 6, 2008 Share Posted June 6, 2008 What does the rendered HTML for the displayed page(s) look like? Perhaps examining the actual HTML will give you a hint at something that is not quite right. Quote Link to comment https://forums.phpfreaks.com/topic/108908-solved-insert-value-from-drop-down-into-mysql/#findComment-559359 Share on other sites More sharing options...
hansford Posted June 6, 2008 Share Posted June 6, 2008 this function call - BuildCategory() returns a complete <select> dropdown no problems there. this function call - BuildInputForm() should echo out whatever BuildCategory() returned. We need to see the missing code for BuildInputForm() where it does the acutal echoing. Quote Link to comment https://forums.phpfreaks.com/topic/108908-solved-insert-value-from-drop-down-into-mysql/#findComment-559409 Share on other sites More sharing options...
hansford Posted June 6, 2008 Share Posted June 6, 2008 what appears to be happening is you're echoing out BuildCountry() and BuildCategory() outside of the form. Quote Link to comment https://forums.phpfreaks.com/topic/108908-solved-insert-value-from-drop-down-into-mysql/#findComment-559418 Share on other sites More sharing options...
hansford Posted June 6, 2008 Share Posted June 6, 2008 What I mean by this is these 2 statements are not contained in a form. So thats why the $_POST for these 2 are null; $category_lists ='Category List' .BuildCategory(); $countrylist = 'Country' . BuildCountry(); Quote Link to comment https://forums.phpfreaks.com/topic/108908-solved-insert-value-from-drop-down-into-mysql/#findComment-559422 Share on other sites More sharing options...
dartfrogs Posted June 6, 2008 Author Share Posted June 6, 2008 You hit the nail on the head!!! It is working! I did not see that I never called the info into the form! No wonder there was no post vaule. I added this bit of code into the form and did a couple of tweeks to the function and I am getting data and inserting it into mysql! yes! Thank you for spotting that! <tr> <td> Category List: <select name="category_lists"> <option value='.$category_lists.'</option> </select> </td> </tr> <tr> <td> Country: <select name="countrylist"> <option value='.$countrylist.'</option> </select> </td> Quote Link to comment https://forums.phpfreaks.com/topic/108908-solved-insert-value-from-drop-down-into-mysql/#findComment-559482 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.