...

View Full Version : Form problem



ole90
04-04-2007, 10:30 PM
Okay, this might be confusing but bare with me.

I have created a while loop which grabs all items in a users shop and displays them. When it displays them, i created a form field for the price so the user can set a price.

But my problem is, when there is multiple rows, how do i update each price field? E.G

While (there are rows in the db) {
display item
form
input price
}
echo form submit button.

How do i grab each input for each row and update it =X?

Sorry if its confusing. xD

iLLin
04-04-2007, 10:36 PM
arrays in your input fields like so <input name="item[]" OR you can setup field names based on the id number. For instance <input name="update_$id"> Then just explode your names by the "_" and update by the id... OR add in the id number in the input name array like so <input name="item[$id]"> ...

That should get you started.

ole90
04-04-2007, 10:41 PM
Okay, i'll store the itemid in the name, so how would i grab all the individual itemid's?

=X

iLLin
04-04-2007, 10:46 PM
I am assuming your items have individual id numbers. Or I could be misunderstanding what your trying to accomplish.

Are you querying for all your items to display in input's? When you do that you should have the id at that time.

ole90
04-04-2007, 10:53 PM
Yep. Each item has it's own id.

I've just thought of something right now and i'm going to try it;

I'll run the query to grab all the users items in the shop and then store each itemid in the variable itemid then in a while loop do $price = ($_POST['$itemid']);
which should work considering it'll grab all the users items from the shop :D

ole90
04-04-2007, 11:10 PM
Hmm okay, i'm getting a little problem.


while($row=mysql_fetch_array($fhg)) {
//Now we need to get all item infor for each item.
$itemid= $row['itemid'];
$price= $row['price'];
$amount= $row['amount'];
//query
$go="SELECT * FROM `items` WHERE `ID` = '$itemid'";
$jew=mysql_query($go) or die(mysql_error());
$i=mysql_fetch_array($jew);
$itemname= $i['itemname'];
$image= $i['itemimageurl'];
//Now lets echo a table.
echo"<table width='700' border='0' cellspacing='1' cellpadding='0' bgcolor='#000000'>
<tr bgcolor='1A498F'>
<td width='30'><img src='$image'></td>
<td width='300'>$itemname</td>
<td width='70'>$amount</td>
<td width='200'>$itemid<form action='yourshop.php?action=update' method='post'><input type='TEXT' name='$itemid' value='$price'></td>
<td width='100'><a href='yourshop.php?action=takeout&&itemid={$itemid}'>Take Out</a></td>
</tr>
</table>";
}
echo"<input type='submit' value='Update'></form>";

This is where i display the item. As you can see, i have created a form which allows the user to input a price. I have given the name of the input as itemid.


if($action =="update") {
//Grab all items
$gog="SELECT * FROM `iteminshop` WHERE `userid` = '$ID9'";
$frg=mysql_query($gog) or die(mysql_error());
while($roow=mysql_fetch_array($frg)) {
$itemid = $roow['itemid'];
$price = ($_POST['$itemid']);
echo"$price";
}

This is where the form goes to once the user clicks the submit button. Now, i have ran a while loop which will get all items in the users shop and then grab the id. However, $_POST['$itemid'] is not grabbing the value from the form. I have echoed $itemid out in the first program and in the second program and they both are the same. $price when echoed contains nothing.

Can you spot any errors =X?

Sorry for double post.

iLLin
04-04-2007, 11:20 PM
I dont complete understand what your doing. First you have a while statement. What query is that? Then you are querying within that while? I never do that unless I have no choice, you should be able to grab all your data and loop through it all without requerying. Post up your first query lets start there.

ole90
04-04-2007, 11:26 PM
$gog="SELECT * FROM `iteminshop` WHERE `userid` = '$ID9'";
$fhg=mysql_query($gog) or die(mysql_error());
$fff=mysql_num_rows($fhg);
echo"<table width='700' border='0' cellspacing='1' cellpadding='0' bgcolor='#000000'>
<tr bgcolor='1A498F'>
<td width='30'>Image</td>
<td width='300'>Item Name</td>
<td width='70'>Amount</td>
<td width='200'>Your Price</td>
<td width='100'>Take Out</td>
</tr>
</table>";
if($fff > 0 ) {
//Create a loop.

Though i dont see how this will help.

ole90
04-04-2007, 11:37 PM
Nevermind! I found my problem. It was that you cannot do ($_POST['$itemid']) it must be ($_POST["$itemid"])

Woops xD

iLLin
04-04-2007, 11:40 PM
Ok so you have iteminshop, then you have items for your tables. Let me simplify what you need to do.



<?php
$query = mysql_query("SELECT * FROM items") or die;
while($row = mysql_fetch_array($query)) {
<tr bgcolor='1A498F'>
<td width='30'><img src='$image'></td>
<td width='300'>$itemname</td>
<td width='70'>$amount</td>
<td width='200'>$itemid <input type='TEXT' name='item[$itemid']' value='$price'></td>
<td width='100'><a href='yourshop.php?action=takeout&&itemid={$itemid}'>Take Out</a></td>
</tr>
}
?>


Then you need to loop through your post and update everything



foreach($_POST['item'] as $item_id => $price) {
mysql_query("UPDATE items SET price = '".$price."' WHERE id = '".$item_id."'") or die;


Maybe that will help you some. There are other ways some better I'm sure but this will get you where you need to be. I also think your using too many queries but one prob at a time :)

rafiki
04-05-2007, 01:13 PM
echo "<tr bgcolor='1A498F'>";
echo "<td width='30'><img src=$image></td>";
echo "<td width='300'>$itemname</td>";
echo "<td width='70'>$amount</td>"
echo "<td width='200'>$itemid <input type='TEXT' name=item['$itemid'] value=\'$price\'></td>";
echo "<td width='100'><a href=\'yourshop.php?action=takeout&&itemid={$itemid}\'>Take Out</a></td>";
echo "</tr>" ;
//dont put a string in ' ''sor it will display $itemid not the contents of the string
//and you also missed a ' in name=item['$itemid']

iLLin
04-05-2007, 02:48 PM
echo "<tr bgcolor='1A498F'>";
echo "<td width='30'><img src=$image></td>";
echo "<td width='300'>$itemname</td>";
echo "<td width='70'>$amount</td>"
echo "<td width='200'>$itemid <input type='TEXT' name=item['$itemid'] value=\'$price\'></td>";
echo "<td width='100'><a href=\'yourshop.php?action=takeout&&itemid={$itemid}\'>Take Out</a></td>";
echo "</tr>" ;
//dont put a string in ' ''sor it will display $itemid not the contents of the string
//and you also missed a ' in name=item['$itemid']


I'm not making code for him to copy and paste, but thanks for showing him correct syntax. :)

Oh and you missed my close bracket in the other one, don't forget that one :p



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum