2

I am creating a user interface for managing a database. Each entry has two radio buttons, one for validation (and subsequent emailing to concerned authorities) and one for deleting the entry. The code is given below, and the bold parts are the ones which concern this functionality. A couple of questions: 1) Is it possible to do so without the form attribute? If so, how would I do it?

2) With the form attribute, I use 'handle.php' to delete each entry. The syntax in the 'handle.php' includes this line

$sql="DELETE FROM rti WHERE ID=x"; //x here is the ID number to delete.

Now, how do I pass the value of the ID as well from my interface, so that the above line of code deletes the entry corresponding to the button that was pressed?

<html>
    <head>
        <style>
        * {
            margin: 0px;
            padding: 0px;
        }

        div.topbar {
            position: relative;
            background-color: black;
            height: 45px;
            text-align: center;
            font-family: Calibri;
            font-size: 30px;
            padding-top: 10px;
        }

        div.topbar img {
            position: absolute;
            left: 10px;
            top: 0px;
        }

        div.topbar span {
            color: white; 
        }

        div.container {
            background-color: #a5a5a5;
            width: 100%;
            height: 100%;
            color: white;
        }

        div.data {
            padding: 20px;
        }

        table {
            background-color: #b1b1b1;
            border-collapse: collapse;
        }


        th, td {
            text-align: center;
            border: solid 1px black;
            margin: 0px;
        }

        th.one {
            width: 50px;
        }

        th.two{
            width: 150px;
        }

        th.three {
            width: 75px;
        }

        th.four {
            width: 200px;
        }

        div.validation {
            background-color: #b5b5b5;
            width: 200px;
            height: 100%;
            position: relative;
            left: 1100px;
            bottom: 300%;
            text-align: center;
            border-left: solid 1px black;
        }
        </style>
    </head>
    <body>
        <div class = "topbar"><img src="logo.png"><span>RTI DATABASE</span></div> 
            <div class = "container">
                <div class ="data"> 
                    <?php 
                    $servername="localhost";
                    $username="root";
                    $password='';
                    $conn=mysql_connect($servername, $username, $password) or die ("Error connecting to mysql server: ".mysql_error());
                    $dbname = 'bsp';   
                    mysql_select_db($dbname, $conn) or die ("Error selecting specified database on mysql server: ".mysql_error());
                    $sql="SELECT * FROM rti";
                    $result=mysql_query($sql) or die ("Query to get data from firsttable failed: ".mysql_error());   
                    echo "<table>";
                    echo "<tr>";
                    echo '<th class="one">ID</th>
                        <th class="two">Name</th>
                        <th class="three">Board</th>
                        <th class="four">Query</th>
                        <th class="five">Validate</th>
                        <th class="six">Delete</th>
                        <th class="seven">Submit</th>';

                    echo "</tr>";
                    while ($row=mysql_fetch_array($result)) {
                        $id=$row["ID"];
                        $name=$row["name"];
                        $board=$row["board"];
                        $query=$row["query"];
                        echo "<tr>";
                        echo "<td>$id</td>
                            <td>$name</td>
                            <td>$board</td>
                            <td>$query</td>
                           **<form action='handle.php' method='POST'>
                                <td><input type='radio' name='option' value='validate'></td>
                                <td><input type='radio' name='option' value='delete'></td>
                                 <td><input type='submit' value='Submit' name='submit'>
                            </form></td>";
                        echo "</tr>";**
                    }
                echo "</table><br>";
                ?>
            </div>
        </div>
    </body>
</html>
2
  • Is it required use form tag over table ? You can try with <a> tag also.. Commented Jun 16, 2016 at 5:30
  • 2
    Stop using deprecated mysql_* API. Use mysqli_* or PDO Commented Jun 16, 2016 at 5:31

3 Answers 3

1
**<form action='handle.php' method='POST'><td><input type='radio' name='option' value='validate'></td>
   <td><input type='radio' name='option' value='delete'>
  <input type='hidden' name="id" value='".$id."'> 
  </td>
   <td><input type='submit' value='Submit' name='submit'></form></td>";

In handle.php:

$id = $_POST['id'];
Sign up to request clarification or add additional context in comments.

6 Comments

I tried your solution, but the handle.php gives me the error Undefined index: id, corresponding to the line you suggested. Any suggestions?
make sure id is in your post request? print_r($_POST);
Also, one thing that you may have missed out in my code. It's also causing confusion to me. I've used double quotes (" ") for the echo statement, and inside, for the html attributes like method, name and value, I've used single quotes. I guess that could create problems.
I checked. ID was not in the post request. Here's the code. It's not complete, in the sense that it doesn't have functionality for the validate button. But it should still delete the entry <?php $servername = "localhost"; $username = "root"; $password = ''; $dbname = "bsp"; $conn = new mysqli($servername, $username, $password, $dbname); $id=""; print_r($_POST); $id = $_POST['id']; $sql="DELETE FROM rti WHERE ID=$id"; if ($conn->query($sql) === TRUE) { echo "Record deleted successfully"; } else { echo "Error deleting record: " . $conn->error; } $conn->close(); ?>
I got it working. Thanks so much. There was a small issue in the syntax of my code. Thanks a lot for your help sir :)
|
1

inside your form add an hidden type input with the id value for each item , so each time user click the button the id sent. just like this :

 <form action='handle.php' method='POST'><td><input type='radio' name='option' value='validate'></td>

<input type = 'hidden' name="id" value='".$row['id']."'><td>
            <input type='radio' name='option' value='delete'></td>
        <td><input type='submit' value='Submit' name='submit'></form></td>";

Comments

0

use something like <input type='radio' name='option[your_id]' value='validate'> to identify the item. Like: <input type='radio' name='option[$id]' value='validate'>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.