【问题标题】:Create a function that auto-creates a new DB-entry创建一个自动创建新数据库条目的函数
【发布时间】:2017-03-27 22:35:03
【问题描述】:

我正在尝试通过访问 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();
  }
 }

当我访问该网址时,我收到以下错误:

致命错误:在 null in 上调用成员函数 addmed() test2.php 第 20 行

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

【问题讨论】:

标签: php mysql pdo


【解决方案1】:

你使用

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

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

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

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

$this->addmed(...)

【讨论】:

  • 是的,addmed() 函数是在一个类中编写的。我已经用完整的代码更新了这个问题。它看起来更好还是更糟? :)
  • 您使用 $reg_med 对象,但它应该在 createMedOne 函数中启动。类似于: $reg_med = new classWithMethodAddmed()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-30
  • 1970-01-01
相关资源
最近更新 更多