dongtazu3080 2015-05-11 02:16 采纳率: 100%
浏览 92
已采纳

如何从html表单php mysql中删除数据

I'm new to php and I learned some stuffs like adding data but I got stuck in deleting data from html form. Here's my code.

database name: mydb

tablename : registered

HTML

<HTML>
<BODY>
<form method="post" action="dataout.php">
ID:<input type="Text" id="idelete" name="idelete"><br>
<input type="Submit" name="submit" value="delete">
</form>
</HTML>

dataout.php

<HTML>
<head>
</head>
<body>
<?php
$db = mysqli_connect("localhost", "root","");
mysqli_select_db($db,"mydb");
$id=$_POST['idelete'];
mysqli_query("DELETE FROM registered WHERE id=$id",$db);
echo "Information Deleted";
?>
</body>
</HTML>

When I click the button nothing appears, no errors and nothing; please help me.

  • 写回答

3条回答 默认 最新

  • dongtan6695 2015-05-11 02:20
    关注

    This line:

    mysqli_query("DELETE FROM registered WHERE id=$id",$db);
    

    the connection comes first in mysqli_ and not last.

    mysqli_query($db, "DELETE FROM registered WHERE id=$id");
    

    mixed mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )

    You could also do it all in one go, without using mysqli_select_db

    $db = mysqli_connect("localhost", "root", "", "mydb");
    

    You should also use conditional statements along with isset() and empty().

    Also make sure the id being passed through is an int. Otherwise, you will need to quote it.

    I.e.:

    mysqli_query($db, "DELETE FROM registered WHERE id='$id'");
    

    Sidenote: Your present code is open to SQL injection. Use mysqli with prepared statements, or PDO with prepared statements, they're much safer.


    Make use of error reporting/checking for both PHP and MySQL.

    Consult:


    Edit:

    Do the following:

    mysqli_query($db, "DELETE FROM registered WHERE id='$id'")
      or die(mysqli_error($db));
    

    to see if errors come of it from your query.

    Add error reporting to the top of your file(s) which will help find errors.

    <?php 
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    

    Edit #2:

    Replace:

    $id=$_POST['idelete'];
    mysqli_query("DELETE FROM registered WHERE id=$id",$db);
    

    with:

    if(!empty($_POST['idelete'])){
    $id=$_POST['idelete'];
    }
    $query = "DELETE FROM registered WHERE id=$id";
    $result = mysqli_query($db, $query);
    if ( !$result ) {
        trigger_error('query failed', E_USER_ERROR);
    }
    

    and see if any errors come of it.

    • If you see "query failed...", then your query failed.

    • You will need to find out why.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度