php - 创建一个自动创建新数据库条目的函数

标签 php mysql pdo

我正在尝试找到一种通过访问 url 来自动创建数据库条目的方法。 该网址应该每天被访问一次。

这是我现在正在尝试的功能:

    function createMedOne() {
 $medid = "1";
 $mname = "Name1";
 $menh = "Amount 1";
 $mtime = "17:23";
 $mdte = date("Y-m-d");
 $mhtml = '<tr class="" id="med1">
        <td><p style="font-weight: bold;">Name1</p></td>
        <td>Amount 1</td>
        <td>Kl: 17:23</td>
        <td><button type="button" class="btn btn-warning btn-sm" style="float: right;">Sign</button></td>
      </tr>';

 $reg_med->addmed($medid,$mname,$menh,$mtime,$mdte,$mhtml);
}

createMedOne();

其他功能:

function addmed($medid,$mname,$menh,$mtime,$mdte,$mhtml)
 {
  try
  {       
   $stmt = $this->conn->prepare("INSERT INTO tbl_med(medID,medName,medEnh,medTime,medDate,medHtml) 
                                                VALUES(:med_Id, :med_name, :med_enh, :med_time, :med_date, :med_html)");                                            
   $stmt->bindparam(":med_Id",$medid);      //id of the med
   $stmt->bindparam(":med_name",$mname);    //name of the med
   $stmt->bindparam(":med_enh",$menh);      //the amount
   $stmt->bindparam(":med_time",$mtime);    //When to give med
   $stmt->bindparam(":med_date",$mdte);     //date
   $stmt->bindparam(":med_html",$mhtml); //the html-code to generate content
   $stmt->execute(); 
   return $stmt;
  }
  catch(PDOException $ex)
  {
   echo $ex->getMessage();
  }
 }

当我访问该网址时,出现以下错误:

Fatal error: Call to a member function addmed() on null in test2.php on line 20

它指的是函数createMedOne,但我找不到我错过了什么,但显然我错过了。

最佳答案

你使用

$stmt = $this->conn->prepare(...)

这是一个类方法?如果这是一个类方法,您应该创建一个类对象,然后调用您的方法:

$reg_med = new classWithMethodAddmed();
$reg_med->addmed(...);

或者如果它们是同一个类的方法,则应该使用 $this 调用 addmed:

$this->addmed(...)

关于php - 创建一个自动创建新数据库条目的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43057580/

相关文章:

mysql:使用 LIKE 选择多列

mysql - 在 mySQL 中执行 "nodupkey"(SAS) 的最佳方法是什么?

php - PDO UPDATE 似乎不存储引号?

php - 我如何找到这个 div ? (PHP 简单 HTML DOM 解析器)

php - 插入新行时避免数据库重复

mysql - SQL 查询和线程

PHP PDO 准备查询问题

php - Mysql PDO查询的公共(public)参数使用方法

php - 如何将数学 mysql 查询应用于数据库中的每一行?

php - 如何从大(大)文件中删除最后 N 行