www.webdeveloper.com
Results 1 to 2 of 2

Thread: Confrim function for delete

  1. #1
    Join Date
    Jul 2013
    Posts
    1

    Confrim function for delete

    Hi there, i basically want a confirmation box to appear when a button is clicked, and ask the user whether they are sure if they want to delete a row from the database.

    However, with the code i constructed now, whether ok or canel is clicked, the row is deleted anyway, If you can have a look over my code below and see why this is happening i will very much appreciate it thank you.


    html>

    <head>
    <link rel='stylesheet' href='layoutcms.css'>

    <head/>


    <script language='JavaScript'>
    function myFunction()
    {
    var x;
    var r=confirm("Press a button!");
    if (r==true)
    {
    x=$query;
    }
    else
    {
    x=query2;
    }
    document.getElementById("demo").innerHTML=x;
    }
    </script>



    <body>


    <div id="header">
    <h1><img src="laz_logocms.png"
    width="380" height="60" alt="Laz Electric Products" border="0" />
    </h1>
    </div>


    <div class='navbar'>
    <div class='button'><a href='add_product.php'>Add Product</a></div>
    <div class='button'><a href='edit_product.php'>Edit Product</a></div>
    <div class='button'><a href='delete_product.php'>Delete Product</a></div>
    <div class='button'><a href='index.php'>Front End</a></div>


    </div>

    <div id="page-container">





    <div id="content">
    <div class="padding">
    <h2>Please select one of the options below by clicking a button.</h2>
    </div>


    <?php
    if (isset($_REQUEST['id'])) {
    $id = $_REQUEST['id'];
    }

    // Connect to server and select database

    require_once "config.php";
    $dbhandle = mysql_connect($hostname, $username, $password);

    if (!$dbhandle) die("unable to connect to mysql: " . mysql_error());

    mysql_select_db("store")
    or die ("unable to connect to databse: " . mysql_error());

    // Get values from form

    if (isset($_POST['delete']) && isset($_POST['id']))
    {

    // Delete data from mysql

    $id = get_post('id');

    $query = "DELETE FROM products WHERE product_id = '$id'";


    // if not successfully deleted data from database, displays message "delete failed".

    if (!mysql_query($query, $dbhandle))
    echo "delete failed: $query<br />" .
    mysql_error() . "<br /><br />";
    }






    // Selects all colums rows from table and displays results

    $query2 = "SELECT * FROM products";
    $result = mysql_query($query2, $dbhandle);

    //if not successful in selected data, display "database access failed"

    if (!$result) die ("Database access failed: " . mysql_error());
    $rows = mysql_num_rows($result);

    //displays all records of tables, rather then just first one, which holds "product_id" 2

    for ($j = '0' ; $j < $rows ; ++$j)
    {

    //returns the rows from table "laptop_products"

    $row = mysql_fetch_row($result);

    // data that needs to be displayed through above, function

    echo <<<_END
    <pre>
    product_id: $row[0]
    product_name: $row[1]
    weight: $row[2]
    quantity: $row[3]
    price: $row[4]
    description: $row[5]
    image: $row[6]
    url: $row[7]
    category: $row[8]
    </pre>

    <form action="delete_product.php" method="post">
    <input type="hidden" name="delete" value="yes" />
    <input type="hidden" name="id" value="$row[0]" />
    <input type="submit" onclick="myFunction()" value="DELETE RECORD" /></form>
    _END;

  2. #2
    Join Date
    Jun 2008
    Posts
    106
    I'm not very familiar with SQL, but it seems you're deleting the row before you even click the button.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles