用于编辑和删除当前行的php代码

时间:2012-07-13 06:57:30

标签: php

我在php中创建了一个代码,用于显示mysql数据库中的记录。现在我想通过我的网页编辑和删除dataabse中的行。如何在PHP中实现?我的数据库表不包含id字段

<?php

mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("newlog1") or die(mysql_error());
$key=mysql_query("select id from newregister1") or die(mysql_error());
$result=mysql_query("select * from newregister1 where id='$key'") or die(mysql_error());

echo "<table border='1'>";
echo "<tr><th>NAME</th><th>EMAILID</th><th>ADDRESS</th><th>CONTACT</th><th>STATE</th><th>CITY</th><th>EDIT</th></tr>";

while($row=mysql_fetch_array($result)) {
  echo "<tr><td>";
  echo $row['name'];
  echo "</td><td>";
  echo $row['emailid'];
  echo "</td><td>";
  echo $row['address'];
  echo "</td><td>";
  echo $row['contact'];
  echo "</td><td>";
  echo $row['state'];
  echo "</td><td>";
  echo $row['city'];
  echo "</td><td>";
  echo "Edit";
  echo "</td><td>";
  echo "Delete";
  echo "</td></tr>"
} 
echo "</table>";

?> 

2 个答案:

答案 0 :(得分:1)

首先你不应该使用旧的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;
}
?> 

希望它有所帮助,它不是一个完成的脚本只是一个例子......

答案 1 :(得分:0)

使用SQL查询更新数据库中的行UPDATE:UPDATE users SET name = 'new_name' WHERE id = '5' MySQL update

使用其他SQL查询删除行 - 删除:DELETE FROM users WHERE id = '5' MySQL delete