Jump to content

PHP Show a Text box for each row and update all with 1 submit button?


Stalingrad

Recommended Posts

Hi! I am still stuck on this problem, I can't give up on it... but there is no other way around it... What I am trying to do is this: Display the name/image of the row AND a text box underneath it... but there are multiple rows in most cases. I also want it so that the user can type in a number in each of the textboxes, and have them all update with the different inserted text with the same, one submit button... here is my code I am having trouble with:

if($action == "stock") {

$setprice = $_POST['prices'];
$updateprice = $_POST['updateprice'];

$_POST['prices'] = Array('$setprice');

echo "<a href=?action=edit>Edit Shop</a> | <a href=?action=view&user=$suserid>View Shop</a> | <a href=?action=stock>View Stock</a> | <a href=?action=quick>Quick Stock</a><br><br><font size=5>Stock Shop</font><br><br>";
?>
<form action="<?php echo "$PHP_SELF"; ?>" method="POST">
<?php
$eq = "SELECT * FROM uitems WHERE username='$suserid' AND location='2' GROUP BY theitemid";
$ee = mysql_query($eq);
while($erow = mysql_fetch_array($ee)) {

$eeloc = $erow['location'];
$eeid = $erow['theitemid'];
$eenowid = $erow['uitemid'];
$eeprice = $erow['price'];

$wq = "SELECT * FROM items WHERE itemid='$eeid'";
$ww = mysql_query($wq);
while($wrow = mysql_fetch_array($ww)) {

$cq = mysql_query("SELECT * FROM uitems WHERE username='$suserid' AND location='2' AND theitemid='$eeid'");
$lcq = mysql_num_rows($cq);
$fid = $wrow['itemid'];
$fname = $wrow['name'];
$fimage = $wrow['image'];
$frarity = $wrow['rarity'];
$fdesc = $wrow['description'];

echo "<br>$fname<br><img src=/images/items/$fimage><br><br>";
?>
<form action="<?php echo "$PHP_SELF"; ?>" method="POST"><input type="text" name="prices[]" value="<?php echo "$eeprice"; ?>"><br>
<?php

}
?>
<?php
}

Please note that this is NOT the whole code oft he page, but none of the other parts of the code have anything to do with this part... it is split into "pages" with PHP (same page, but $_GET is used for "multiple" pages. If anybody can help me do this, I would appreciate it SO much! Oh.. and by the way... right now (the code you see above), displays the text boxes and the image/name of it... with one submit button.... the form loads, but only the very last text box displayed gets "updated" and it says "Array". Thank you!

Link to comment
Share on other sites

 

I can't give up on it......

 

Of course NOT, your nickName is Stalingrad :)

 

So, seriously, you should really avoid runing queries in loop(s), unless you want to easy to start hammering the database.

 

Use a SQL join clause which combines records from two or more tables in a database.

 

Redesign your script and come back again and we help you out on this. 

Link to comment
Share on other sites

This is totally off the cuff and not tested. I don't have your database and I'm not going to take the time to create one.

 

Here is how you can create the form with inputs for all the items in one form. Note that the id of each record is used as the index of the field name

 

<?php

if($action == "stock")
{
    $sql = "SELECT uitemid, price,
                   name, image
            FROM uitems
            JOIN items ON uitems.theitemid = items.itemid
            WHERE username='$suserid'
              AND location='2'
            GROUP BY theitemid";
    $result = mysql_query($sql);
    
    $formHTML = '';
    while($row = mysql_fetch_assoc($result))
    {
        $formHTML .= "{$row['name']}<br><img src=/images/items/{$row['image']}><br>\n";
        $formHTML .= "<input type=\"text\" name=\"prices[{$row['uitemid']}]\" value=\"{$row['price']}\" /><br><br>";
    }
?>
<a href=?action=edit>Edit Shop</a> |
<a href=?action=view&user=$suserid>View Shop</a> |
<a href=?action=stock>View Stock</a> |
<a href=?action=quick>Quick Stock</a>
<br><br>
<font size=5>Stock Shop</font>
<br><br>

<form action="<?php echo "$PHP_SELF"; ?>" method="POST">
<?php echo $formHTML; ?>
</form>

 

 

Here is how you could process the form to update the values in the DB

 

<?php

if(isset($_POST['prices']))
{
    //Process user input
    $updateValues = '';
    $updateIDs = array();
    foreach($_POST['prices'] as $uitemid => $price)
    {
        $uitemid = intval($uitemid);
        if(!$uitemid) { continue; }
        $updateIDs[] = $uitemid;
        $price = round(floatval($price), 2);
        
        $updateValues .= "WHEN {$uitemid} THEN {$price} \n";
    }
    $uitemidList = implode(',', array_keys());
    
    //Create ONE query to update all the values
    $sql = "UPDATE uitems
            SET price = CASE uitemid
              {$updateValues}
            WHERE username='$suserid'
              AND location='2'";
    $result = mysql_query($sql);
}

?>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.