Results 1 to 6 of 6

Thread: Need help with MySQL UPDATE

  1. #1
    Join Date
    Nov 2005
    Location
    Austin TX,US
    Posts
    71
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Need help with MySQL UPDATE

    Hi, I have a View basket page, which works fine in showing a list of items at first. In this same View Basket page, I include a button/form with checkbox to delete any item out. When users select a checkbox and click Update, this leads them to the reserve_edit page, and right back to this updated View basket page.

    The problem is: When I select any item, or 2 or more items and click Update, ONLY ONE item always gets deleted: the last item in the table.

    Could you help me out of this? I know my description is not very clear, but I hope someby could help.
    Here's my View Basket page code:
    Code:
    <?php
    $username=$_COOKIE['log']; 
    $query="SELECT * FROM cart WHERE username='$username' AND status=''"; //initial status is blank.
    $result=mysql_query($query,$connection);
    ?>
    <form name='edit' method='post' action='reserve_edit.php'>
    <table width="590"class="basket">
      <tr class='basket_header'>
        <td width='12%'height="35">Remove</td>
        <td width='34%' >Title</td>
        <td width='15%'>Number reserved </td>
        <td width='12%'>From-to</td>
        <td width='12%'>Cost</td>
        <td width='15%'>Pickup date </td>
      </tr>
    
      <?php 
      while($row = mysql_fetch_array($result)) {
      printf("<tr class='basket'>
        <td><input type='checkbox' name='delete' value='d'>
        <input name='id' type='hidden' value='%s'></td> 
        <td><a href='listing_detail_m.php?id=%s'>%s</a></td> 
        <td>%s&nbsp;%s</td>
        <td>%s</td>
        <td>$%s</td>
        <td>%s</td></tr>", 
    $row["id"],$row["id"],$row["title"],$row["number"],$row["type"],$row['from_to'],$row["cost"],$row["pickup"]); } ?>
    </table>
    <br>
    <input type="submit" name="update" value="Update" class="btn">
    </form>
    Here's my reserve_edit page code:
    Code:
    <?php
    require "config.php";
    $username=$_COOKIE['log']; 
    $status=$_POST['delete'];
    $id=$_POST['id'];
    	 
    $result = mysql_query("UPDATE cart SET status='$status' WHERE  username='$username' AND id='$id'");
    header("Location: reserve_basket.php");
    ?>
    Last edited by mtran; 06-01-2006 at 07:49 AM.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    If you want to update multiple rows, you need to have multiple elements. All those checkboxes have the same name, so they'll just override one another.

    You should also use mysql_real_escape_string() to safely escape any user data you intend to be using in a query. Not doing so leaves your database open to attack.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Nov 2005
    Location
    Austin TX,US
    Posts
    71
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    The problem is also: Even when I don't select the last item in the table, it's always the one, and the only one that gets deleted. How come?
    I'm trying to change the name of my checkboxes and may come back and bug you again!
    To Twey, about mysql_real_escape_string(), you showed my last time, but I forgot using it this time. Bad student huh!
    Last edited by mtran; 06-01-2006 at 04:44 PM.

  4. #4
    Join Date
    Nov 2005
    Location
    Austin TX,US
    Posts
    71
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    I've been researching about dynamically naming the checkboxes. I see some tutorial doing like this, but because of novice knowledge, don't know how to make it work. Could you help please with whatever way as long as it works. Thanks!!!
    Code:
    View basket:
    <input type='checkbox' name='delete[]' value='d'>
    Code:
    Edit basket:
    $status=$HTTP_POST_VARS['delete'];
    $result = mysql_query("UPDATE cart SET status='$status' WHERE  username='$username' AND id='$id'");

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Personally, I'd do it like this:
    Code:
    <input type='checkbox' name='delete" . $row['id'] . "' value='d'>
        <input name='id' type='hidden' value='%s'>
    I haven't used printf because there's no real need to here

    Then:
    Code:
    $status = mysql_real_escape_string($_POST['delete' . $id]);
    $username = mysql_real_escape_string($username);
    $result = mysql_query("UPDATE cart SET status='$status' WHERE  username='$username' AND id='$id'");
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Nov 2005
    Location
    Austin TX,US
    Posts
    71
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default It worksl but only for the last record

    I followed Twey's instruction, and the edit page works but ONLY when you select the last record:
    • if only the last record is selected, it gets deleted: OK
    • if the last record and other record are selected: only the last record is deleted: not ok
    • if other records are selected and the last one is not: nothing happens: not Ok


    I don't know why. Need help!

Bookmarks

Posting Permissions

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