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.

2
  • Add error reporting to the top of your file(s) right after your opening PHP tag for example <?php error_reporting(E_ALL); ini_set('display_errors', 1); then the rest of your code, to see if it yields anything. Commented May 11, 2015 at 2: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? Commented May 11, 2015 at 2:41

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

10 Comments

@Emmanuel it doesn't work doesn't help me. Plus, I hope you'll be marking it as an edit under your original post.
im still new here im confused sorry im trying
@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.
@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.
im sorry im really confused here
|
0

You're misplacing db connection parameter

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

4 Comments

"You're missing db connection parameter" - it's not missing, it's misplaced
Correct. My answerer looks absolute wrt yours . and can't delete from phone
i have updated my code but still it doesnt work. im using WAMP server and it worked before when im adding data but im having problem in deleting i just want a simple code to delete data for a beginner i would like to explore the injection thingy later
Your files are in the same folder?
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.