Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Feb 2010
    Posts
    74
    Thanks
    9
    Thanked 0 Times in 0 Posts

    How do i edit and delete records from database?

    Im running an online march madness pool, and i want the users to be able to edit their bracket or delete it entirely.

    I already have the submit bracket and view bracket thing down. i just need for them to edit what has already been submitted to the database.

    i have read some tutorials but i still havent been able to get it to work. any guidance or code of working examples?

    thanks

  2. #2
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,125
    Thanks
    10
    Thanked 1,162 Times in 1,153 Posts
    How about showing us some of the code you have.
    X-out any passwords you have for your MySQL.

  3. #3
    New Coder
    Join Date
    Feb 2010
    Posts
    74
    Thanks
    9
    Thanked 0 Times in 0 Posts
    PHP Code:
    <html>
    <body>
    <?php
    //connect to mysql
    //change user and password to your mySQL name and password
    mysql_connect("localhost","root","xxxx");
        
    //select which database you want to edit
    mysql_select_db("ipool"); 

    //If cmd is not hit
    if(!isset($cmd)) 
    {
       
    //display all the news
       
    $result mysql_query("select * from news"); 
       
       
    //run the while loop that grabs all the news scripts
       
    while($r=mysql_fetch_array($result)) 
       { 
          
    //grab the title and the ID of the enws
          
    $title=$r["title"];//take out the title
          
    $id=$r["id"];//take out the id
         
          
    echo "<a href='delete.php?cmd=delete&id=$id'>$title - Delete</a>";
          echo 
    "<br>";
        }
    }

    if(
    $cmd=="delete")
    {
        
    $sql "DELETE FROM news WHERE id=$id";
        
    $result mysql_query($sql);
        echo 
    "Row deleted!";
    }
    ?>


    </body>
    </html>
    thats what i have for deleting, which i cant get to work either. it shows the list and goes to the automated url correctly, but it wont actually delete the row.
    im using the same cmd==update to edit the rows as well and thats getting me just as far as the delete page is

  4. #4
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,125
    Thanks
    10
    Thanked 1,162 Times in 1,153 Posts
    Missing the $_GET for getting the variable from the URL ...

    Try this:
    PHP Code:
    <html>
    <body>
    <?php
    //connect to mysql
    //change user and password to your mySQL name and password
    mysql_connect("localhost","root","xxxx");
        
    //select which database you want to edit
    mysql_select_db("ipool"); 

    // GET variable(s) from your URL.
    $cmd=$_GET['cmd'];
    $id=$_GET['id'];

    //If cmd is not hit
    if(!isset($cmd)) 
    {
       
    //display all the news
       
    $result mysql_query("select * from news"); 
       
       
    //run the while loop that grabs all the news scripts
       
    while($r=mysql_fetch_array($result)) 
       { 
          
    //grab the title and the ID of the enws
          
    $title=$r["title"];//take out the title
          
    $xid=$r["id"];//take out the id, use $xid because $id is used for another purpose.
         
          
    echo "<a href='delete.php?cmd=delete&id=$xid'>$title - Delete</a>";
          echo 
    "<br>";
        }
    }

    if(
    $cmd=="delete")
    {
    // Always sanitize any variables you use in your query - to prevent SQL injections.
    $id mysql_real_escape_string($id);

        
    $sql "DELETE FROM news WHERE id=$id";
        
    $result mysql_query($sql);
        echo 
    "Row deleted!";
    }
    ?>


    </body>
    </html>
    Last edited by mlseim; 03-11-2010 at 03:21 AM.

  5. #5
    New Coder
    Join Date
    Feb 2010
    Posts
    74
    Thanks
    9
    Thanked 0 Times in 0 Posts
    yea ino, i was just about to post to say i got it working now

    thanks tho

  6. #6
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,125
    Thanks
    10
    Thanked 1,162 Times in 1,153 Posts
    Make sure you do this to any variables in your query ...

    // Always sanitize any variables you use in your query - to prevent SQL injections.
    $id = mysql_real_escape_string($id);


 

Posting Permissions

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