PHP MySQL: Update Data (original) (raw)

Skip to content

Summary: in this tutorial, you will learn how to update data in a MySQL table using PHP.

To update data in MySQL from PHP, you follow these steps:

Updating data

The following update.php script sets the value in the completed column of row id 1 in the tasks table to true:

`<?php require_once 'config.php';

try { conn=newPDO("mysql:host=conn = new PDO("mysql:host=conn=newPDO("mysql:host=host;dbname=$dbname", username,username, username,password);

$sql = 'update tasks 
        set completed=:completed 
        where id=:id';

<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>t</mi><mi>m</mi><mi>t</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">stmt = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6151em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal">m</span><span class="mord mathnormal">t</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>conn->prepare($sql);

$stmt->execute([':completed' => true, ':id' => 1]);

} catch (PDOException $e) { die($e); }`Code language: PHP (php)

How it works.

First, include the config.php that contains the database configuration.

require_once 'config.php';Code language: PHP (php)

Second, establish a new connection to the todo database on the MySQL server:

$conn = new PDO("mysql:host=$host;dbname=$dbname", <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>u</mi><mi>s</mi><mi>e</mi><mi>r</mi><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo separator="true">,</mo></mrow><annotation encoding="application/x-tex">username, </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">u</span><span class="mord mathnormal" style="margin-right:0.02778em;">ser</span><span class="mord mathnormal">nam</span><span class="mord mathnormal">e</span><span class="mpunct">,</span></span></span></span>password);Code language: PHP (php)

Third, construct an UPDATE statement with two parameterized placeholders:

$sql = 'update tasks set completed=:completed where id=:id';Code language: PHP (php)

Fourth, prepare the statement for the execution:

<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>t</mi><mi>m</mi><mi>t</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">stmt = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6151em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal">m</span><span class="mord mathnormal">t</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>conn->prepare($sql);Code language: PHP (php)

Finally, execute the UPDATE statement with data:

$stmt->execute([':completed' => true, ':id' => 1]);Code language: PHP (php)

If an error occurs, the script displays it and is terminated:

die($e);Code language: PHP (php)

Summary

Was this tutorial helpful?