0

I am trying to find a way to auto-create a DB-entry by just visiting an url. The url is supposed to be visited once every new day.

This is my function that I am trying right now:

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

Other function:

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

When I visit the url I get the following error:

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

Which refers to the function createMedOne, but I can't find that I've missed something, but obviosly I have.

Danahlen
  • 11
  • 2

1 Answers1

1

You use

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

This is a class methods? If this is a class methods, you should create a class object, then call your method:

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

Or If they are methods of the same class, you should call addmed by using $this:

$this->addmed(...)
Nutscracker
  • 702
  • 3
  • 9
  • Yes, the addmed() function is written within a class. I've updated the question with the full code. Does it look better or worse? :) – Danahlen Mar 27 '17 at 23:47
  • You use $reg_med object, but it should be initiated inside createMedOne function. Something like: $reg_med = new classWithMethodAddmed() – Nutscracker Mar 27 '17 at 23:53