What is delete function php?


Delete Data From a MySQL Table Using MySQLi and PDO

The DELETE statement is used to delete records from a table:

DELETE FROM table_name
WHERE some_column = some_value

Notice the WHERE clause in the DELETE syntax: The WHERE clause specifies which record or records that should be deleted. If you omit the WHERE clause, all records will be deleted!

To learn more about SQL, please visit our SQL tutorial.

Let's look at the "MyGuests" table:

idfirstnamelastnameemailreg_date
1 John Doe 2014-10-22 14:26:15
2 Mary Moe 2014-10-23 10:22:30
3 Julie Dooley 2014-10-26 10:48:23

The following examples delete the record with id=3 in the "MyGuests" table:

Example (MySQLi Object-oriented)

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

// sql to delete a record
$sql = "DELETE FROM MyGuests WHERE id=3";

if ($conn->query($sql) === TRUE) {
  echo "Record deleted successfully";
} else {
  echo "Error deleting record: " . $conn->error;
}

$conn->close();
?>




Example (MySQLi Procedural)

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}

// sql to delete a record
$sql = "DELETE FROM MyGuests WHERE id=3";

if (mysqli_query($conn, $sql)) {
  echo "Record deleted successfully";
} else {
  echo "Error deleting record: " . mysqli_error($conn);
}

mysqli_close($conn);
?>


Example (PDO)

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";

try {
  $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  // set the PDO error mode to exception
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  // sql to delete a record
  $sql = "DELETE FROM MyGuests WHERE id=3";

  // use exec() because no results are returned
  $conn->exec($sql);
  echo "Record deleted successfully";
} catch(PDOException $e) {
  echo $sql . "<br>" . $e->getMessage();
}

$conn = null;
?>


After the record is deleted, the table will look like this:

idfirstnamelastnameemailreg_date
1 John Doe 2014-10-22 14:26:15
2 Mary Moe 2014-10-23 10:22:30



Change language:

Submit a Pull Request Report a Bug

deleteSee unlink() or unset()

Description

There is no delete keyword or function in the PHP language. If you arrived at this page seeking to delete a file, try unlink(). To delete a variable from the local scope, check out unset().

See Also

  • unlink() - Deletes a file
  • unset() - Unset a given variable

+add a note

User Contributed Notes

There are no user contributed notes for this page.

PHP beginner here, probably bind to this mistake. Got a crud function going on here, trying to get the delete button to work. Users would currently be on the /crud/view.php. It's currently asking "do you want to delete" followed by the screen refreshing and nothing happening.

<?php 

session_start();

if(isset($_SESSION['u_uid'])){

$uid = $_SESSION['u_id'];

require_once('connect.php');
$ReadSql = "SELECT * FROM `contact` WHERE users_id=$uid ORDER BY Name";
$res = mysqli_query($connection, $ReadSql);

?>
<!DOCTYPE html>

<html>
<head>
<title>Motoko</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">
</script>
</head>
<body> 
<div class="container">
<div class="row">
    <table class="table">
        <tr>    

        <th><strong>Extras</strong></th>
        </tr>
        <?php 
        while($r = mysqli_fetch_assoc($res)){
        ?>
        <tr> 

            <td><a href="update.php?id=<?php echo $r['id'] ?>">Edit</a>
</td>
            <td><input type="button" onClick="deleteme(<?php echo 
$r['u_uid']; ?>)" name="Delete" value="Delete"></td>
             </tr>
 <script language="Javascript">
 function deleteme(delid)
 {
 if(confirm("Are you sure you want to Delete?")){
 window.location.href='delete.php';
 }
 } 
 </script>
        <?php } ?>
    </table>
</div>
</body>

</html>

<?php

}else{

header("Location: http://motoko.sorainsurance.com.au"); 

}

?>

With the /delete.php being:

<?php

 if(isset($_SESSION['u_uid'])){

require_once('connect.php');

$select = "DELETE from contact where id='".$_GET['del_id']."'";
$query = mysqli_query($connection, $select) or die($select);
}else{

header("Location: http://motoko.sorainsurance.com.au/crud/view.php"); 

}
?>

Cœur

35.6k24 gold badges188 silver badges257 bronze badges

asked Feb 6, 2018 at 5:02

What is delete function php?

2

Change these lines

<input type="button" onClick="deleteme('<?php echo 
$r['u_uid']; ?>')" name="Delete" value="Delete">


