Rumah  >  Artikel  >  pembangunan bahagian belakang  >  PHP MySQL:更新数据

PHP MySQL:更新数据

藏色散人
藏色散人asal
2018-11-03 10:23:524581semak imbas

简介:在本教程中,您将学习如何使用PHP PDO预处理语句更新MySQL表中的数据。

推荐相关视频教程:MySQL教程

我们将使用示例数据库中的tasks 表进行练习。如果您尚未创建表,请按照PHP MySQL创建表教程首先完成。

下图说明了该tasks表的结构。

tasks-table.png

要更新表中的数据,请使用以下步骤:

首先,通过创建新的PDO对象连接到MySQL数据库。

其次,构造一个UPDATE语句  来更新数据。如果要将值传递给UPDATE语句,请使用命名的占位符,例如:name。

然后,使用包含语句中指定的命名占位符的相应输入值的数组调用对象的execute()  方法。PDOStatementUPDATE

PHP MySQL:更新数据示例

PHP MySQL - 更新单行

我们来看看下面的UpdateDataDemo课程。

<?php
 
/**
 * PHP MySQL Update data demo
 */
class UpdateDataDemo {
 
    const DB_HOST = &#39;localhost&#39;;
    const DB_NAME = &#39;classicmodels&#39;;
    const DB_USER = &#39;root&#39;;
    const DB_PASSWORD = &#39;&#39;;
 
    /**
     * PDO instance
     * @var PDO
     */
    private $pdo = null;
 
    /**
     * Open the database connection
     */
    public function __construct() {
        // open database connection
        $connStr = sprintf("mysql:host=%s;dbname=%s", self::DB_HOST, self::DB_NAME);
        try {
            $this->pdo = new PDO($connStr, self::DB_USER, self::DB_PASSWORD);
        } catch (PDOException $e) {
            die($e->getMessage());
        }
    }
 
    /**
     * Update an existing task in the tasks table
     * @param string $subject
     * @param string $description
     * @param string $startDate
     * @param string $endDate
     * @return bool return true on success or false on failure
     */
    public function update($id, $subject, $description, $startDate, $endDate) {
        $task = [
            &#39;:taskid&#39; => $id,
            &#39;:subject&#39; => $subject,
            &#39;:description&#39; => $description,
            &#39;:start_date&#39; => $startDate,
            &#39;:end_date&#39; => $endDate];
 
 
        $sql = &#39;UPDATE tasks
                    SET subject      = :subject,
                         start_date  = :start_date,
                         end_date    = :end_date,
                         description = :description
                  WHERE task_id = :taskid&#39;;
 
        $q = $this->pdo->prepare($sql);
 
        return $q->execute($task);
    }
 
    /**
     * close the database connection
     */
    public function __destruct() {
        // close the database connection
        $this->pdo = null;
    }
 
}
 
$obj = new UpdateDataDemo();
 
if ($obj->update(2, &#39;MySQL PHP Update Tutorial&#39;, 
                    &#39;MySQL PHP Update using prepared statement&#39;, 
                    &#39;2013-01-01&#39;, 
                    &#39;2013-01-01&#39;) !== false)
    echo &#39;The task has been updated successfully&#39;;
else
    echo &#39;Error updated the task&#39;;

脚本如何工作。

首先,通过PDO在UpdateDataDemo类的构造函数中创建新实例来连接到数据库。

其次,在 update()方法中,UPDATE使用命名占位符构造  语句。

然后,使用预准备UPDATE语句为执行语句准备语句并使用数组参数执行它。

您可以使用以下脚本更新ID为2的行:

$obj = new UpdateDataDemo();
 
if($obj->update(2,
 &#39;MySQL PHP Update Tutorial&#39;,
 &#39;MySQL PHP Update using prepared statement&#39;, 
 &#39;2013-01-01&#39;,
 &#39;2013-01-01&#39;) !== false)
 echo &#39;The task has been updated successfully&#39;;
else 
 echo &#39;Error updated the task&#39;;

您可以从表中查询数据tasks以验证更新:

SELECT * FROM tasks;

在本教程中,您学习了如何使用PHP PDO预处理语句更新MySQL表中的数据。

Atas ialah kandungan terperinci PHP MySQL:更新数据. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn