首先,您不应该使用旧的 mysql_* 函数,但我不会进入,因为它代表您有 sql 注入。
最简单的方法是为您希望页面执行的每个操作创建一个模型,添加编辑删除等,然后有一个控制器来处理调用哪个方法。在我的示例中为简单起见,我将使用基于$_GET['do']
参数的开关。
然后取决于交换机的哪个部分正在运行,将在您的模型中调用不同的方法。
<?php
//Open a connection to the db
try {
$db = new PDO("mysql:host=localhost;dbname=newlog1", 'root', 'yourPW');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}catch (Exception $e){
die('Cannot connect to mySQL server.');
}
/**
* The contacts model
*/
class contacts_object{
//Pass the db connection to this class
function __construct(PDO $db){
$this->db = $db;
}
function fetch_all(){
$sql = 'SELECT * FROM newregister1';
$statement = $this->db->prepare($sql);
$statement->execute();
return $statement->fetchAll(PDO::FETCH_ASSOC);
}
function fetch_by_id($id){
$sql = 'SELECT * FROM newregister1 WHERE id=:id';
$statement = $this->db->prepare($sql);
$statement->bindParam(':id', $this->user, PDO::PARAM_INT);
$statement->execute();
return $statement->fetch(PDO::FETCH_ASSOC);
}
function add($data){
$sql = 'INSERT into newregister1 (name, emailid, address, contact, state, city)
VALUES (:name, :emailid, :address, :contact, :state, :city)';
$statement = $this->db->prepare($sql);
$statement->bindParam(':name', $data['name'], PDO::PARAM_STR);
$statement->bindParam(':emailid', $data['emailid'], PDO::PARAM_STR);
$statement->bindParam(':address', $data['address'], PDO::PARAM_STR);
$statement->bindParam(':contact', $data['contact'], PDO::PARAM_STR);
$statement->bindParam(':state', $data['state'], PDO::PARAM_STR);
$statement->bindParam(':city', $data['city'], PDO::PARAM_STR);
$statement->execute();
}
function update($data, $id){
$sql = 'UPDATE newregister1 SET name=:name, emailid=:emailid, address=:address, contact=:contact, state=:state, city=:city
WHERE id=:id';
$statement = $this->db->prepare($sql);
$statement->bindParam(':id', $id, PDO::PARAM_INT);
$statement->bindParam(':name', $data['name'], PDO::PARAM_STR);
$statement->bindParam(':emailid', $data['emailid'], PDO::PARAM_STR);
$statement->bindParam(':address', $data['address'], PDO::PARAM_STR);
$statement->bindParam(':contact', $data['contact'], PDO::PARAM_STR);
$statement->bindParam(':state', $data['state'], PDO::PARAM_STR);
$statement->bindParam(':city', $data['city'], PDO::PARAM_STR);
$statement->execute();
}
function delete($id){
$sql = 'DELETE FROM newregister1 WHERE id=:id';
$statement = $this->db->prepare($sql);
$statement->bindParam(':id', $this->user, PDO::PARAM_INT);
$statement->execute();
}
}
//Example usage
$contact = new contacts_object($db);
//URL: http://example.com/admin/?do=add
$do = null;
if(isset($_GET['do'])){
$do = $_GET['do'];
}
switch($do){
case "add":
if($_SERVER['REQUEST_METHOD']=='POST'){
//Do some validation
if(name, emailid, address, contact, state, city are set){
$data = array('name'=>$_POST['name'],
'emailid'=>$_POST['emailid'],
'address'=>$_POST['address'],
'contact'=>$_POST['contact'],
'state'=>$_POST['state'],
'city'=>$_POST['name']);
$contact->add($data);
}
}
//Echo your add form ect
break;
case "edit":
//EG:http://example.com/admin/?do=edit&id=13
if(!empty($_GET['id']) && is_numeric($_GET['id'])){
$result = $contact->fetch_by_id($_GET['id']);
}else{
exit(header('Location: http://example.com/admin/'));
}
//Handle the update
if($_SERVER['REQUEST_METHOD']=='POST'){
//Do some validation eg
if(name, emailid, address, contact, state, city are set){
$data = array('name'=>$_POST['name'],
'emailid'=>$_POST['emailid'],
'address'=>$_POST['address'],
'contact'=>$_POST['contact'],
'state'=>$_POST['state'],
'city'=>$_POST['name']);
$contact->update($data, $_POST['id']);
}
}
//Echo your edit form ect and loop through the $result Array
break;
case "delete":
//EG:http://example.com/admin/?do=delete&id=13
if(!empty($_GET['id']) && is_numeric($_GET['id'])){
$contact->delete($_GET['id']);
exit(header('Location: http://example.com'));
}
break;
default:
//EG http://example.com/admin/
$result = $contact->fetch_all();
//Echo your front page act and loop through the $result Array for all contacts
break;
}
?>
希望它有所帮助,它不是一个完成的脚本只是一个例子......