我正试图更多地理解函数的工作,所以我决定为我的PHP脚本编写一些函数。好吧,第一个功能不能工作,我想了解为什么要避免这些问题在未来。
我是这样做的:我只是想创建一个函数来删除数据库内的管理员。下面是我的代码:
<?php
// Delete Mitarbeiter
function deleteMitarbeiter($id) {
if(!isset($id) || !is_int($id)) {
return FALSE; // wrong parameters, return early
}
$deleteUser = $this->db->prepare("DELETE FROM `admin` WHERE id=?");
$deleteUser->bindParam(1, $id, PDO::PARAM_INT);
return $deleteUser->execute(); // returns true on success, otherwise false
}
if (isset ($_POST['deletemitarbeiter'])) {
$member_Id = $_POST['member_Id'];
deleteMitarbeiter($member_Id);
}
?>
<form method="post" action="edit_mitarbeiter.php" class="form-horizontal form-label-left">
<input type="hidden" value="<?php echo $result_member_ID;?>" name="member_Id">
<button type="submit" name="deletemitarbeiter" value="deletemitarbeiter" class="btn btn-danger btn-lg pull-right">Mitarbeiter löschen</button>
</form>
我有error_reporting上,但我没有得到任何错误。我想我的参数有问题吧?我真的很坚持。
编辑:以下是我编辑过的代码:
<?php
// Delete Mitarbeiter
function deleteMitarbeiter($id) {
if(!isset($id) || !is_int($id)) {
return FALSE; // wrong parameters, return early
}
try {
$db = new DbContext();
$deleteUser = $db->prepare("DELETE FROM `admin` WHERE id=?");
$deleteUser->bindParam(1, $id, PDO::PARAM_INT);
return $deleteUser->execute(); // returns true on success, otherwise false
} catch (PDOException $e) {
die("Could not execute the delete: " . $e->getMessage());
}}
if (isset ($_POST['deletemitarbeiter'])) {
$member_Id = intval($_POST['member_Id']);
deleteMitarbeiter($member_Id);
}
?>
现在我的错误信息是:Fatal error: Class 'DbContext' not found
!我的代码应该是什么样子?
mysqli_real_escape_string
返回一个字符串。所以is_int($id)
为假,方法不执行。
在方法内部进行转义来解决这个问题。
is_int
函数检查实类型,如果您传递一个字符串,返回false,即使字符串是数字。您可以使用is_numeric()
来检查数字字符串。
让我们深入了解一些基础知识。为什么不能在自由函数中使用$this:
<?php
//our base class
class Test
{
private $privateVar = 0;
public function __construct($value)
{
//access $this from within this class Test
$this->privateVar = $value;
}
public function DoSomething()
{
//access $this from within this class Test with the initialized value from the constructor
echo $this->privateVar;
}
public static function DoSomeStatic()
{
//since this is a static method you can't access $this here...
echo "some static";
}
}
//your function here
function testFunction()
{
$test = new Test(1233);
$test->DoSomething();
$test2 = new Test(456677);
$test2->DoSomething();
Test::DoSomeStatic();
//and you can't access $this here also, since $this is the context of the class.
//i.e. if you aren't in a class there exists no object $this and therefor you can't call $this->DoSomething() here.
}
//and call your testfunction
testFunction();
因为你的方法deleteMitarbeiter
是一个自由函数(不存在于类上下文中),所以没有$this
对象。因此,你必须创建对象的实例或使用静态方法。
顺便说一句:我也会考虑在编程时只使用英语单词,而不是德语单词…
现在解决你的问题:
你想在这个自由函数中使用你的数据库上下文:
//i suppose your db context looks something like this:
class DbContext
{
private $db = NULL;
public function __construct()
{
$this->db = new PDO($dsn, $user, $password);
}
}
,所以你不想在这个类之外访问$db。你必须修改它,或者做一些代理。
如果使用代理,可以这样扩展类:
public function execute()
{
$this->db->execute();
}
,你可以在你的方法deleteMitarbeiter
中创建一个上下文的实例,像这样:
function deleteMitarbeiter($id)
{
$context = new DbContext();
$context->execute();
}
但是如果你选择使用DbContext
作为你的基类,你可以扩展你的类,你的"Mitarbeiter"或"合作者"的所有操作都在发生。
class DbContext
{
protected $db;
public function __construct()
{
$this->db = new PDO...
}
}
class CoWorkerController extends DbContext
{
function delete($id)
{
$this->db->execute();
}
}
这会让你朝着正确的方向前进。
希望对你有帮助。
<?php
$host = 'localhost'; $db = 'database-name'; $user = 'database-user'; $pw = 'database-password';
$conn = new PDO('mysql:host='.$host.';dbname='.$db.';charset=utf8', $user, $pw);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Delete Mitarbeiter
function deleteMitarbeiter($id) {
if(!isset($id) || !is_int($id)) {
return FALSE; // wrong parameters, return early
}
try {
$id = $_POST['member_Id']; // this is the line I added
$sql = "DELETE FROM `admin` WHERE id=?";
$deleteUser = $conn->prepare($sql);
$deleteUser->bindParam(1, $id, PDO::PARAM_INT);
return $deleteUser->execute(); // returns true on success, otherwise false
} catch (PDOException $e) {
die("Could not execute the delete: " . $e->getMessage());
}}
if (isset ($_POST['deletemitarbeiter'])) {
$member_Id = $_POST['member_Id'];
deleteMitarbeiter($member_Id);
}
?>