繁体   English   中英

从HTML表单中的类中调用PHP函数

[英]Call PHP function in a class from a HTML form

我有一堂课是维护客人信息。 因此,在该类中,我有以下方法将访客数据插入guest_info表中

require_once "BaseModel.php";

class GuestModel extends BaseModel
{
    public static $table = "guest_info";

    public static function guestInfoFormData(){
         $title = $_POST['title'];
         $firstname = $_POST['firstname'];
         $lastname = $_POST['lastname'];
         $nicpassport = $_POST['nicpassport'];
         $contactnumber = $_POST['contactnumber'];
         $addressline1 = $_POST['addressline1'];
         $addressline2 = $_POST['addressline2'];
         $addressline3 = $_POST['addressline3'];
         $country = $_POST['country'];
         $guestinfo = array($title,$firstname,$firstname,$nicpassport,$addressline1,$addressline2,$addressline3,$country);
         insertInto($guestinfo);
    }
}

如何从HTML表单中调用此方法并将访客数据传递给它?

<?php GuestModel::guestInfoFormData(); ?>

确保在GuestModel之前需要/包括GuestModel类。

您可以使用GuestModel::guestInfoFormData() ,但不能在html文件中进行操作,它必须是php文件。

别忘了转义所有输入值,现在您将所有用户输入传递给insertInto ,因此在sql查询中使用它们以防止sql注入之前,必须转义数据。

顺便说一句:当您从类中调用全局函数时,为什么要创建一个类呢? 将全局函数insertInto移到此类(或另一个更好的类)中也许是个好主意:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM