By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,692 Members | 1,498 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,692 IT Pros & Developers. It's quick & easy.

While loop using mysqli_fetch_array with submit buttons

P: 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.

Share this Question
Share on Google+
14 Replies


Dormilich
Expert Mod 5K+
P: 8,639
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

P: 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
Expert Mod 5K+
P: 8,639
what should be the difference?
Jun 3 '10 #4

P: 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
Expert Mod 5K+
P: 8,639
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

P: 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
100+
P: 404
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

P: 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
Expert Mod 5K+
P: 8,639
It still selects the last row for some reason.
how does your script determine, what to process?
Jun 4 '10 #10

P: 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
Expert Mod 5K+
P: 8,639
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

P: 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

P: 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
100+
P: 404
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

Post your reply

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