I've got a PHP class which has 1 method for inserting data. Once I create my instance of the class, I'm not sure how to call the method. I've seen other questions about how to accomplish calling a method, but none deal with calling the method of a class instance.
My PHP is:
<?php
$servername = "localhost";
$username = "kevin";
$password = "ally";
$database = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['btnInsert']) && ($_POST['btnInsert'] == "Insert"))
{
$Dog = new Dog($_POST['txtName'], $_POST['txtSize'], $_POST['txtColor'], $_POST['txttype'], $_POST['txtDescription']);
}
class Dog
{
public $name = "Dog Name";
public $size = 0;
public $color = "255:255:255";
public $typeName = "type Name";
public $typeDescription = "type Description";
public function Dog($name, $size, $color, $type, $description){
$this->name = $name;
$this->size = $size;
$this->color = $color;
$this->typeName = $type;
$this->typeDescription = $description;
}
public function InsertDog(){
$query = "SELECT Id from tbl_type WHERE Name = $typeName";
$result = mysqli_query($conn, "INSERT INTO tbl_type($this->typeName, $this->$typeDescription)") or die("Query fail: " . mysqli_error());
$result2 = mysqli_query($conn, "INSERT INTO tbl_Dog($this->$name, $this->$size, $this->$color, $query)") or die("Query fail: " . mysqli_error());
}
public function UpdateDog(){
}
}
?>
And my form:
<form action="index.php" method="post">
Dog Name:<br />
<input name="txtName" type="text" /><br />
<br />
Size:<br />
<input name="txtSize" type="text" /><br />
<br />
Color:<br />
<input name="txtColor" type="text" /><br />
<br />
type Name:<br />
<input name="txttype" type="text" /><br />
<br />
type Description:<br />
<input name="txtDescription" style="width: 419px; height: 125px" type="text" /><br />
<br />
<input name="btnInsert" type="submit" value="Insert" />
</form>
How can I call the InsertDog()
method for the $Dog
instance?