SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot Para's Avatar
    Join Date
    Mar 2005
    Location
    London UK
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How can i add a delete button to my script?

    Hi i want to add a delete button to the following script, to delete rows from the database, ive tried a few things, but no joy can anyone point me in the right direction.

    I want the button to be below this line

    <a href="http://collect.myspace.com/index.cfm?fuseaction=invite.addfriend_verify&friendID=<?=strip_tags($items[$i]['displayID']);?>" target="_blank">Add To Friends</a>
    Thanks for any help in advance, im only just getting to grips with php unfortunatly.

    PHP Code:
    <?php

    require 'db.php';

    if ( isset(
    $_GET['p']) && is_numeric($_GET['p']) )
    {
        
    $page $_GET['p'];
    }
    else
    {
        
    $page 1;
    }
    $limit 12;
    $start = ( $page $limit ) - $limit;

    $fetch mysql_query("SELECT * FROM profiles ORDER BY id DESC LIMIT $start$limit") or die(mysql_error());
    $numRows mysql_num_rows($fetch);
    while ( 
    $row mysql_fetch_assoc($fetch) )
    {
        
    $items[] = $row;
    }

    $maxRow 1;
    $totalRows ceil $numRows $maxRow );

    $fetch mysql_query("SELECT id FROM profiles");
    $totalPages ceil(mysql_num_rows($fetch) / $limit);
    $itemCount=0;
    $percent ceil 100 $maxRow );

    echo 
    '';

    for ( 
    $a=1$a<=$totalRows$a++ )
    {
        
    ?>
        
        
        
        <?
        $start 
    = ( $a ) - 1;
        for ( 
    $i=$start$i<$start+1$i++ )
        {
            
    ?>
            
            
            
                <?
                    
    if ( $items[$i] != NULL )
                    {
                    
    ?>
            <div class="left_articles">    <table width="100%" border="0" cellspacing="2" cellpadding="2"><tr><td width="20%">
                
    <img src="<?=$items[$i]['displayPhoto'];?>" width="60" height="60" alt="Image" title="Image" class="image" />

    </td>
                        <td width="60%">
                                        





        <strong>Name:</strong> <?=strip_tags($items[$i]['displayName']);?><br />    
        <strong>Age:</strong> <?=strip_tags($items[$i]['displayAge']);?><br />
        <strong>Gender:</strong> <?=strip_tags($items[$i]['displayGender']);?><br />  



        <strong>Country:</strong> <?=strip_tags($items[$i]['displayCountry']);?><br /> 
        
    </td>
    <td width="20%">
    <b><a href="<? echo ("$url"); ?>index.php?app=profile-train&action=view&id=<?=$items[$i]['id'];?>" target="_blank">View Profile</a><br />  
    <a href="http://collect.myspace.com/index.cfm?fuseaction=invite.addfriend_verify&friendID=<?=strip_tags($items[$i]['displayID']);?>" target="_blank">Add To Friends</a></b>
    <strong>Profile views:</strong> <?=strip_tags($items[$i]['totalViews']);?>
    <br />  
    </td>

    </tr></table></div>
                
                    <?        
                    
    }
                
    ?>
                
                    
                    
            
            <?
        
    }    
        
        
    ?>
        
        
        
        <?

    }

    echo 
    '';

    if(
    $page 30==0){
    echo 
    "<br>";


    if ( 
    $page )
    {
        
    $prev = ( $page );
        
    $links .= '<strong><a href="index.php?app=profile-train&p='.$prev.'">&lt;&lt;Prev</a>&nbsp;</strong>';
    }
    for ( 
    $i 1$i <= $totalPages$i++ )

        if ( 
    $page == $i )
        { 
            
    $links .= '<strong>['.$i.']</strong>&nbsp;'
        }
        else
        {
            
    $links .= '<a href="index.php?app=profile-train&p='.$i.'">'.$i.'</a>&nbsp;'
        } 
    if(
    $i%30==0){
    $links .= "<br>";








    if( 
    $page $totalPages )

        
    $next $page 1
        
        
    $links .= '<strong><a href="index.php?app=profile-train&p='.$next.'">Next&gt;&gt;</a></strong>'
    }         
    $links '<table><strong>Total Pages ('.$totalPages.')</strong></table> <table>'.$links.'</table><br clear="all" />';




    ?>
    <div class="left_articles"><div align="center"><?=$links;?></div></div>

  2. #2
    SitePoint Zealot Para's Avatar
    Join Date
    Mar 2005
    Location
    London UK
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    After scouring the web, i figure i have to make a seperate file called delete.php with the following code

    PHP Code:
    <?php
    $con 
    mysql_connect("localhost","test","test");
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }

    mysql_select_db("my_db"$con);

    mysql_query("DELETE FROM profiles WHERE id='i dont no what to put here'");

    mysql_close($con);
    ?>
    Im just unsure what to put in WHERE id=

    Also how would my delete button link? would it be something like

    Code:
    delete.php?id=<?=$row->id;?>
    Sorry to be a pain in the butt, im just getting to grips with things.

  3. #3
    SitePoint Addict caddyfixr's Avatar
    Join Date
    Aug 2004
    Location
    Albuquerque, NM
    Posts
    249
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    id would be the record in the database you want to delete.
    Here is how I am doing it
    PHP Code:
    <?php
     
    // Delete all team info
    $id $_GET['id'];
    $ok = @mysql_query("DELETE FROM Team WHERE ID='$id'");

    if (
    $ok) {
    include 
    'adminheader.php';
    echo(
    "<div align='center'>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>

    <p><font size='6' face='Arial, Helvetica, sans-serif'><ul><strong>Processing your request, one moment.</strong></font><br>
    <img src=\"../images/animated_timer_bar.gif\" width=\"220\" height=\"18\"></p></div> 
    <meta http-equiv=\"refresh\" content=\"1;URL=choosedelete.php\">"
    );      

      } else { 
      echo(
    '<p>Error deleting author from database!<br />'.
           
    'Error: ' mysql_error() . '</p>');
    }

    ?>
    I send the pass the id with this page
    PHP Code:
     <?php
    $Teams6 
    = @mysql_query("SELECT * 
                           FROM Team
                           WHERE 1 AND division = '10U'
                           ORDER BY teamname"
    );
    if (!
    $Teams6) {
      die(
    '<p>Error retrieving Teams6 from database!<br />'.
          
    'Error: ' mysql_error(). '</p>');
    }

    while (
    $Team mysql_fetch_array($Teams6)) {
      
    $id   $Team['ID'];
      
    $teamname $Team['TeamName'];
      
    $userid $Team['userid'];
      
    $date $Team['date'];
      
    $password $Team['password'];
      
    $teamage $Team['teamage'];
      
    $teamcity $Team['teamcity'];
      
    $teamstate $Team['teamstate'];
      
    $contactname $Team['contactname'];
      
    $contactlname $Team['contactlname'];
      
    $contactphone $Team['contactphone'];
      
    $contactemail $Team['contactemail'];
      
    $position1 $Team['position1'];
      
      if (
    $date == '') {$signup_date '';} else {$signup_date date("m-d-y",strtotime("$date"));}  
      if (
    $contactphone=='') {$contactphone $contactcphone;}
      
      
      echo(
    "<table>
      <tr>
        <td width='66'><font size='-1'>
    $position1 <a href='delete.php?id=$id' onclick='return confirmDelete()' class='st_links'>DELETE</a></font></td> 
        <td width='66'><font size='-1'>
    $signup_date</font></td>
        <td width='160'><b><font size='-1'>
    $teamname</font></b></td>
        <td width='100'><font size='-1'>
    $contactname $contactlname</font></td>
        <td width='95'><font size='-1'>
    $contactphone</font></td>
        <td width='150'><font size='-1'><a href='mailto:
    $contactemail'>$contactemail</a> </font></td>
      </tr>
    </table>"
    );
    }
    ?>

  4. #4
    SitePoint Zealot Para's Avatar
    Join Date
    Mar 2005
    Location
    London UK
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks caddyfixr ill look into this option.

    I have my delete link setup now

    Code:
    <a href="index.php?app=admin&action=delete_profile&id=<?=$items[$i]['id'];?>">Delete Profile</a>
    Its point to page delete_profile.php but the question is how will pass the id into my delete page.

    Im going to fiddle more, any input is apprciated.

    Many regards

  5. #5
    SitePoint Zealot Para's Avatar
    Join Date
    Mar 2005
    Location
    London UK
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OMG OMG OMG

    Your code worked!!!

    You have actually changed my knowledge for the better, thanks!

    PHP Code:
    <?php
    require 'plugins/profile-train/db.php';
    $id $_GET['id'];
    $ok = @mysql_query("DELETE FROM profiles WHERE ID='$id'");

    if (
    $ok) {

    echo(
    "Profile Deleted");      

      } else {
      echo(
    '<p>Error deleting profile from database!<br />'.
           
    'Error: ' mysql_error() . '</p>');
    }

    ?>

  6. #6
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    keep in mind its preferable to use post as the request method instead of get if it will cause an operation to make a change to the state of the world. in your case, it does make a change because it deletes a database record. a hyperlink uses get as the request method.
    http://www.cs.tut.fi/~jkorpela/forms/methods.html

    imagine what would happen if a crawler like google got a hold of your links. your gonna lose lots of db records. crawlers wont submit forms though.

    also consider what will happen if a user refreshes the page. if you used post, users will receive a warning from thier browser asking them if they really want to resubmit, and may make them think twice.

    just something to keep in mind.

  7. #7
    SitePoint Zealot Para's Avatar
    Join Date
    Mar 2005
    Location
    London UK
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, i ust wanted to get the basic code really, im just learning php and things like this are foreign to me, but im learning more and more everyday.

    The current links are in a secure admin area so theres no chance of any miss use, but i am going to use forms for this anyway, but thats something else i have to learn

    Thanks


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
  •