php
  • mysql
  • pdo
  • 2017-03-27 105 views 0 likes 
    0

    我试图找到一种方法,功能自动创建仅通过访问一个网址为DB-条目。 网址应该在每一个新的一天访问一次。创建自动创建一个新的数据库条目

    这是我的职责,我想现在:

    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(); 
        } 
    } 
    

    当我访问的URL我收到以下错误:

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

    它指的是函数createMedOne,但我找不到我错过了某些东西,但显然我有。

    +1

    我被自己处理过,可能是我可以帮你吗? –

    +0

    它是一个[可变范围(http://stackoverflow.com/q/16959576/1415724) –

    +0

    [PHP呼叫类方法/函数(的可能的复制http://stackoverflow.com/questions/15499513/php-call -class-method-function) – miken32

    回答

    1

    您使用

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

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

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

    ,或者如果他们是同一类的方法,你应该叫使用addmed $此:

    $this->addmed(...) 
    
    +0

    是的,addmed()函数写在一个类中。 我已经用完整的代码更新了这个问题。它看起来好还是坏? :) – Danahlen

    +0

    您使用$ reg_med对象,但它应该在createMedOne函数内启动。例如:$ reg_med = new classWithMethodAddmed() – Nutscracker

    +0

    当然!这解决了它,非常感谢你! – Danahlen

    相关问题