<?php class Mysql { //私有的静态属性(储存实例化对象) private $link; private static $obj; //私有构造函数(目的禁止实例化对象) private function __construct($host, $username, $password, $dbName) { $this->link = mysqli_connect($host, $username, $password, $dbName); } //公有的静态方法(实例化对象)入口 public static function newObj($host, $username, $password, $dbName) { if (self::$obj instanceof Mysql) { return self::$obj; } return self::$obj = new Mysql($host, $username, $password, $dbName); } //禁示克隆 private function __clone() { } //添加 function insert($list, $arr, $data) { $sql = "insert into {$list}"; $sql .= " (" . implode(",", array_keys($arr)) . ")"; $sql .= " value ({$data})"; // var_dump($sql); // die(); $res = mysqli_query($this->link, $sql); if ($res) { return trut; } else { return false; } } //删除 function delete($list, $id) { $sql = "delete from {$list} where id={$id}"; $res = mysqli_query($this->link, $sql); if ($res) { return trut; } else { return false; } } //修改 function update($list, $data, $id) { $sql = "update {$list} set {$data} where id={$id}"; $res = mysqli_query($this->link, $sql); if ($res) { return trut; } else { return false; } } //单条查看 function find($list, $id) { $sql = "select * from {$list} where id={$id}"; $res = mysqli_query($this->link, $sql); $data = mysqli_fetch_assoc($res); return $data; } //查询多条 function getAll($list) { $sql = "select * from {$list}"; $res = mysqli_query($this->link, $sql); $data = mysqli_fetch_all($res, 1); return $data; } }
//添加
<?php extract($_REQUEST); include "Mysql.php"; //$s=implode(",",array_keys($_REQUEST)); //var_dump($s); //die(); $obj=Mysql::newObj("127.0.0.1","root","root","week1"); $All="'$enterprise','$firm','$legal','$number','$type','$email'"; $data=$obj->insert("firmtable",$_REQUEST,"$All"); if($data){ echo "<script>alert('添加成功');location.href='form.php'</script>"; }else{ echo "<script>alert('添加失败');location.href='add.php'</script>"; }