SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    At My Desk!!
    Posts
    1,642
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Deleting entries in DB

    I have some code which searches the DB for the info the uiser has added to there shopping list, when in the shopping list i want to have a button that they can press to remove one of the items if they dont want it anymore, here is some code that i have tried but can get to work:

    PHP Code:
    <?php

    $Connect 
    mysql_connect('localhost''****''****');

    mysql_select_db('shadesofjapan'$Connect);

    $Result mysql_query("SELECT ProductName, Finish, Size, Price, Quantity FROM shoppinglist WHERE UserId = '" $_SESSION['UserId'] . "'") or die('Error: '.mysql_error()); 


    if (
    mysql_num_rows($Result) != 0)
    {

        while (
    $MyRow mysql_fetch_array($Result))
        {
        
            
    $ProductName $MyRow['ProductName'];
            
    $Finish $MyRow['Finish'];
            
    $Size $MyRow['Size'];
            
    $Price $MyRow['Price'];
            
    $Quantity $MyRow['Quantity'];
        
            
    ?>
              
              <table width="607" height="197"  border="0">
                <tr>
                  <td><h2>Product Name:<font color="#FF0000"><? echo $MyRow['ProductName']; ?></font></h2> </td>
                </tr>
                <tr>
                  <td><h2>Finish:<font color="#FF0000"><? echo $MyRow['Finish']; ?></font></h2> </td>
                </tr>
                <tr>
                  <td><h2>Size:<font color="#FF0000"><? echo $MyRow['Size']; ?></font></h2> </td>
                </tr>
                <tr>
                  <td><h2>Price:<font color="#FF0000"><? echo $MyRow['Price']; ?></font></h2> </td>
                </tr>
                <tr>
                  <td><h2>Quantity:<font color="#FF0000"><? echo $MyRow['Quantity']; ?></font></h2> </td>
                </tr>
                <tr>
                  <td height="83"><div align="center">
                    <form name="form1" method="post" action=""><input type="submit" name="RemoveItem" value="Remove Item">
                    </form> 
                  </div></td>
                </tr>
              </table>
              <p>
              
            
    <?php
        
    }
        
    }
    else
    {
        echo
    "<h2>You have not selected any items to order</h2>";
    }
    //Here Onward is the deletion part

        
    if ($_POST['RemoveItem'] == TRUE)
        {
        
            
    $Result mysql_query("DELETE FROM 'shoppinglist' WHERE 'ProductName' = '$ProductName' AND 'Finish' = '$Finish' AND 'Size' = '$Size' AND 'Price' = '$Price' AND 'Quantity' = '$Quantity' AND 'UserId' = '{$_SESSION['UserId']}'") or die (mysql_error);
        
        }
        
            if (
    $_POST['RemoveItem'] == TRUE AND $Result == TRUE)
            {
                
    ?>
                <script type="text/javascript">
                alert ("Item Successfully Removed")
                </script>
                <?php
            
    }
                
        
    ?>
    The code includes the working part where the info is dragged out of the DB anddisplayed in tables,
    "Am I the only one doing ASP.NET in Delphi(Pascal)?"

  2. #2
    SitePoint Addict evilone's Avatar
    Join Date
    Oct 2004
    Location
    Estonia
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php 

    $Connect 
    mysql_connect('localhost''****''****'); 

    mysql_select_db('shadesofjapan'$Connect); 

    $Result mysql_query("SELECT ProductName, Finish, Size, Price, Quantity FROM shoppinglist WHERE UserId = '" $_SESSION['UserId'] . "'") or die('Error: '.mysql_error()); 


    if (
    mysql_num_rows($Result) != 0


        while (
    $MyRow mysql_fetch_array($Result)) 
        { 
         
            
    $ProductName $MyRow['ProductName']; 
            
    $Finish $MyRow['Finish']; 
            
    $Size $MyRow['Size']; 
            
    $Price $MyRow['Price']; 
            
    $Quantity $MyRow['Quantity']; 
         
            
    ?> 
               
              <table width="607" height="197"  border="0"> 
              <form name="form1" method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
                <tr> 
                  <td><h2>Product Name:<font color="#FF0000"><? echo $MyRow['ProductName']; ?></font></h2> </td> 
                </tr> 
                <tr> 
                  <td><h2>Finish:<font color="#FF0000"><? echo $MyRow['Finish']; ?></font></h2> </td> 
                </tr> 
                <tr> 
                  <td><h2>Size:<font color="#FF0000"><? echo $MyRow['Size']; ?></font></h2> </td> 
                </tr> 
                <tr> 
                  <td><h2>Price:<font color="#FF0000"><? echo $MyRow['Price']; ?></font></h2> </td> 
                </tr> 
                <tr> 
                  <td><h2>Quantity:<font color="#FF0000"><? echo $MyRow['Quantity']; ?></font></h2> </td> 
                </tr> 
                <tr> 
                  <td height="83"><div align="center"> 
    <input type="submit" name="RemoveItem" value="Remove Item">  
                  </div></td> 
                </tr>
              </form> 
              </table> 
              <p> 
               
             
    <?php 
        

         

    else 

        echo
    "<h2>You have not selected any items to order</h2>"

    //Here Onward is the deletion part 

        
    if (isset($_POST['RemoveItem'])) 
        { 
         
            
    $deleting mysql_query("DELETE FROM shoppinglist WHERE ProductName = '" $_POST["ProductName"] . "' AND Finish = '" $_POST["Finish"] . "' AND Size = '" $_POST["Size"] . "' AND Price = '" $_POST["Price"] . "' AND Quantity = '" $_POST["Quantity"] . "' AND UserId = '" $_SESSION['UserId'] . "'") or die (mysql_error); 
         
        } 
         
            if (
    $deleting
            { 
                
    ?> 
                <script type="text/javascript"> 
                alert ("Item Successfully Removed") 
                </script> 
                <?php 
            

                 
         
    ?>

  3. #3
    orange pips! orange pips!
    Join Date
    Oct 2001
    Location
    Its all about location
    Posts
    652
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In your delete query, dont put single quotes around the table column names, and the table name itself. For example:
    PHP Code:
    'shoppinglist'
    //change to
    shoppinglist

    'ProductName' '$ProductName'
    //change to
    ProductName '$ProductName' 

  4. #4
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    At My Desk!!
    Posts
    1,642
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right, thanks.

    But now i have a new problem and i was kinda expecting it.

    It deletes the first entry in the DB and works its waty down, whereas i need it to delete the one where the button is located??
    "Am I the only one doing ASP.NET in Delphi(Pascal)?"

  5. #5
    SitePoint Addict evilone's Avatar
    Join Date
    Oct 2004
    Location
    Estonia
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    add a hidden field to the form and apply somekind of id to it. how your mySQL table looks like?

  6. #6
    orange pips! orange pips!
    Join Date
    Oct 2001
    Location
    Its all about location
    Posts
    652
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Aren't the rows of data in your table unique by username/product name/etc?

  7. #7
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    At My Desk!!
    Posts
    1,642
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the id for the DB is just the users IP i collect it when they enter the site and store it in a variable. So all the items the user has are under 1 id.
    "Am I the only one doing ASP.NET in Delphi(Pascal)?"

  8. #8
    orange pips! orange pips!
    Join Date
    Oct 2001
    Location
    Its all about location
    Posts
    652
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    evilone has the right idea. You'll need to uniquely identify each item in a cart in your DB and put that as a hidden value in the form you are writing out. Along with evilone's suggestion, I would also suggest to add a unique ID field to each entry in the DB as well. Something simple like a field which auto increments for each table insertion would be sufficient for what you want to do.

    Let me know if you have any other questions.


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
  •