//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 is how to properly organize the delete button? In my "delirious code", the cycle scrolls and does not even see if any button was pressed in it. PS I need a fresh idea. (Newbie)

    2 answers 2

    I didn’t really understand it, but as far as I understood in the very principle of the “delete” buttons there is no form: Maybe it’s necessary to start with or something like that

     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>'; 

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

      In general, removing something is best never to do on a GET. You'd better make the first column in the table a checkbox, ohm to select multiple entries, in this case for deletion. And make removal on JS, let's say on the same jQuery. When clicking we ask, "Are you sure you want to delete 5 entries?" if a person chooses "Yes", then create a POST request for a separate script (if you have a file separation) and process POST variables. Such a simplified AJAX is obtained.

      I would do something like that.