php
  • mysql
  • pdo
  • 2017-03-27 103 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

    相關問題