简体   繁体   English

从mysql表中删除记录

[英]deleting records from mysql table

Continuing with my simple CRUD, I'm stuck again... 继续我的简单CRUD,我再次陷入困境...

So I have a table created called "usuaris" and a column called "id" which is my auto-increment and then another column called "usuari_nom". 因此,我创建了一个名为“ usuaris”的表和一个名为“ id”的列,这是我的自动增量,然后是另一个称为“ usuari_nom”的列。 Now, I want to add "delete function", so when I am displaying the records of my table I've added a to delete it: 现在,我想添加“删除功能”,所以当我显示表的记录时,我添加了一个删除它:

 <div id="main">
        <?php 

        global $conn;

    $query = "SELECT * FROM usuaris";

    if($grup_usuaris = mysqli_query($conn, $query)) {
         echo "<table>";
         echo "<tr><th>Usuaris</th><th>Accions</th></tr>";
             while($row = mysqli_fetch_assoc($grup_usuaris)) {
                 echo "<tr><td>" . $row['usuari_nom'] . "</td><td><a href=\"elimina_usuari.php?subject=" . $row['id'] . "\">Eliminar usuari</a></td></tr>";

             }
         echo "</table>";
         echo "<a href=\"nou_usuari.php\">+ Afegeix Usuari</a>";
         mysqli_free_result($grup_usuaris);
    } else {
        echo "query failed";
        echo("Error description: " . mysqli_error($conn));
    }                 
        ?>
    </div>

So now, If I click on "eliminar usuari" it goes to the file where I am adding the query to delete, plus the id of that user; 所以现在,如果我单击“ eliminar usuari”,它将转到我要添加要删除的查询的文件,以及该用户的ID; for example: " http://localhost/calendario/elimina_usuari.php?subject=6 ". 例如:“ http://localhost/calendario/elimina_usuari.php?subject = 6 ”。 But then, in the file elimina_usuari.php, how do I select the id to know what record to delete? 但是,然后在文件elimina_usuari.php中,如何选择ID以知道要删除的记录?

I've thought with $_GET but it doesn't seems to work, either with $_POST: 我曾经考虑过使用$ _GET,但是似乎无法使用$ _POST:

elimina_usuari.php elimina_usuari.php

<?php 

global $conn;

$usuari_id = $_GET['id'];
$query = "DELETE FROM subjects WHERE id = {$usuari_id} LIMIT 1";

$result = mysqli_query($conn, $query);

if ($result && mysqli_affected_rows($conn) == 1) {
     redirect_to("calendari.php");      
} else {
echo "no eliminat";
}
?>

Any clue how can I get its id? 任何线索,我怎么能得到它的身份证? Should I take it from the url somehow? 我应该以某种方式从网址中获取它吗?

Thanks 谢谢

you're doing fine. 你做的不错。

just need to change this 只需要改变这个

$usuari_id = $_GET['id'];

to

$usuari_id = $_GET['subject'];

as you're setting subject instead of id in your url 在您设置subject而非网址中的id

http://localhost/calendario/elimina_usuari.php?subject=6
                                               ^  

and if you want to process id , like $_GET['id'] , you need to change URL. 如果要处理id ,例如$_GET['id'] ,则需要更改URL。

"http://localhost/calendario/elimina_usuari.php?id=6"
                                                ^ change here   

EDIT 编辑

as per your comment , 根据您的评论

you can use any $variable to $_POST or $_GET , it has nothing to do with the database column name. 您可以使用任何$variable$_POST$_GET ,它与数据库列名称无关。

Like you can use following. 就像你可以使用以下。

"http://localhost/calendario/elimina_usuari.php?eve_mf=6"

and on elimina_usuari.php page, elimina_usuari.php页面上,

$id = $_GET['eve_mf'];

and second part, why can I do that and I don't need to call it id as it is called in my db table? 第二部分, 为什么我可以这样做,而无需在数据库表中调用它的id?

Again, it's not the issue what you call variables in you local environment, all you to do(and should take care of) is to put right parameters in your sql query . 同样,在本地环境中调用variables也不是问题,您要做的(并且应该注意的)只是在sql query放入正确的参数。

$query = "DELETE FROM subjects WHERE id = {$usuari_id} LIMIT 1";

Here id is the name of your column name in your database. id是数据库中列名称的名称。 You can't change it here if you even want it to. 如果要更改它, 就不能在这里更改。

however, $usuari_id is your local variable, and you can change it whatever you want. 但是, $usuari_id是您的局部变量,您可以根据需要进行更改。

Hope I've explained what you're looking for :) 希望我已经解释了您要寻找的东西:)

You can get the id with $_GET['subject'] . 您可以使用$_GET['subject']获取ID。

Please be aware about SQL injection as you are wrongly get the id of the user to be deleted: 请注意SQL注入,因为您错误地获取了要删除的用户的ID:

$usuari_id = mysqli_real_escape_string($conn, $_GET['subject']);
<?php 

global $conn;
$usuari_id = $_GET['subject'];
$query = "DELETE FROM subjects WHERE id = {$usuari_id} LIMIT 1";
$result = mysqli_query($conn, $query);
if ($result && mysqli_affected_rows($conn) == 1) {
 redirect_to("calendari.php");      
} else {
echo "no eliminat";
}
?>

You just need to Get the exact variable name or parameter name which you have sent with your url 您只需要获取随URL发送的确切变量名或参数名即可

I mean see your url contains subject=6 that means you have to get subject instead of id; 我的意思是看到您的网址包含subject = 6,这意味着您必须获取Subject而不是id;

please replace this code 请替换此代码

$usuari_id = $_GET['id'];

to

$usuari_id = $_GET['subject'];

try this in elimina_usurai.php 在elimina_usurai.php中尝试一下

<?php 

global $conn;

$usuari_id = $_GET['subject'];
$query = "DELETE FROM subjects WHERE id = {$usuari_id} LIMIT 1";

$result = mysqli_query($conn, $query);

if ($result && mysqli_affected_rows($conn) == 1) {
     redirect_to("calendari.php");      
} else {
echo "no eliminat";
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM