我这样的脚本连接数据库:
<?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");
}
?>
第二个脚本:
<?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);
}
}
}
?>
我的问题是 - 如何调用&#34; deleteSelected&#34;单击&#34;删除&#34;按钮,所以删除具有给定ID的类别?
答案 0 :(得分:2)
首先要记住网页的生命周期。
首先启动页面,除了使用默认数据构建页面之外,您不想做任何其他事情。
然后用户输入一些数据并按下提交按钮,在这种情况下,您想要知道用户确实做了某些事情并且页面已经发送给您处理他们所做的事情。
所以最好将PHP代码放在脚本的顶部,并在测试内部,只有在提交页面进行处理时才会生效,而不是在页面首次加载时点击链接或菜单
此外,原始代码中存在大量错误,因此您在更改代码之前最好先了解所有这些错误。
<?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>
答案 1 :(得分:1)
&#34; require_once(&#39; ../ config / Database.php&#39;);&#34;:
if ( $_POST['id'] && $_POST['delete'] ) deleteSelected( (int) $_POST['id'] );