I'm trying to get a feel for OOP with PHP/MySQL, so I attempted to write a program that will take a text input called "name" and store it in a database, then display the names that are stored. This is my first attempt at OOP so I'm not sure if I'm doing it right.
Any suggestions? Am I inserting the value properly? The table is called "names" and the column is "name."
Here are my two different files.This one is called template.php
<html>
<head>
</head>
<body>
<form action="template.php" method="post">
Person: <input name="person" type="text" />
<input type="submit" />
</form>
<table>
<?php
$insert_name = new MyController();
$insert_name-> getname($_POST['person']);
foreach ($names as $name); ?>
<tr>
<td><?php echo htmlspecialchars($name); ?></td>
<tr>
<?php endforeach; ?>
</table>
</body>
</html>
Now for my other file, index2.php
<?php
$connection = mysql_query("localhost","root","password") or die(mysql_error());
mysql_select_db("test",$connection) or die(mysql_error));
require_once("template.php");
class MyController
{
var $name;
function getname($new_name) {
$this->name = $new_name;
}
function insert(){
mysql_query("INSERT INTO names(name)
VALUE ( "$this->name" )");
}
function run()
{
$result = mysql_query("select * from names");
$names = array();
while ($row = mysql_fetch_array($result))
{
$names[] = $row['name'];
}
include("template.php");
}
}
$controller = new MyController();
$controller->run();
?>