1

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Duckbenok
  • 95
  • 1
  • 2
  • 9
  • Add error reporting to the top of your file(s) right after your opening PHP tag for example ` – Pedro Lobito May 11 '15 at 02:17
  • When you are submitting the form, does the `idelete` field have the right ID to delete? Are the values (host, username, password and database name) inside your connection correct? – Logan Wayne May 11 '15 at 02:41

3 Answers3

3

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.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • @Emmanuel it doesn't work doesn't help me. Plus, I hope you'll be marking it as an edit under your original post. – Funk Forty Niner May 11 '15 at 02:31
  • im still new here im confused sorry im trying – Duckbenok May 11 '15 at 02:32
  • @Emmanuel if you're using your HTML form and PHP/MySQL inside the same file, you need to use a conditional statemen around the executable code. Also, do you have a webserver setup? Is the file `.php` extension? Too many things can be an issue here. – Funk Forty Niner May 11 '15 at 02:32
  • @Emmanuel what you did, isn't how things are done on Stack. You updated your question and overwrote everything with my code in there without marking it as an edit. I stand at getting downvoted for it, once people see the correct method and tell themselves: *"the db parameter is ok, so why the answer?"*. I have performed a rollback to the original question and you can add on to it after. – Funk Forty Niner May 11 '15 at 02:35
  • im sorry im really confused here – Duckbenok May 11 '15 at 02:39
  • @Emmanuel Reload my answer and look near the bottom under **Edit:**. there are a few ways to check for errors. Plus, since I noticed you are running under Wamp, make sure your server's properly setup and that the file is `.php` extension for the PHP/MySQL. If you made changes to your server, make sure you restarted all services and that all services are all indeed running. – Funk Forty Niner May 11 '15 at 02:39
  • @Emmanuel The "id" column needs to be of `int` type and not `varchar` if that is what you're using. Also, you need to make sure that whatever you enter in that input, must exist in your database. what value are you passing in that? – Funk Forty Niner May 11 '15 at 02:42
  • yes the id is using INT and the database is good, the wamp is also running fine – Duckbenok May 11 '15 at 02:44
  • @Emmanuel as I said, check for errors as outlined in my answer and make sure the `id` number you're passing in the input exists in your table and that there are no spaces anywhere, including during your input. That is about as far as I can help with this, since I can't see anything else causing problems. I too run Wamp. – Funk Forty Niner May 11 '15 at 02:50
  • @Emmanuel Reload my answer again, and look near the bottom again, under **Edit #2:**. If that fails, you'll need to find out why. I have to go to bed now, so I hope this gets resolved. Good luck. – Funk Forty Niner May 11 '15 at 02:59
0

You're misplacing db connection parameter

mysqli_query($db, "DELETE FROM registered WHERE id=$id");
Sunil B N
  • 4,159
  • 1
  • 31
  • 52
0

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

channasmcs
  • 1,104
  • 12
  • 27