Results 1 to 8 of 8

Thread: How to remove?

  1. #1
    Join Date
    Jul 2006
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy How to remove?

    Hello!

    How to remove the entry from database, while viewing it from web




    k

  2. #2
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    You can view the following tutorials/examples based on that you can tackle your problem (I assume that you are using PHP & MySQL).

    1. PHP-MySQL Tutorial

    2. MySQL Tutorial

  3. #3
    Join Date
    Jul 2006
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow

    Quote Originally Posted by codeexploiter View Post
    You can view the following tutorials/examples based on that you can tackle your problem (I assume that you are using PHP & MySQL).

    1. PHP-MySQL Tutorial

    2. MySQL Tutorial

    Hi!

    Its my outpage code

    Code:
    <?
    
    $host = 'localhost'; 
    $username="";
    $password="";
    $database="";
    
    mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    $query="SELECT * FROM comment";
    $result=mysql_query($query);
    
    $num=mysql_numrows($result);
    
    mysql_close();
    
    echo "<b><center>Database Output</center></b><br><br>";
    
    $i=0;
    while ($i < $num) {
    
    $name=mysql_result($result,$i,"name");
    $number=mysql_result($result,$i,"number");
    $comment=mysql_result($result,$i,"comment");
    
    
    echo "<table cellspacing='0' cellpadding='0' width='100%' border='1'>\n<tr>"; 
    echo "<tr><td width=\"15%\">Name</td><td width=\"15%\">Number</td><td 
    
    width=\"70%\">Comments</td></tr><tr>"; 
    
    { 
    echo "<td width='15%'>".$name."</td>"; 
    echo "<td width='15%'>".$number."</td>"; 
    echo "<td width='70%'>".$comment."</td>"; 
    echo "</tr><tr><td colspan='3'>&nbsp;</td></tr><tr>"; 
    } 
    echo "</tr></table>"; 
    
    
    $i++;
    }
    
    ?>
    When this page loads, it displays all records of database.
    1 want to use 4th column in which there will be option to delete the single entry, just like in phpMYadmin

    Preview:-


    The above 2 mentioned sites didn;t helpful!

    K

  4. #4
    Join Date
    Jul 2006
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    you could set up the fourth column to have links like so :
    HTML Code:
    <a href="delete_entry.php?name=<?=$name;?>">delete</a>
    Now this assumes that every name is unique. If they are not, its better to go with the primary key/id for that field, because you want to delete ONLY that unique field.The links will pass the name id via GET to "delete_entry.php"
    Your delete_entry.php will look something like this :
    PHP Code:
    $name $_GET[name];
    $query "DELETE FROM comment WHERE name=$name";
    $result mysql_query($query) or die ("Error in query: $query.".mysql_error()); 
    The execution of delete_entry.php will delete the row from your table.
    You can also just put this code on the top of the same page, then use an
    if(isset($name))
    to check if the $name var is set, then run that code. Then the page will continue and reload your updated table:

    If you need the code, let me know, but eh , at first glance, your table looked a little confusing so I didn't try to update your code! sorry. haha
    Also, in your echo statements, its better to use ' ' wherever possible.
    For example for regular html entries where you don't need any parsing, its best to u ' ' rather than " " (php has to parse EVERYTHING within " " )

  5. #5
    Join Date
    Jul 2006
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi!

    Quote Originally Posted by superjadex12 View Post
    If you need the code, let me know, but eh , at first glance, your table looked a little confusing so I didn't try to update your code! sorry. haha
    Also, in your echo statements, its better to use ' ' wherever possible.
    For example for regular html entries where you don't need any parsing, its best to u ' ' rather than " " (php has to parse EVERYTHING within " " )

    The database is looking confusing bc it is showing 4 columns, but the code mentioned is meant for 3 columns, Indeed the above pic! I just pasted,
    to show what i want!



    Can you Give me Modified code for it?

    K

  6. #6
    Join Date
    Jul 2006
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow

    BUZZZZZZ

  7. #7
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    I will explain this thing with the help of some source code. So that you can use the same technique used in my source code in your case.

    Please note that the database, table, fieldnames used in my source code might be different from your source code.

    The first php page will extract the database records and display them into the page. Along with the table fields there will be another field which will be an hyperlink based on an image

    PHP Code:
    <?php
    class database
    {

            private 
    $db_handle;
        private 
    $user_name;
        private 
    $password;
        private 
    $data_base;
        private 
    $host_name;
        private 
    $sql;
        private 
    $results;

        function 
    __construct($host="localhost",$user,$passwd)
        {
            
    $this->db_handle mysql_connect($host,$user,$passwd);
        }

        function 
    dbSelect($db)
        {
            
    $this->data_base $db;
            if(!
    mysql_select_db($this->data_base$this->db_handle))
            {
                
    error_log(mysql_error(), 3"/phplog.err");
                die(
    "Error connecting to Database");
            }
        }
        
        function 
    executeSql($sql_stmt)
        {
            
    $this->sql $sql_stmt;
            
    $this->result mysql_query($this->sql);
        }
        function 
    returnResults()
        {
            return 
    $this->result;
        }
    }

    //$host = "jp";
    $user "root";
    $passwd "";
    $db "newTest"
    $sql "SELECT * FROM EMP ORDER BY empname";
    $dbObject = new database($host,$user,$passwd);
    $dbObject->dbSelect($db);
    $dbObject->executeSql($sql);
    //$dbObject->outputGenerate();

    $res $dbObject->returnResults();
    echo 
    "<table cellspacing='0' cellpadding='0' width='600' border='1'>\n<tr>";
    echo 
    "<td width='100'>Employee ID</td>";
    echo 
    "<td width='100'>Employee Name</td>";
    echo 
    "<td width='100'>Employee Sal</td>";
    echo 
    "<td width='100'>Employee Age</td>";
    echo 
    "<td width='100'>Employee Comm</td>";
    echo 
    "<td width='100'>Delete</td>";
    echo 
    "</tr><tr>";

    while(
    $record mysql_fetch_object($res))
    {
        echo 
    "<td width='100'>".$record->empid."</td>";
        echo 
    "<td width='100'>".$record->empname."</td>";
        echo 
    "<td width='100'>".$record->empsal."</td>";
        echo 
    "<td width='100'>".$record->empage."</td>";
        echo 
    "<td width='100'>".$record->empcomm."</td>";
        echo 
    "<td width='100'><a href='[U]delete.php?id=".$record->empid.[/U]"'><img src='1.jpg' alt='delete' border='0'></a></td>";
        echo 
    "</tr><tr><td colspan='5'>&nbsp;</td></tr><tr>";
    }
    echo 
    "</tr></table>";
    ?>
    Code:
    echo "<td width='100'><a href='delete.php?id=".$record->empid."'><img src='1.jpg' alt='delete' border='0'></a></td>";
    In the abov line which displays the delete button in the above source code is in which 1.jpg is the cross image which meant for delete.

    If you look at the above mentioned line (red color)
    Code:
    href='delete.php?id=".$record->empid."'
    It actually refers another file (delete.php) and passes a querystring parameter into it which is id which has the value of empid (which is using as the unique identifier to identify my records). In your case you can pass your unique identifier, If you pass a name there is a chance of deleting more than one records because there is a chance of having multiple records with the same name.

    The second source code which I am going to paste here is delete.php

    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body>
    <?php
    class database
    {
        private 
    $db_handle;
        private 
    $user_name;
        private 
    $password;
        private 
    $data_base;
        private 
    $host_name;
        private 
    $sql;
        private 
    $results;

        function 
    __construct($host="localhost",$user,$passwd)
        {
            
    $this->db_handle mysql_connect($host,$user,$passwd);
        }

        function 
    dbSelect($db)
        {
            
    $this->data_base $db;
            if(!
    mysql_select_db($this->data_base$this->db_handle))
            {
                
    error_log(mysql_error(), 3"/phplog.err");
                die(
    "Error connecting to Database");
            }
        }
        
        function 
    executeSql($sql_stmt)
        {
            
    $this->sql $sql_stmt;
            
    mysql_query($this->sql);
        }
    }

    //$host = "jp";
    $user "root";
    $passwd "";
    $db "newTest"
    $id $_GET['id'];
    $sql "DELETE FROM EMP WHERE empid=$id";
    $dbObject = new database($host,$user,$passwd);
    $dbObject->dbSelect($db);
    $dbObject->executeSql($sql);

    echo 
    "<script type='text/javascript'>location.href='db1.php';</script>";
    ?>
    </body>
    </html>
    Code:
    $id = $_GET['id'];
    The above line gets the unique identifier from the first page which here is the employee id using which we are going to delete a record from the employee table.

    Code:
    $sql = "DELETE FROM EMP WHERE empid=$id";
    The above one is the DELETE SQL query using which we delete the record of a particular employee.

    Code:
    echo "<script type='text/javascript'>location.href='db1.php';</script>";
    This line is for redirecting from delete.php page to the database record display page (in my case it is db1.php).

    If you still have any doubt about how to implement this operation plz let me know about it.

  8. #8
    Join Date
    Jul 2006
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow

    Hi!

    I'm currently out of country! Will be Back Soon!

    & Test & Reply !

    K

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
  •