[英]First attempt at OOP with PHP/MySQL
我试图通过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();
?>
您正在错误地生成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。 我自己喜欢。
在代码段的第二部分,开始标记是<?php
not <?
。 另一件事是将连接数据库查询包装在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。
自从我现在主要专注于Rails和Node.js上的ruby以来,我自己已经有一段时间没有做太多PHP了。 我认为与Rails一起工作尤其有趣。 因此,对您的另一建议是将来查看它们(同样,如果您还不了解它们的话)。 谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.