首次尝试使用PHP / MySQL进行OOP

时间:2011-12-18 20:01:23

标签: php mysql oop

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

有什么建议吗?我是否正确插入了值?该表称为“名称”,列为“名称”。

以下是我的两个不同文件。这个名为 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();

?>

2 个答案:

答案 0 :(得分:1)

您生成的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();

我-的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。我自己更喜欢它。

答案 1 :(得分:0)

在代码段的第二部分,开场代码为<?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的框架;即.cake,zend。

我自己一段时间没有做太多PHP,因为我现在主要关注rails和node.js上的ruby。我认为使用rails特别是拖曳工作会更有趣。所以,另一个建议是将来再看看它们(再次,如果你还不知道它们)。感谢。