Need help create a form that will delete record from Database using php

Discussion in 'PHP' started by rhoula, Dec 25, 2012.

  1. #1
    This should be very easy for someone that is familiar with PHP and MySQL.

    I need a form where I enter the record "id" and then the script asks me if I'm sure I want to delete the record from the database, then proceeds to deleting it from the database.

    Your help would be very appreciated.

    This is what I have so far:

    
    <?php
    $dbhost = 'localhost';
    $dbuser = 'username';
    $dbpass = 'Pasword';
    $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    if(! $conn )
    {
      die('Could not connect: ' . mysql_error());
    }
    $sql = 'DELETE FROM alexa
            WHERE id=39';
    
    mysql_select_db('database');
    $retval = mysql_query( $sql, $conn );
    if(! $retval )
    {
      die('Could not delete data: ' . mysql_error());
    }
    echo "Deleted data successfully\n";
    mysql_close($conn);
    ?>
    
    PHP:
     
    Solved! View solution.
    Last edited: Dec 25, 2012
    rhoula, Dec 25, 2012 IP
  2. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #2
    Can you also post the HTML part of where its asking the user for the ID or is this a CLI script?
     
    ThePHPMaster, Dec 25, 2012 IP
  3. rhoula

    rhoula Well-Known Member

    Messages:
    875
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    145
    #3
    Actually that is the part that I need made. If it could all fit in the same script that would be great.

    Thank you
     
    rhoula, Dec 25, 2012 IP
  4. #4
    Refer this

    <script type="text/javascript">
    function check(){
    var cnfrm = confirm("Are you sure?");
    if(cnfrm){
    return true;
    }else{
    return false;
    }
    }
    </script>


    <?php
    $dbhost = 'localhost';
    $dbuser = 'username';
    $dbpass = 'password';
    $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    if(! $conn )
    {
    die('Could not connect: ' . mysql_error());
    }
    mysql_select_db('database');


    if(isset($_POST['submit'])){


    $id = $_POST['id'];
    $sql = "DELETE FROM alexa WHERE id=$id";
    $retval = mysql_query( $sql, $conn );
    if(! $retval )
    {
    die('Could not delete data: ' . mysql_error());
    }


    echo "Deleted data successfully\n";
    mysql_close($conn);


    }
    ?>


    <form action="" method="post">
    <label>Id:</label><input type="text" name="id" />
    <input type="submit" value="submit" name="submit" onclick="return check();"/>
    </form>
     
    avinash gaud, Dec 25, 2012 IP
  5. rhoula

    rhoula Well-Known Member

    Messages:
    875
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    145
    #5
    Thank you so much once again for your help. It's working exactly like I want it.

    Have a blessed day :)
     
    rhoula, Dec 26, 2012 IP