당신은 사용하지 말아야 이전 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;
}
?>
은, 희망이 도움이 아니 완성 된 스크립트 단지 예 ...
형식이 읽을 수 있도록하는 질문 –