I've script like this to connect with database:
<?php
class Database {
public function getConnection() {
$result = false;
try {
$result = new PDO('mysql:host=localhost;dbname=college', 'root', '');
} catch(PDOException $e) { }
return $result;
}
}
$db = new Database();
$conn = $db->getConnection();
if (!$conn) {
die("Error connecting to the database");
}
?>
And the second script:
<?php
require_once('../config/Database.php');
?>
<form action="" method="post">
<label>Category id :</label>
<input type="text" name="id" id="id" required="required" placeholder="Please Enter Id"/><br /><br />
<input type="submit" value=" Delete " name="delete"/><br />
</form>
</div>
<?php
class Category {
private $conn;
private $id;
private $name;
private $description;
private $created;
public function __construct($db) {
$this->conn = $db;
}
/* This function will get the ids of categories as
parameters and delete them from database.*/
public function deleteSelected($ids) {
$query = 'DELETE FROM categories WHERE id=';
if (is_array($ids)) {
foreach ($ids as $id)
$stmt = $this->conn->prepare($query)->execute($query.$id);
}
else {
$stmt = $this->conn->prepare($query)->execute($query.$ids);
}
}
}
?>
My question is - how can I call the "deleteSelected" function by clicking "Delete" button, so it deletes the category with given id?
First remember the life cycle of a web page.
First you launch the page and you dont want to do anything other than build the page with its default data.
Then the user enters some data and presses the submit button, in this case you want to know that the user actually did something and the page has been sent to you to deal with what they did.
So its best to put you PHP code at the top of the script and inside a test that will only be true when a page is submitted for processing, and not when the page is first loaded fron a click on a link or menu.
Also there are lots of errors in your original code so you had better look at all of this before changing your code with the first thing you notice.
<?php
require_once('../config/Database.php');
class Category {
private $conn;
private $id;
private $name;
private $description;
private $created;
public function __construct($db) {
$this->conn = $db;
}
/* This function will get the ids of categories as
parameters and delete them from database.*/
public function deleteSelected($ids) {
// notice I added a prameter to this query
$query = 'DELETE FROM categories WHERE id=?';
// one of the points of using a prepared query is that
// you can prepare it once and then execute it many time
// with different paramters
$stmt = $this->conn->prepare($query);
if (is_array($ids)) {
foreach ($ids as $id)
$stmt->execute([$id]);
}
} else {
$stmt->execute([$ids]);
}
}
}
// did user press submit
if($_SERVER['REQUEST_METHOD'] == 'POST'){
// what button did they press
if ( isset( $_POST['delete'], $_POST['id']) ) {
// we have been request to delete
// and we have an id to control the delete
// instantiate the class we need
$cat = new Category($conn);
// clean up our input before we us it in a SQL query
$id = filter_input ( INPUT_POST , 'id', FILTER_SANITIZE_NUMBER_INT );
// call the method we want to run
$cat->deleteSelected($id);
}
}
?>
<form action="" method="post">
<label>Category id :</label>
<input type="text" name="id" id="id" required="required" placeholder="Please Enter Id"/><br /><br />
<input type="submit" value="Delete" name="delete"/><br />
</form>
</div>
After "require_once('../config/Database.php');":
if ( $_POST['id'] && $_POST['delete'] ) deleteSelected( (int) $_POST['id'] );