472,351 Members | 1,579 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,351 software developers and data experts.

While loop using mysqli_fetch_array with submit buttons

12
Greetings,

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);
  4.  
  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>";
  10.  
  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>';
  23.  
  24. // I'm placing variables here to pull the data for later
  25.  
  26. }
Jun 3 '10 #1

✓ answered by HaLo2FrEeEk

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.

14 6957
Dormilich
8,658 Expert Mod 8TB
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
kadeous
12
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
Dormilich
8,658 Expert Mod 8TB
what should be the difference?
Jun 3 '10 #4
kadeous
12
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
Dormilich
8,658 Expert Mod 8TB
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
kadeous
12
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.

Edit:

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
HaLo2FrEeEk
404 256MB
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
kadeous
12
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
Dormilich
8,658 Expert Mod 8TB
It still selects the last row for some reason.
how does your script determine, what to process?
Jun 4 '10 #10
kadeous
12
Based off of the value of wq and product. They are sent into a if statement, values are compared to customer currency and then either successful purchase or failed purchase.

I think once the individual rows that are printed have the id tag assigned to them it would work. I just can't figure that out.
Jun 4 '10 #11
Dormilich
8,658 Expert Mod 8TB
Based off of the value of wq and product.
how do you pass the wq, currently you only have the id from the <input> passed down?
Jun 4 '10 #12
kadeous
12
This input passes down the user quantity typed in next to the submit button.

Expand|Select|Wrap|Line Numbers
  1. <input type=\"text\" id=\"wq\" name=\"wq\" SIZE = 4 MAXLENGTH = 4>
This new line was added to assign the ID for the row to the input as hidden.

Expand|Select|Wrap|Line Numbers
  1. <input type=\"hidden\" id=\"product\" name=\"product\" value=\"%s\">
So wq and product are submitted with the form and they are assigned to a variable then sent to an if statement.

Expand|Select|Wrap|Line Numbers
  1. $wnum = mysqli_real_escape_string($dbc, trim($_POST['wq']));
  2. $wid = $_POST['product'];
Jun 4 '10 #13
kadeous
12
Edit: I see what you were saying about handling the output, your right. I need to change the way I'm working with the variables. Will try and report back.. Thanks to both of you I think it just might work ;)

Edit 2: It worked! I wasn't using the form data correctly, and from looking back over the entire script, I wasn't far off from getting it to actually work! Learning is so much fun THANK YOU both soooo much!
Jun 4 '10 #14
HaLo2FrEeEk
404 256MB
I'm glad you figured it out. It can be a pain when it doesn't work, finally figuring it out is a huge rush though. Good work.
Jun 4 '10 #15

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

Similar topics

4
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...
6
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?
9
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...
1
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...
5
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,...
3
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...
4
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...
2
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"...
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...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

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.