Dear,
I am making an ordering form and all of the products' data are stored in a MySQL database.
The menu form to select products are all working with plain HTML not php echo's.
But now I can change products' info via a separate web page by updating the MySQL using PHP - more convenient.
From there I can display all 20 items on the menu using PHP's mysql_query:

require('connect.php');
$qty1=$_POST['qty1'];
$qty2=$_POST['qty2'];
//more php code to send to mysql..... (cut out for this)



$getmenu= mysql_query('SELECT * FROM orders WHERE `member`="System"') or die("We've encountered an error:" . mysql_error());

	  while ($rows3 = mysql_fetch_array($getmenu)) 
{
	  $item1=$rows3['qty1'];
	  $item2=$rows3['qty2'];
	  $item3=$rows3['qty3'];
	  $item4=$rows3['qty4'];
	  $item5=$rows3['qty5'];
	  //This goes on for 20 products..is there a more efficient way? 
          //Does this put too much strain on the server? 
}

Because of this, I can also filter out the blank items:

<form action="configpizza.php" method="post">
<?php
if($item1!=NULL)
{ 
echo '<input name="qty1" type="text" class="textfield" id="qty1" />'
echo '//some form elements here...';
}
if($item2!=NULL)
{ 
echo '<input name="qty2" type="text" class="textfield" id="qty2" />'
echo '// and some more form elements here...';
}
//Now I do this for every item..very tedious and slow!
?>
</form>

Problem:
When I press the submit button it does not send properly to the database. I think it is because it does not process the if statements again so the echoed form parts aren't included... any solutions to this php submitting problem or a more efficient way to deal with all this products?

Remember, this working if I'm using plain html form codes (only good for a small numbers of items) and not the PHP If's.

I hope this makes sense, and I look forward to ideas,

Thanks in advance.

Can I just ask, do you want to display 20 records from the table? And each of these records have a field called 'qty' or something?

Also, I don't understand what you're doing with the last part.. Are you inserting something?

I want to display 20 records (field name is "qty1", "qty2" etc), if they are blank, don't display them; this is already covered but not efficiently.

Let me clarify.

<form action="configpizza.php" method="post">
<?php
//database connection and other variables here
//mysql_query  INSERTS.. if submit button is pressed.

if($item1!=NULL)
{
echo '<input name="qty1" type="text" class="textfield" id="qty1" />';
echo '//some form elements here...';
}
[B]//This does not get submitted to MySQL![/B]
?>

//Other plain HTML form elements:
<input name="qty2" type="text" class="textfield" id="qty2" />
<input name="qty3" type="text" class="textfield" id="qty3" />
//and so on for the rest of 20 items
//This is submitted correctly as it is not in PHP.

</form>

I need to use more PHP (to echo out form) in order to hide blank rows, that's why I don't want to use the plain HTML form.

Thanks in advance.

Why do you have a table in your database with:

'qty1, qty2, qty3, qty4' etc? Just have 1 called 'qty' :S This way you wouldn't have to mess around with any of this!

Ah, but each field name, 'qty', is a separate item.

Right, what I'm guessing is your table looks like this:

order_number first_name, last_name, qty1 qty2 qty3
----------------------------------------------
1 Phorce In Time 1
2 James Henry 1
3 blah blah 2


You shouldn't really have null data in your table, it's bad practise..


order_number first_name, last_name, qty
------------------------------------
1 Phorce In Time 1
2 James Henry 1
3 blah blah 2

If that makes sense? Sorry if I'm completely missing the point here? ...

Each item ordered has a qty..

thanks for your help, Phorce:
Btw, I use 'qty' to stand for quantity.
My table looks like this:

ID name qty1 qty2 qty3 etc...
------------------------------------------------
1 System Book Tea NULL
2 Phorce 1 2 -----------(this means you ordered 1 Book, 2 Tea etc...)
3 Lee 0 1 -----------(I ordered 1 Tea)

Items "qty1" and "qty2" are displayed. However since qty3 is NULL (nothing) it is not displayed. I have done this by:

if($item1!=NULL)
{ 
echo '//input text field etc';
}

This not submited when the submit button is pressed. The form is a self submitting form (ie form action="<?php echo $_SERVER; ?>" )

Thanks,

Lee

Hey Lee,

You're designing and structuring your database wrong, which, is why you're having this problem:

Items "qty1" and "qty2" are displayed. However since qty3 is NULL (nothing) it is not displayed. I have done this by:

