简体   繁体   中英

MySQL, updating ONE row

I've been looking through different sites trying to find some help but I can't seem to find anything with my problem, so here it is:

I have a value system, which needs updating quite often, and instead of deleting & re-creating it, I want to use an edit function with MySQL update.

I already have an update working, but it updates ALL rows. I want it to update just one - but that's not the full problem.

I want it so when I click Edit on item1, it goes to the next page with JUST item1, not item1, item2 and item3.

Example:

+------------+-------------+---------------+
| Item       | Value       | Actions       |
+------------+-------------+---------------+
| item1      | 123         | Edit / Delete |
| item2      | 456         | Edit / Delete |
| item2      | 789         | Edit / Delete |
+------------+-------------+---------------+

That's my administrator table of items (not the database). I want it so I click Edit (delete works fine), and it takes me to the next page with just the item I want editing. At the moment, it takes me to the next page but will ALL the items.

Here's what my MySQL would look like:

+------------+-------------+---------------+
| id         | name        | value         |
+------------+-------------+---------------+
| 7          | item1       | 123           |
| 8          | item2       | 456           |
| 9          | item3       | 789           |
+------------+-------------+---------------+

"id" is Primary, and auto increment. Basically, I have everything set up apart from when it comes to the next page for editing.

I have the edit links linking to the correct ID and all that so it'll be for item1 with ID 7: /admin/index.php?_managevalues&itemId=7

But it's still displaying ALL items & updates ALL items values.

Thanks in advance, and I will happily post code if it's needed.

EDIT1:

Okay, here's manageitems.php:

<tr>
    <td><b>Item Name</b></td>
    <td><b>Image</b></td>
    <td><b>Action</b></td>
    </tr>
        <?php
            if($getitem = $db->query("SELECT * FROM lukevalues ORDER BY name ASC")) {
            while($item = $getitem->fetch_assoc()) {
    echo "
    <tr>
    <td><b>" . $item['name'] . "</b><br>at the price of <b>" . $item['price'] . "</b></td>
    <td><img src='" . $item['image'] . "'></td>
    <td><a href='index.php?_page=edititem&itemId=" . $item['id'] . "'>Edit Price</a> / <a href='index.php?_page=manageitem&del=" . $item['id'] . "'>Remove</a></td>
    </tr>";
    }
        }
            ?>
    </table>
    <?php if(isset($_GET["del"]))
        {
        $db->real_query("DELETE FROM lukevalues WHERE id = '" . filter($_GET["del"]) . "'");
    echo "
    Item has been removed successfully. <br /><br />
    ";
        }
        echo $output;
        ?>

EDIT2:

Here's my edit page:

<?php
        if($getitem = $db->query("SELECT * FROM lukevalues")) {
        while($item = $getitem->fetch_assoc()) {
echo "

You're editing: <b>" . $item['name'] . "</b><br>
Which has a price of <b>" . $item['price'] . "</b>
            <br /><br>
            <form method='post'>
                <h3>New Price</h3>
                <input type='text' name='newprice'> <br />
                <br />
                <input type='submit' value='Update Price'>
            </form>";
                if(isset($_POST["newprice"])) {
                    $new = $db->real_escape_string($_POST["newprice"]);

                        $db->real_query( "UPDATE lukevalues SET price='$new' WHERE id = ".$item['id']);

                echo "<br><br>Done! <a href='http://habzilla.net/admin/index.php?_page=manageitems'>Go back</a> to manage items.";
            }
        }

    }
        ?>

The problem with your code is that when a user hits 'Update Price' and your forms submits you are no longer have the value of item['id'] . You can store id values for your items in hidden fields in your forms.

Your code for edit page might look like

<?php
// If it was an update submission do the update first
if(isset($_POST['newprice']) && isset($_POST['id']) && 
         $_POST['newprice'] && $_POST['id']) {
    $new = $db->real_escape_string($_POST['newprice']);
    $id  = $db->real_escape_string($_POST['id']);
    $db->real_query("UPDATE lukevalues SET price='$new' WHERE id = '$id'";
    echo "<br><br>Done! <a href='/admin/index.php?_page=manageitems'>Go back</a> to manage items.";
}
// Now fetch all items and produce edit forms with updates values
if($getitem = $db->query("SELECT * FROM lukevalues")) {
    while($item = $getitem->fetch_assoc()) {
        echo "You're editing: <b>" . $item['name'] . "</b><br>
              Which has a price of <b>" . $item['price'] . "</b>
              <br /><br>
              <form method='post'>
                <h3>New Price</h3>
                <input type='text'   name='newprice'> <br />
                <input type='hidden' name='id' value=" .$item['id']. ">
                <br />
                <input type='submit' value='Update Price'>
            </form>";
    }
}

Note: code obviously has not been tested.

On a side note: learn and use prepared statements. Your current code is vulnerable to sql injections.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM