WEB Halp me with PHP

Discussion in 'OT Technology' started by Mint, Aug 25, 2009.

  1. Mint

    Mint Active Member

    Joined:
    Jun 22, 2007
    Messages:
    5,208
    Likes Received:
    0
    Location:
    Southern Cali
    Okay so i'm trying to return all the values for "data" within the rows of a table with a delete button next to them.

    It all works so far, except the delete button doesnt delete :(

    Code:
    mysql_select_db("alinco_alincosite", $con);
       $result = mysql_query("select * from news order by id desc");
       $id = $_GET['ID'];
       if (isset($id))
       {
       mysql_query("DELETE FROM news WHERE ID = '$id'");
       echo "Post deleted!";
       }
       while($r=mysql_fetch_array($result))
       {
       echo $r["data"];
       echo "<a href=\"?del=".$r['ID']."\">Delete</a><br />";
       }
     
    Last edited: Aug 25, 2009
  2. brds

    brds OT Supporter

    Joined:
    Jun 26, 2006
    Messages:
    17,643
    Likes Received:
    13
    Location:
    Atlanta
    Wrong wrong wrong ;)... lemme type something up real quick

    EDIT:
    Assuming you have all the records in a 2d array:

    Code:
    <?php
    
    if (!isset($_POST['deleteid'])
    {
    echo "
    <table cellspacing='0' cellpadding='0' width='100%'>
          <tr>
                <td><strong>Column1:</strong></td>
                <td><strong>Column2:</strong></td>
                <td><strong>Column3:</strong></td>
                <td><strong>Column4:</strong></td>
                <td><strong>&nbsp;</strong></td>
          </tr>";
    for ($i = 0; $i < count($arrayName); $i++)
          echo "
                <tr>
                      <td>" . $arrayName[$i]['field1Name'] . "</td>
                      <td>" . $arrayName[$i]['field2Name'] . "</td>
                      <td>" . $arrayName[$i]['field3Name'] . "</td>
                      <td>" . $arrayName[$i]['field4Name'] . "</td>
                      <td><form action='thispage.php' method='post'><input type='hidden' name='deleteid' value='" . $arrayname[$i]['uniqueID'] . "' /><input type='submit' value='Delete &gt;&gt;' /></form></td>
                </tr>
          ";
    echo "
    </table>
    ";
    }
    else
    {
          $deleteid = $_POST['deleteid'];
          mysql_query("DELETE FROM news WHERE ID = $deleteid");
          echo "<script language=\"javascript\">
                window.location='PAGE_YOU_WANT_TO_FORWARD_THEM_TO..._PROBABLY_BACK_TO_THE_ORIGINAL';
                  </script>";
    }
    ?>
    
    *NOTE: i did this freehand, so there might be a stupid mistake like a missing ' or ; somewhere
     
    Last edited: Aug 25, 2009
  3. brds

    brds OT Supporter

    Joined:
    Jun 26, 2006
    Messages:
    17,643
    Likes Received:
    13
    Location:
    Atlanta
    You should also look into getting a DBconnector class... It makes connecting/running queries/disconnecting easier and more safe.

    I can post the one i made if you and and show you how to use it real quick.
     
  4. Mint

    Mint Active Member

    Joined:
    Jun 22, 2007
    Messages:
    5,208
    Likes Received:
    0
    Location:
    Southern Cali
    figured it out bro.

    changed my last echo to

    Code:
    echo "<a href=\"?ID=".$r['ID']."\">Delete</a><br />";
     
  5. pharmokan

    pharmokan OT Supporter

    Joined:
    Oct 18, 2002
    Messages:
    102,125
    Likes Received:
    423
    Location:
    L.A.
    :rofl: @ cookie monster wasting his time on something TS didnt end up using anyways
     
  6. Mint

    Mint Active Member

    Joined:
    Jun 22, 2007
    Messages:
    5,208
    Likes Received:
    0
    Location:
    Southern Cali
    :wtc:
     
  7. brds

    brds OT Supporter

    Joined:
    Jun 26, 2006
    Messages:
    17,643
    Likes Received:
    13
    Location:
    Atlanta
  8. hank85

    hank85 sudo shred /dev/sda -f -v -z --iterations=6

    Joined:
    Jul 23, 2008
    Messages:
    4,360
    Likes Received:
    0
    Code:
    echo '<a href="?ID=' . $r['ID'] . '">Delete</a><br />';
    For simplicity if you're going to echo out double quotes use single quotes to encapsulate.
     
  9. hank85

    hank85 sudo shred /dev/sda -f -v -z --iterations=6

    Joined:
    Jul 23, 2008
    Messages:
    4,360
    Likes Received:
    0

    Also for outputting large chunks of data I would use:

    Code:
    [COLOR=Black]echo <<<END
    This uses the "here document" syntax to output
    multiple lines with $variable interpolation. Note
    that the here document terminator must appear on a
    line with just a semicolon. no extra whitespace!
    END;[/COLOR]
     
  10. intrktevo

    intrktevo New Member

    Joined:
    Oct 18, 2004
    Messages:
    5,781
    Likes Received:
    0
    Location:
    UCF
    sanitize your inputs!!

    $id = $_GET['ID'];
    if (isset($id))
    {
    mysql_query("DELETE FROM news WHERE ID = '$id'");


    casting to an int would help.
    $id = (int) $_GET['ID'];
     
  11. retorq

    retorq What up bitch??

    Joined:
    Dec 14, 2006
    Messages:
    6,061
    Likes Received:
    0
    Location:
    Mohave Desert
    You should be using some "|| DIE" statements on those queries so when shit doesn't work you can track it down alot easier. Just saying ...
     

Share This Page