469,289 Members | 2,225 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,289 developers. It's quick & easy.

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 6796
Dormilich
8,651 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,651 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,651 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,651 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,651 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

Post your reply

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

Similar topics

4 posts views Thread by Bob Lehmann | last post: by
9 posts views Thread by Poker Man | last post: by
4 posts views Thread by =?Utf-8?B?YzY3NjIyOA==?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.