DELETE Data In a MySQL Table Using MySQL and PHP functions

DELETE Data In a MySQL Table Using MySQL and PHP functions

The DELETE query statement :

DELETE Database_Name.Table_name  WHERE column=value

MYSQL DELETE QUERY EXAMPLE:

DELETE FROM `database`. table name WHERE `table name`.`id` =8

DELETE FROM `erp`. employee WHERE `employee`.`id` =8


<?php 

function deleterec($conn,$tableName,$db,$id) {
echo $sql = "DELETE FROM `erp`. $tableName WHERE `employee`.`id` =".$id." "; 
          mysql_select_db(''.$db.'');
$retval = mysql_query( $sql, $conn );

return $retval;
}

?>



Example Create data.php


<?php

function DB_Mysql() {

     $dbhost = 'localhost';
      $dbuser = 'root';
      $dbpass = 'root';
      $conn = mysql_connect($dbhost, $dbuser, $dbpass);
      if(! $conn ) {
         die('Could not connect: ' . mysql_error());
      }
return $conn;
}


function deleterec($conn,$tableName,$db,$id) {


echo $sql = "DELETE FROM `erp`. $tableName WHERE `employee`.`id` =".$id." ";
          mysql_select_db(''.$db.'');
$retval = mysql_query( $sql, $conn );

return $retval;
}


function getData($conn,$tableName,$db,$id='') {

$sql = "SELECT * FROM $tableName";
          mysql_select_db(''.$db.'');
$retval = mysql_query( $sql, $conn );

return $retval;
}


?>


 Create index.php

<?php
include_once('data.php');
$conn=DB_Mysql();
$tableName="employee";
$db="erp";
if($_GET) {
$id=$_GET['id'];
 $edit=getData($conn,$tableName,$db,$id);

deleterec($conn,$tableName,$db,$id) ;

}
?>
 <html>
<head></head>
<body>

<?php


$retval=getData($conn,$tableName,$db);

   if(! $retval ) {
      die('Could not get data: ' . mysql_error());
   }
 
   while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
echo "<br> No :{$row['id']}   ";
echo "Name :{$row['name']}   ";
echo "Email :{$row['email']}   ";
echo "Address :{$row['address']}   ";
echo "Slug :{$row['slug']}  <br> ";
echo "Slug :{$row['id']} <br> ";

echo "<a href=\"index.php?id={$row['id']}\">test</a>";

}
?>

No comments:

Post a Comment

how to call ssh from vs code

 To call SSH from VS Code, you can use the built-in Remote Development extension. This extension allows you to open a remote folder or works...