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. - $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
-
$query = "SELECT * FROM test LIMIT 5";
-
$data = mysqli_query($dbc, $query);
-
-
$form_block = "
-
<form method=\"post\" action=\"$_SERVER[PHP_SELF]\">
-
<input type=\"text\" id=\"wq\" name=\"wq\" SIZE = 4 MAXLENGTH = 4>
-
<input type=\"submit\" value=\"Purchase\" name=\"submit\">
-
</form>";
-
-
while ($row = mysqli_fetch_array($data)) {
-
echo '<strong>Name: </strong>' . $row['name'];
-
echo '<br>';
-
echo '<strong>Attire: </strong>' . $row['wk'];
-
echo '<strong>Found: </strong>' . $row['wf'];
-
echo '<br>';
-
echo '<strong>Purchase Price: </strong>' . $row['wc'] . ' dollars';
-
echo '<br>';
-
echo '<strong>Description: </strong>' . $row['wt'];
-
echo '<br>';
-
echo "$form_block";
-
echo '</ P>';
-
-
// I'm placing variables here to pull the data for later
-
-
}
Does each item have a unique ID tied to that row only? If so then add this line to your form block: - <input type=\"hidden\" id=\"product\" name=\"product\" value=\"%s\">
Then replace your current code that prints the form block:
(which by the way shouldn't have quotes around it) with this: - 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
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 …
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.
what should be the difference?
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.
out of interest, how does this form - <form method="post" action="…">
-
<input type="text" id="wq" name="wq" SIZE = 4 MAXLENGTH = 4>
-
<input type="submit" value="Purchase" name="submit">
-
</form>
select anything from your row? I see nothing that relates that to a purchase.
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 :(
Does each item have a unique ID tied to that row only? If so then add this line to your form block: - <input type=\"hidden\" id=\"product\" name=\"product\" value=\"%s\">
Then replace your current code that prints the form block:
(which by the way shouldn't have quotes around it) with this: - 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.
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.
It still selects the last row for some reason.
how does your script determine, what to process?
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.
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?
This input passes down the user quantity typed in next to the submit button. - <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. - <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. - $wnum = mysqli_real_escape_string($dbc, trim($_POST['wq']));
-
$wid = $_POST['product'];
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!
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.
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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"...
|
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...
|
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...
|
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...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
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...
|
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.
...
|
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...
|
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...
|
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...
|
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...
| |