Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12

Thread: Form problem

  1. #1
    Regular Coder
    Join Date
    Jan 2007
    Posts
    217
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Form problem

    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

  • #2
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Jan 2007
    Posts
    217
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Okay, i'll store the itemid in the name, so how would i grab all the individual itemid's?

    =X

  • #4
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Regular Coder
    Join Date
    Jan 2007
    Posts
    217
    Thanks
    9
    Thanked 0 Times in 0 Posts
    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

  • #6
    Regular Coder
    Join Date
    Jan 2007
    Posts
    217
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Hmm okay, i'm getting a little problem.

    PHP Code:
    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.

    PHP Code:
    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.

  • #7
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #8
    Regular Coder
    Join Date
    Jan 2007
    Posts
    217
    Thanks
    9
    Thanked 0 Times in 0 Posts
    PHP Code:
    $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 ) {
    //Create a loop. 
    Though i dont see how this will help.

  • #9
    Regular Coder
    Join Date
    Jan 2007
    Posts
    217
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Nevermind! I found my problem. It was that you cannot do ($_POST['$itemid']) it must be ($_POST["$itemid"])

    Woops xD

  • #10
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ok so you have iteminshop, then you have items for your tables. Let me simplify what you need to do.

    PHP Code:
    <?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

    PHP Code:
    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

  • #11
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    PHP Code:
    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'] 

  • #12
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rafiki View Post
    PHP Code:
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •