PHP Echo Help

Hi All,

Im currently having difficulties trying to echo certain data from a database.

I have a dropdown list which consist of 4 items from my database, i added these to the dropdown using the following code:

   		<td align = "right">Product Category: </td>


   		<td>

   			<select name = "product_category">

   				<option> Select a category </option>
   				<?php

   				   $get_cats = "select * from categories";

                   $run_cats = mysqli_query($con, $get_cats);

                   while ($row_cats=mysqli_fetch_array($run_cats)){

	               $cat_id = $row_cats['cat_ids']; 
	               $cat_title = $row_cats['cat_title'];

                   echo "<option value = '$cat_id'>$cat_title</option>";

               }

This part is fine as the dropdown loads the list from the database. the next step is echoing what ever is selected in the dropdown. i do this using the following code:

<?php if (isset($_POST['insert_post'])) { //Retrieving data from input fields from above. $product_title = $_POST['product_title']; $product_cat = $_POST['product_cat']; $product_course = $_POST['product_course']; $product_price = $_POST['product_price']; $product_desc = $_POST['product_desc']; $product_keywords = $_POST['product_keywords']; echo $insert_product = "insert into products (product_course, product_cat, product_title,product_price,product_desc,product_keywords) values ('$product_course','$product_cat','$product_title','$product_price','$product_desc','$product_keywords')"; } This is partially fine which is where i come to my issue. [b]nothing is echoed for product_cat.[/b] it just appears as to quotation marks. product_course is identical to category but different items and works fine. if i select the third option from the dropdown for course it will echo a 3 on the page. This is the echoed result. insert into products (product_course, product_cat, product_title,product_price,product_desc,product_keywords) values ('5','','Example','3.00',' Example ','Example') as you can see above there is a '' displayed which should contain a value between 1-4 depending on the selected item. Any help will be greatly appreciated.

DUPLICATE POST:
http://www.phphelp.com/forum/general-php-help/accessing-data-dynamically-from-myphpadmin/msg87334/#msg87334

You already post this in another thread. Please do not make a new thread with the same issue.

LOL… Kevin, he fixed the other problem you pointed out to him and made a new post with a new problem.
The previous post should be marked solved and this one contnued. Aaronsp, normally we continue a post
until it is completely finished. And, then we will mark it solved…

So, you have created a drop-down using this:
Which makes your field named “product_category” not “product_cat” !
Simple error. Since that drop-down value is a DB id, it is usually an integer. So, when displayed, most
likely it will show as a number. Hopefully that is what you are looking for.

Hi

i have changed the select name to product_cat in my code to reflect the echo statement.

echo statement below

f (isset($_POST[‘insert_post’])) {

	//Retrieving data from input fields from above.

        $product_title = $_POST['product_title'];
	$product_cat = $_POST['product_cat'];
	$product_course = $_POST['product_course'];
	$product_price = $_POST['product_price'];
	$product_desc = $_POST['product_desc'];
	$product_keywords = $_POST['product_keywords'];


	 echo $insert_product = "insert into products (product_course, product_cat, product_title,product_price,product_desc,product_keywords) values ('$product_course','$product_cat','$product_title','$product_price','$product_desc','$product_keywords')";


}

I’m still getting nothing from the product_cat field when an item is selected.

managed to solve this now.

mods can you delete this thread. thank you.

aaronsp, Well, let’s go back to form basics so you can try to debug it…

You did not show your form on this thread. So, It should look something like:

So, the tag points at to where the form is posted to. Quite often it posts to the same page.
Once posted, all input fields are sent to the receiving page. (itself or not) That page which you showed
pulls out the data. That would be the first area to debug. If your tag is named “product_cat”,
you should first check to see what is being sent back to your code. So, change the first line of code to
these two lines. Run it and see what it displays…
[php]
if (isset($_POST[‘insert_post’])) {
die($_POST[‘product_cat’];
[/php]
What this does it stop the code right after it gets posted. And, it shows the data that was sent from the
clause… If it displays nothing, then you have to repost the clause as you must have
spelled something wrong or formatted something wrong. Form posting is simple. Not much to it. If you
have many posted variables and all but one get thru, then that says clearly that you have a badly formed
clause (in this case a select) or you are not loading it back correctly.

Now, to review the clause to see if it is valid you DEBUG the live page. Go to the page and test
if the select allows it to be changed. If so, next RIGHT-CLICK on the page in a blank area and select the
VIEW-SOURCE option. (on Firefox it is VIEW-PAGE-SOURCE) Then, you can look at the page as it appears
to a browser. You can move down to the tag and see if it is well-formatted. What I mean by
that is make sure the tags are all in place and no extra spaces and all the quotes and double-quotes are
matched up, etc.

Well, debug that part and get back to us. If you can not figure it out, please post your form code for us.

Lastly, please note that you need to post code as-is inside of tags in the buttons above when you
reply. Press the PHP button and place your code inside that. Makes it much easier for us. Good luck!

Sponsor our Newsletter | Privacy Policy | Terms of Service