473,544 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

While loop using mysqli_fetch_ar ray with submit buttons

12 New Member

Currently my code is arranged in this nature, and please excuse me for being so new, I'm learning! :) So no laughing at my echo's as I don't know much about tables yet.

The issue I have is that I am using a while loop to display 5 rows of data from the mysql database. However when I put a submit button in the loop it will loop it 5 times properly but when any of the submit buttons are clicked it will default to the values from the last row of data retrieved.

Here's what I have so far with code.

Expand|Select|Wrap|Line Numbers
  1. $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
  2. $query = "SELECT * FROM test LIMIT 5";
  3. $data = mysqli_query($dbc, $query);
  5. $form_block = "
  6. <form method=\"post\" action=\"$_SERVER[PHP_SELF]\">
  7. <input type=\"text\" id=\"wq\" name=\"wq\" SIZE = 4 MAXLENGTH = 4>
  8. <input type=\"submit\" value=\"Purchase\" name=\"submit\">
  9. </form>";
  11. while ($row = mysqli_fetch_array($data)) { 
  12.  echo '<strong>Name: </strong>' . $row['name'];
  13.  echo '<br>';
  14.  echo '<strong>Attire: </strong>' . $row['wk'];
  15.  echo '<strong>Found: </strong>' . $row['wf'];
  16.  echo '<br>';
  17.  echo '<strong>Purchase Price: </strong>' . $row['wc'] . ' dollars';
  18.  echo '<br>';
  19.  echo '<strong>Description: </strong>' . $row['wt'];
  20.  echo '<br>';
  21.  echo "$form_block";
  22.  echo '</ P>';
  24. // I'm placing variables here to pull the data for later
  26. }
Jun 3 '10 #1
14 7106
8,658 Recognized Expert Moderator Expert
firstly, it’s $_POST['PHP_SELF'] (with quotes, otherwise a notice is issued).

judging your form, I’d say it defaults to no row, since it’s 5 times exactly the same form …
Jun 3 '10 #2
12 New Member
Thanks for taking a look at it, I'm still trying to figure out how to get it working. I'm not sure how to make a different form for each of the outputs in the loop.

Any suggestions?

Edit: Forgot to mention it defaults to the last row and does what it should when I click submit.
Jun 3 '10 #3
8,658 Recognized Expert Moderator Expert
what should be the difference?
Jun 3 '10 #4
12 New Member
When the data is displayed to the browser everything works as it should.

When user clicks the submit button under each row it will choose row 5 no matter what.

I want the user to be able to click the 1st row submit button and continue to purchase.
Jun 3 '10 #5
8,658 Recognized Expert Moderator Expert
out of interest, how does this form
Expand|Select|Wrap|Line Numbers
  1. <form method="post" action="…">
  2.   <input type="text" id="wq" name="wq" SIZE = 4 MAXLENGTH = 4>
  3.   <input type="submit" value="Purchase" name="submit">
  4. </form>
select anything from your row? I see nothing that relates that to a purchase.
Jun 3 '10 #6
12 New Member
That does the same thing.

What I'm trying to do is display data from the table into 5 rows with LIMIT, I'm using echo to display the information how I want it laid out. If the user clicks submit on that row then the rest of the code takes it and makes the purchase. But at the current point it only uses row 5 I presume because that's the last row and the submit button only works for that even though it repeats under each row of output.

I'm stumped, thank you for your time and help. I hope it can be figured out.


I have some if then statements below that code that take the value from the submit button and uses that to update the database for the purchase.

Maybe I just can't explain what I'm trying to do good :(
Jun 4 '10 #7
404 Contributor
Does each item have a unique ID tied to that row only? If so then add this line to your form block:

Expand|Select|Wrap|Line Numbers
  1. <input type=\"hidden\" id=\"product\" name=\"product\" value=\"%s\">
Then replace your current code that prints the form block:

Expand|Select|Wrap|Line Numbers
  1. echo "$form_block";
(which by the way shouldn't have quotes around it) with this:

Expand|Select|Wrap|Line Numbers
  1. echo sprintf($formblock, $row['unique_id']);
And replace 'unique_id' with the name of the row in the database table that actually contains the ID for that item.

If each item doesn't have a unique ID...add one then do the above : )

Disclaimer: I did not test this code, so if it doesn't work just let me know and I'll try to figure it out. It won't break anything, it just won't work if it's wrong.
Jun 4 '10 #8
12 New Member
Halo, thanks for taking the time to look into this for me. I did as you requested and each item has a unique ID in the database. $row['w_id'] so I added that code as you specified reloaded and tried again.

It still selects the last row for some reason. Let me know what else to give a try with.

EDIT: It is now assigning the row id to the %s value.
Jun 4 '10 #9
8,658 Recognized Expert Moderator Expert
It still selects the last row for some reason.
how does your script determine, what to process?
Jun 4 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Bob Lehmann | last post by:
Hi, I pretty sure I've seen this problem addressed before, but I can't find any references. The short story is that I have I have multiple submit buttons on a page, each providing different functionality. For example, I have a search option, a log in option and a save text option. I would like to handle the submits in the Click event...
by: John Pass | last post by:
What is the difference between a While and Do While/Loop repetition structure. If they is no difference (as it seems) why do both exist?
by: Poker Man | last post by:
Hi, I know how to do Sumbit buttons in Forms, and I know how to do custom buttons in Javascript. What I can't seem to find is how to do custom Submit buttons using Javascript! Anybody out there have any pagesthat might provide a clue?
by: Bill_W_Stephens | last post by:
I have a complicated page with several submit buttons. I don't want to create multiple forms because much of the input is shared and the code is getting very ugly. However I would like to determine which values will be submited. Here is a simplified example that may better illustrate my question. Or maybe there is a better approach... ...
by: Steve JORDI | last post by:
Just a question using images as submit buttons and PHP4.4.4. It seems that my code correctly works in FireFox but not in IExplorer. For example, I have a FORM with 2 buttons called "search" and "save". In a second file, the one mentioned in the "action" parameter, I test which button has been clicked in order to decide which action to take....
by: normadiah | last post by:
Dear all, I'm really appreciated if anyone can help me. I would like to ask regarding the following code... B]System.out.println("Enter Pre_condition or q to quit):"); while(condition.equalsIgnoreCase("q")==false) { condition = Input.nextLine(); if(condition.equalsIgnoreCase ("q")) break;
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all I have the following code: I am trying to use the value of the image button to evaluate what plan the user has chosen, i.e. it request("submit1")="Basic", then the user chose basic plan ... But when I click one of the image button, the thing is different from what I thought. it seems that it has field name like...
by: sbettadpur | last post by:
Hi everybody, Hi iam strugling with more than one submit buttons with in one form here is my code <form method="post" action="Offer.php" name='issueFrm' onSubmit="return fullOfferfields();"> OFFER <input type=image src="amarok_rewind.png " title="rewind" > <input type=image src="amarok_back.png" title="back"> <input type="text" size=3...
by: printline | last post by:
Hello All I have a form with multiple submit buttons, where i would like to open in a new window when hitting one of the submit buttons. Can't seem to figure this out according to the code i'm using. Could someone perhaps tell me what to do...??? Here is the code i'm using: <SCRIPT> function submitFunction(i) { if (i==1)...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.