如何从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.

我是php的新手,我学到了一些东西,比如添加数据,但是我在从HTML格式中删除数据时遇到了困难 。 这是我的代码。 p>

数据库名称:mydb p>

tablename:registered p>

HTML p >

 &lt; HTML&gt; 
&lt; BODY&gt; 
&lt; form method =“post”action =“dataout.php”&gt; 
ID:&lt; input type =“Text”id  =“idelete”name =“idelete”&gt;&lt; br&gt; 
&lt; input type =“Submit”name =“submit”value =“delete”&gt; 
&lt; / form&gt; 
&lt; / HTML&gt; 
  代码>  PRE> 
 
 

dataout.php p>

 &LT; HTML&GT; 
&LT; HEAD&GT; 
&LT; /头&GT; 
&LT;  body&gt; 
&lt;?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”; 
?&gt; 
&lt; / body&gt; 
&lt; / HTML&gt; 
  code>   pre> 
 
 

当我点击按钮时没有任何显示,没有错误,什么也没有; 请帮助我。 p> div>

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.

You're misplacing db connection parameter

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

wel come to PHP

here is simple & understandable sample code

html

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

dataout.php file

<?php
mysql_connect("localhost", "root", "") or die("Connection Failed");  //my sql connection create
mysql_select_db("mydb")or die("Connection Failed"); // data base connection
$id = $_POST['idelete']; delete feiled id
$query = "delete from registered  where id = '".$id."'";  //query 
if(mysql_query($query)){ //check is true or false 
 echo "deleted id ".$id." ";//out put if ture
 }
 else{ 
 echo "Delete fail";//out put if false
} 
?>

i hope this will be help to improve your php https://www.siteground.com/tutorials/php-mysql/display_table_data.htm