Form To Delete Data From Mysql Database Using Php
So I'm creating a small program with 2 forms, one to add data to a database, and one to delete from it. I've managed to create the first input form, but I'm slightly confused as to
Solution 1:
Your SQL Statement
"DELETE FROM ID (ID) VALUES ('".$id."')"is wrong.
It should be
DELETEFROM table_name
WHERE some_column=some_value;
. So, change your statement to
DELETEFROM ID WHERE ID='$id'Suggestions
- You should use
POSTmethod for action which will result in data edit. - You should check the input, make sure it did not contain SQL statement. A good way is to use
$stuff = mysql_real_escape_string($_GET["stuff"]).
Solution 2:
I see you have name 'ID' in the form but your are trying to get 'id'. That could be the problem
Solution 3:
The sql statement for deletion should look something like the snippet below.
$sql = "DELETE FROM ID WHERE `id`=".$id.";";
$results = mysqli_query($conn,$sql);
Solution 4:
In addition to above answers you should give different name to the both forminput tags as
<h2>Add Tasks</h2><formaction="test.php"method="get">
Name of Task: <inputtype="text"name="name"><br />
Hours: <inputtype="number"name="hours"><br /><inputtype="submit"value="Add"name="submit"></form><h2>Delete Tasks</h2><formaction="delete.php"method="get">
ID: <inputtype="number"name="ID"><br /><inputtype="submit"value="Delete"name="delete"></form>So for adding into database , you can use
if (isset($_GET['submit'])){
// your code here
}
And for deleting from database , you can use
if (isset($_GET['delete'])){
mysqli_select_db ($conn, "Tasks");
$id = $_GET['id'];
$sql = "DELETE FROM ID (ID) WHERE ID='".mysql_real_escape_string($id)."' ;
$query = "SELECT `Name` FROM `ID`";
$result = mysqli_query($conn, $query);
$x=0;
}
This will solve all the problems.
If you are using same name for the type="submit" in both forms than you can use POST method on one form and GET method on the other.
And yes mysql_real_escape_string is used to prevent SQL INJECTION.
Post a Comment for "Form To Delete Data From Mysql Database Using Php"