【发布时间】:2014-08-26 00:17:29
【问题描述】:
我对 PHP 中的 OOP 不是很熟悉。刚从网上得到一些简单的教训,我尝试制作一个类来动态插入、删除更新、将数据从表单上传到数据库表......
我不确定它是否真的是面向对象的..任何人都可以帮助我找到错误或让它变得更好....
要使用这个类...这里有一些规则或必要性: 1. 表单中的字段名称应与数据库中的字段名称相同。 2.“提交”按钮的名称应与表格将要插入数据的表的名称相同。 3. form(table)中的字段不能和数据库中的ANY TABLE同名。(提交按钮除外)
插入使用函数 INSERTDB.. 和 如果有图片上传使用 IMAG...
代码如下:
db.class.php
<?php class database{
var $user,$host,$pass,$db;
public function connect($user,$host,$pass,$db){
$this->user=$user;
$this->host=$host;
$this->pass=$pass;
$this->db=$db;
$this->mysqli=new mysqli($this->user,$this->host,$this->pass,$this->db);
if ($this->mysqli->connect_error) {
die('Error : ('. $this->mysqli->connect_errno .') '. $this->mysqli->connect_error);
}
}
function imag($path,$tb){
define ("MAX_SIZE","400");
$errors=0;
$imag =$_FILES["image"]["name"];
$j=date("Y.m.d");
$image=$j.$imag;
$uploadedfile = $_FILES['image']['tmp_name'];
if ($image)
{
$filename = stripslashes($_FILES['image']['name']);
$extension =substr($image,-3);
echo "<br>".$extension."<br>".$image;
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg")
&& ($extension != "png") && ($extension != "gif"))
{
echo ' Unknown Image extension ';
$errors=1;
}
else
{
$size=filesize($_FILES['image']['tmp_name']);
if ($size > MAX_SIZE*1024)
{
echo "You have exceeded the size limit";
$errors=1;
}
if($extension=="jpg" || $extension=="jpeg" )
{
$uploadedfile = $_FILES['image']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
}
else if($extension=="png")
{
$uploadedfile = $_FILES['image']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
}
else
{
$src = imagecreatefromgif($uploadedfile);
}
list($width,$height)=getimagesize($uploadedfile);
$newwidth=800;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);
$newwidth1=150;
$newheight1=($height/$width)*$newwidth1;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,
$width,$height);
imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,
$width,$height);
$filename = "../images/".$path."/". $image;
$filename1 = "../images/".$path."/s/". $image;
imagejpeg($tmp,$filename,100);
imagejpeg($tmp1,$filename1,100);
imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
}
}
//If no errors registred, print the success message
if(!$errors)
{
// mysql_query("update SQL statement ");
$this->insertdb($tb);
echo "Image Uploaded Successfully!";
}
}
function insert($tb,$field,$value){
$in= mysqli_query($this->mysqli,"INSERT INTO $tb ($field) values ($value)");
if(!$in){
die("Insert Query Failed" .mysqli_error($this->mysqli) );
}
}
function insertdb($tb){
echo $tb;
$f="";
$v="";
foreach($_POST as $key=>$value){
echo $key . " = " . $value. "<br>";
}
foreach($_POST as $key=>$value){
if(($key!==$tb)&&($key!=="image_y")){
$f=$f.mysqli_real_escape_string($this->mysqli,$key).",";
$v=$v."'".mysqli_real_escape_string($this->mysqli,$value)."',";
echo "<hr> there is no image<hr>";
}
if($key=="image_y"){
$f=$f."image,";
$v=$v."'".$_FILES['image']['name']."',";
echo "<hr> there is an image<hr>";
}
}
$f1=rtrim($f,",");
$v1=rtrim($v,",");
echo $f1 ."<br>".$v1;
$this->insert($tb,$f1,$v1);
}
function del($tb,$field,$value){
$d= mysqli_query($this->mysqli,"DELETE FROM $tb where $field = '$value' ");
if(!$d){
die("Delete Query Failed" );
}
}
function up($tb,$field,$value,$o_field,$o_value){
$u= mysqli_query($this->mysqli,"UPDATE $tb SET $field= '$value' where $o_field= '$o_value' ");
if(!$u){
die("Update Query Failed".mysqli_error($this->mysqli) );
}
}
function show($tb,$field,$value,$condition,$ans){
$s= mysqli_query($this->mysqli,"Select * from $tb where $field $condition '$value' ");
$s2=mysqli_fetch_assoc($s);
echo $s2[$ans];
if(!$s){
die("Select Query Failed".mysqli_error($this->mysqli) );
}
}
}
?>
进程.php
<?php
include"../includes/db.class.php";
$o=new database();
$o->connect("localhost","root","","saycheese");
if(isset($_POST['category'])){
$tb="category";
$o->insertdb($tb);
}
if(isset($_POST['magzine'])){
$tb="magzine";
$o->insertdb($tb);
}
if(isset($_POST['writer'])){
$tb="writer";
$folder="wr";
$o->imag($folder,$tb);
}
if(isset($_POST['images'])){
$tb="images";
$folder="mag";
$o->imag($folder,$tb);
}
?>
`
【问题讨论】:
-
当有几个像 Propel 或 Doctrine 已经被现场验证的 ORM 时,你为什么要编写自己的 ORM?您对数据库占位符和proper escaping 的鲁莽无视将使您付出严重的代价。如果您从学术角度来了解更多有关面向对象编程的信息,那很好,但您可能需要先研究其他实现,然后再开发自己的实现。
-
是的,我正在学习这些东西..我试图在网上搜索一些关于 OOPS 的彻底方法......但无法找到它,或者我发现的任何东西都飞过了我的头......所以我只是从这里和那里挑选一些信息,将它们结合起来,我希望我能很快走上正确的轨道......如果你有任何关于 OOP 教程的好的链接,请发布它们:)
-
大多数“教程”都是垃圾,不值得阅读。了解这些工作原理的最佳方法是阅读使用正确 OO 设计原则的应用程序的源代码,并探索构建软件时使用的各种design patterns。