Delete recording in the DBMS Table

// index.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>test_number_2</title>
</head>
<body>
<form action="test2.php", method="post">
    <div align="center">
        <table>
            <tr bgcolor="#00FF99" align="center">
                <td colspan="2">
                    Table
                </td>
            </tr>
            <tr bgcolor="#00FF99" align="center">
                <td>
                    Name<br>
                    <input type="text" name="Name">
                </td>
                <td>
                    Age<br>
                    <input type="text" name="Age">
                </td>
            </tr>
            <tr>
                <td colspan="2" bgcolor="#00FF99" align="right">
                    <input type="submit" value="" name="onAdd">
                </td>
            </tr>
        </table>
    </div>
</form>
<?php
    require_once "mysql_connect.php";
    $name=$_POST['Name'];
    $age=$_POST['Age'];
    $submit_add=$_POST['onAdd'];
    if(isset($submit_add)){
    $result= mysql_query("INSERT INTO table_one (Id, Name, Age) VALUES ('','$name', '$age')");
    //       result  true
    if($result == 'true') {
        echo "   ";
        }
    else {
        echo "   ";
        }
    }
    echo "<br><br>";
    $result=mysql_query('SELECT * FROM `table_one`');//    
    $del;
    while($row=mysql_fetch_array($result))//     
    { 
        //echo '<p> №:'.$row['Id'].'. : '.$row['Name'].'. : '.$row['Age'].'.</p>';//  
        echo '<table bgcolor="#00CCFF"><tr><td><input type="hidden" name="id_t'.$row['Id'].'" value="№'.$row['Id'].'"></td><td><input type="text" name="name_t'.$row['Id'].'" value=": '.$row['Name'].'"></td><td><input type="text" name="age_t'.$row['Id'].'" value=": '.$row['Age'].'"></td><td><input type="submit" name="delete" value=""></td></tr></table>';
        if(isset($_POST['delete'])) {
            $del=$row['Id'];
            echo $del;
            $delete="DELETE FROM table_one WHERE Id=$del";
            mysql_query($delete);
        }
    }
    echo $del;
?>
</body>
</html>

// mysql_connect.php

<?php ##    MySQL.
$user = "root";
$pass = "";
$db = "db";
//    MySQL. 
mysql_connect("localhost", $user, $pass)
or die("Could not connect: ".mysql_error()) ; 
//   $db —     ! 
//    ,  ,    . 
@mysql_query ( 'CREATE DATABASE $db');
//   $db (     ). 
mysql_select_db($db)
or die("Could not select database: ".mysql_error());
?>

Actually the question, how to properly organize the removal button? In my “delusional code”, the cycle scrolls and not even look if any button is pressed in it.
P.S. I need fresh idea. (Newcomer)


Answer 1, Authority 100%

I did not particularly delve it, but as it understood in the principle of the “Delete” buttons, there is no form:
Maybe you need to start so or something like this

echo '<table bgcolor="#00CCFF"><tr><td><input type="hidden" name="id_t'.$row['Id'].'" value="№'.$row['Id'].'"></td><td><input type="text" name="name_t'.$row['Id'].'" value=": '.$row['Name'].'"></td><td><input type="text" name="age_t'.$row['Id'].'" value=": '.$row['Age'].'"></td><td><form --><input type="hidden" name="id" value="$id"><input type="submit" name="delete" value=""></form></td></tr></table>';

Answer 2

Well, if you need an idea – I will write, but without code:

In general, deleting something is best never done on a GET. It is better for you to make the first column in the table a checkbox, ohm for the possibility of selecting multiple records, in this case for deletion. And delete to do on JS, let’s say on the same JQuery. When pressed, it asks, “Are you sure you want to delete 5 records?” if the person selects “Yes”, then form a POST request for a separate script (if you have file sharing) and process POST variables. Such a simplified AJAX is obtained.

I would do something like this.

Leave a reply

Please enter your comment!
Please enter your name here