qty2 SHOULD contain something.. Because let's say if I came along and ordered a book:

username='Phorce', book='PHP OOP', qty='1' -> I've ordered 1 book.. No redundant data.

Your way:

username='Phorce', book='PHP OOP', pizza='cheese' qty='1', qty='2', qty='3'

Everytime someone orders a product (whether it's a book, pizza, or even a monkey) it should have it's own row inside the table, and, therefore, can have it's own quantity..

I'm trying to put this across in a way you can understand :(!

Dear Phorce,

Thanks for your reply.
Just for clarity, this is only a pizza ordering system. I was using examples before. :)
I think I understand what you are trying to say:

ID name Pizza qty
------------------------------------------------
1 James Cheese 1
2 Phorce Pineapple 2
3 Lee Supreme 1
4 Lee Cheese 1

  • Is this what you are trying to say?

I think I need each user to have one row each - I have statistics to display.
In my previous post, I have a row with a user "System." This is what the qty fields refers to what type of pizza it is:

ID name qty1 qty2 qty3 etc...
------------------------------------------------
1 System Cheese Supreme NULL ---(this means qty3 is not used as I only have 2 items available)
2 Phorce 1 2 -----------(this means you ordered 1 Cheese, 2 Supreme etc...)
3 Lee 0 1 -----------(I ordered 1 Supreme )

I need a System a row so that it is easy to change the item name.
I have done this by:

$qty1=$_POST['qty1'];
mysql_query("UPDATE orders SET qty='$qty1' //etc WHERE user='System'")

Thanks in advance,

Lee

You might have wrong db structure.
Separate the user table and order table. User table should has ID numbers, names and ordered. Also, order table should has ID numbers, names and quantities.

For example, if one user click cheese to order, then update the ordered field (insert the ID number of the ordered item) from user table where username/id is the user/user's id who made the order. If the user made more than one orders, you can store the id numbers of the ordered item together with comma (1,2,3 etc).
And then, you might update quantities field from order table where the field 'quantities' is Cheese. So, you can analyze how many orders exist for each item. Once, the user has finished the ordered process or logged out without finishing the order process, you must remove the order from that user.

Hope this help.

Look up 'Database Normalisation'

From what I can see, your table is not even in first normal form.. Which is why you're having this problem :)

Thanks guys,

I will now try to separate the ordering system in 2 tables:
Users data and Product Info

I will update my progress in a while.

Lee

Dear,

I have restructured my database now, and still in the process of correcting my php files.
I have encountered the original submission error.

Submission code:

$submit=$_POST['submit'];
$sitem=$_POST['qty1'];
$sdesc=$_POST['desc1'];

$sql = "UPDATE products SET item='$sitem' ,description='$sdesc' , WHERE `id`='".mysql_escape_string($id)."'";

if($submit) //submit button is pressed
{
	mysql_query($sql);
}

Form elements:

$system = 'SELECT * FROM products ORDER BY id ASC';
if(!$result2=mysql_query($system)){
die('Error encountered. MySQL said: '.mysql_error());
}
while ($rows2 = mysql_fetch_array($result2)) 
{
	  $id=$rows2['id'];
	  $gitem=$rows2['item'];
	  $gdesc=$rows2['description'];
	  
	  $menu='<input name="qty1" type="text" class="textfield" id="qty1" value="'. $gitem .'" size="25"/>
              <textarea name="desc1" cols="10" rows="3" class="textfield" id="desc1" style="width: 222px; height: 51px;">'.$gdesc .'</textarea>';
echo $menu; 
}
  • Submit button is pressed.

I have 4 items displayed correctly, connection to MySQL is correct.
Only the last item (highest ID) is updated - the other 3 products remains the same (they all share the same textfield name's and id's).

I would like to make my problem more clear:
Using PHP to posting mutiple HTML form fields.

  • I am using PHP to generate HTML form elements (eg. input textfields)
  • Database has been redesigned: Table1= Orders, Table2= Product Data
  • All code to display product information and to connect to MySQL, is working correctly

Problem:
When I submit the form, only the newest/lastest row is updated (the one with the highest ID). The other fields are unaffected.
My idea to why it is happening:
I notice the textfields all share the same name's. This is because of the PHP generated HTML.
Question:
How do I make each textfield have its own unique name using generated PHP? (eg. qty1, qty2).

Thanks in advance,

Lee

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.