第一次尝试使用PHP/MySQL进行OOP


First attempt at OOP with PHP/MySQL

我试图用PHP/MySQL获得OOP的感觉,所以我试图编写一个程序,该程序将接受称为"name"的文本输入并将其存储在数据库中,然后显示存储的名称。这是我第一次尝试OOP,所以我不确定我是否做得对。

有什么建议吗?我是否正确地插入了值?表称为"names",列称为"name"。"

这是我的两个不同的文件。这个叫做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>

现在对于我的另一个文件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();
?>

您生成的HTML完全错误。你不应该混合复杂的PHP代码(例如:mysql查询)与你的HTML。这两个东西应该在完全独立的文件中,大部分PHP部分应该在它自己的类中。例如:

index2.php

<?php
require_once("dbinsert.php");
class MyController
{
  function run()
  {
    $insert_name = new datainsert();
    $insert_name->setname($_POST['person']);
    $result = mysql_query("select * from names");
    $names = array();
    while ($row = mysql_fetch_array($result))
    {
      $names[] = $row['name'];
    }
    include("my-template.php");
  }
}
$controller = new MyController();
$controller->run();

my-template.php

<html>
<head>
</head>
<body>
<form action="index2.php" method="post"> 
Person: <input name="person" type="text" /> 
<input type="submit" />
</form>
<table>
  <?php foreach ($names as $name); ?>
    <tr>
      <td><?php echo htmlspecialchars($name); ?></td>
    <tr>
  <?php endforeach; ?>
</table>
</body>
</html>

或者,使用合适的模板语言,比如Smarty。

在代码段的第二部分,开始标记是<?php而不是<?。另一件事是在try..catch块内包装你的连接数据库查询,这样就更容易知道什么时候有错误。更好的做法是使用PDO来连接数据库。为什么?已经有很多关于它的文章了。其中一个在这里,我想和大家分享一下http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/

而且,最佳实践是在插入到数据库之前对输入进行消毒。为了避免SQL注入,应该对处理发布数据的方法成员进行清理;所以我建议你这样做:

function setname($sent_name){
            $sent_name = mysql_real_escape_string($sent_name);
            $this-> insert_name = $sent_name ;
}

当创建一个作为新对象调用的类时(如果不使用简单的静态变量),您可能希望创建一个构造函数,其中创建私有变量的初始状态。常规约定也是使用大写字母作为类名。所以,在你的类中,你可能想这样做:

class DataInsert{
var $insert_name;
function __construct(){
//initialize
}
function setname($sent_name){
            $sent_name = mysql_real_escape_string($sent_name);
            $this-> insert_name = $sent_name ;
}
function dbinsert(){
    mysql_query("INSERT INTO names(name) 
    VALUE ( "$this->insert_name" )");       
    }
}

希望有帮助。最后,祝您使用PHP愉快。接下来的事情是学习MVC部分(如果你还没有接触过这样的设计模式),那里有一些框架可用于PHP;

我自己已经有一段时间没有做太多PHP了,因为我现在主要关注ruby on rails和node.js。我认为rails尤其有趣。所以,给你的另一个建议是,以后再看看它们(同样,如果你还不知道它们)。谢谢。