$my_conn = new PDO('sqlite:my_student.sqlite3');
// Set errormode to exceptions
$my_conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$sql=$my_conn->prepare("DROP TABLE student");
if($sql->execute()){
echo " <br><br>Table deleted. ";
}else{
print_r($sql->errorInfo());
}
$my_conn = null;
Output is here
Table deleted.
User Confirmation: Suggest adding an additional layer of security by confirming with the user (e.g., through a form or prompt) before executing the delete operation.
$status=$_GET['status']; // Getting value from query string
if($status=='yes'){
try{
$sql=$my_conn->prepare('DROP TABLE student');
if($sql->execute()){
echo "<br> student table deleted. ";
}
}
catch(PDOException $e){
echo " Table not deleted : ".$e->getMessage();
}
}else{
echo "<br><a href=drop.php?status=yes>
Clcik here to delete table</a>";
}
We will check the table if exists or not by using sqlite_master before deleting it. The first line is the query for checking sqlite_master with type=table and name=table_name by using WHERE condition.
If the table is available then we will delete it.
$q="SELECT name FROM sqlite_master WHERE type='table' AND name='student'";
if($result=$my_conn->query($q)){
$row = $result->fetch(PDO::FETCH_OBJ);
if($row->name=='student'){
if(strlen($todo)==3 and $todo=='yes'){
$sql=$my_conn->prepare("DROP TABLE student");
if($sql->execute()){
echo " <br><br>Table deleted. <br> ";
}else{
print_r($sql->errorInfo());
}
$my_conn = null;
} // end of checking todo
}else{ // end of if else if student table exits
echo " <br><br>student table is not available<br> ";
}
}