function deleteme(delid){
    if(confirm("Are you sure you want to Delete?")){
         window.location.href='delete.php?del_id='+delid;
    }
} 

Always preferred to write you php code at top of HTML script

<?php 

session_start();

if(!isset($_SESSION['u_uid'])){
  header("Location: http://motoko.sorainsurance.com.au"); 
}

$uid = $_SESSION['u_id'];

require_once('connect.php');

$ReadSql = "SELECT * FROM `contact` WHERE users_id=$uid ORDER BY Name";
$res = mysqli_query($connection, $ReadSql);

?>
<!DOCTYPE html>
<html>
<head>
<title>Motoko</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">
</script>
</head>
<body> 
<div class="container">
<div class="row">
    <table class="table">
      <tr>    
        <th>
          <strong>Extras</strong>
        </th>
      </tr>
      <?php while($r = mysqli_fetch_assoc($res)){ ?>
        <tr> 
          <td>
            <a href="update.php?id=<?php echo $r['id'] ?>">Edit</a>
          </td>
          <td>
            <input type="button" onClick="deleteme('<?php echo $r['u_uid']; ?>')" name="Delete" value="Delete">
          </td>
        </tr>
        <?php } ?>
    </table>
</div>
</body>
</html>
<script language="Javascript">
  function deleteme(delid){
    if(confirm("Are you sure you want to Delete?")){
      window.location.href='delete.php?del_id=delid';
    }
  } 
</script>

answered Feb 6, 2018 at 5:06

What is delete function php?

Aman MauryaAman Maurya

1,31512 silver badges26 bronze badges

3

<form>
<input type="button" value="Search Contact 1.0" onclick="window.location.href='http://motoko.sorainsurance.com.au/crud/searchone.php'" />
</form>

<form>
<input type="button" value="Search Contact 2.0" onclick="window.location.href='http://motoko.sorainsurance.com.au/crud/search.php'" />
</form>

<html>
<head>
    <title>Motoko</title>
        <!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body> 
<div class="container">
<div class="row">
    <table class="table">
        <tr>

            <th><strong>Name</strong></th>
            <th><strong>Company</strong></th>
            <th><strong>Title</strong></th>
            <th><strong>Phone</strong></th>
            <th><strong>Email</strong></th>
            <th><strong>Address</strong></th>
            <th><strong>Extras</strong></th>
        </tr>
        <?php 
        while($r = mysqli_fetch_assoc($res)){
        ?>
        <tr> 
            <td><?php echo $r['Name']; ?></td> 
            <td><?php echo $r['Company']; ?></td> 
            <td><?php echo $r['Title']; ?></td> 
            <td><?php echo $r['Phone']; ?></td> 
            <td><?php echo $r['Email']; ?></td> 
            <td><?php echo $r['Address']; ?></td> 
            <td><a href="update.php?id=<?php echo $r['id'] ?>">Edit</a></td>
            <td> <input type="button" onClick="deleteme(<?php echo $r['u_uid']; ?>)" name="Delete" value="Delete">
             </tr>

     <script language="Javascript">
 function deleteme(delid) {
 if(confirm("Are you sure you want to Delete?")){
   window.location.href='delete.php?del_id='+delid;
 }
 } 
     </script>
        <?php } ?>
    </table>
</div>
    </body>

    </html>

    <?php

}else{

header("Location: http://motoko.sorainsurance.com.au"); 

}

?>

answered Feb 6, 2018 at 23:54

Could you try ?

$select = "DELETE from contact where id='".$_GET['u_uid']."'";

answered Feb 6, 2018 at 5:39

What is delete function php?

jeromejerome

6956 silver badges20 bronze badges

2

What is delete function?

Use the DELETE function to erase the data contents of a specified field, value, or subvalue and its corresponding delimiter from a dynamic array. The DELETE function returns the contents of the dynamic array with the specified data removed without changing the actual value of the dynamic array.

How do you delete something in PHP?

There is no delete keyword or function in the PHP language. If you arrived at this page seeking to delete a file, try unlink(). To delete a variable from the local scope, check out unset().

Which command is used to delete a file in PHP?

In PHP, we can delete any file using unlink() function. The unlink() function accepts one argument only: file name. It is similar to UNIX C unlink() function.

Is used to delete a variable in PHP?

The unset() function in PHP resets any variable. If unset() is called inside a user-defined function, it unsets the local variables. If a user wants to unset the global variable inside the function, then he/she has to use $GLOBALS array to do so. The unset() function has no return value.