0

我在 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>";

?> 
4

2 回答 2

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;
}
?> 

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

于 2012-07-13T07:57:32.957 回答
0

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

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

于 2012-07-13T07:04:46.920 回答