我正在尝试找到一种通过访问 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/