Php-mysql Course

Once tables contain some data, you can edit and change those existing records with UPDATE statement.
The UPDATE command is used to change existing records in a table.

  - Syntax:
UPDATE table_name
 SET column1=value, column2=value2,...
 WHERE some_column=some_value
- The WHERE clause is important in a UPDATE query, it tells MySQL which record or records should be updated. If you omit the WHERE clause, all rows will be affected!
- The UPDATE statement is sent to the MySQL server with the query() method of the mysqli object.

  - Example
In the previous lessons was created a table named "users" and we added some data in it. Here is how it looks the first two records.
idnamepasswordemailreg_date
1 Marius faith name@domain.net 2011-03-24 09:51:46
2 MarPlo peace user@domain.com 2011-03-24 10:10:27

In this example, we'll change the email for the user with the "name" MarPlo.
<?php
// connect to the "tests" database
$conn = new mysqli('localhost', 'root', 'pass', 'tests');

// check connection
if (mysqli_connect_errno()) {
  exit('Connect failed: '. mysqli_connect_error());
}

// UPDATE sql query
$sql = "UPDATE `users` SET `email`='new_mail@domain.net' WHERE `name`='MarPlo' AND `id`=2";

// perform the query and check for errors
if (!$conn->query($sql)) {
  echo 'Error: '. $conn->error;
}

$conn->close();
?>
As you can see, the WHERE clause sets two conditions with the "AND" operator (WHERE `name`='MarPlo' AND `id`=2), this tells MySQL to update only the rows that have "name='MarPlo'" and "id=2". Setting these two conditions, we are shure that only that row will be updated, not other row with the same name.
Also, you can apply the LIMIT option to set how many rows to be updated.
Example:
                $sql = "UPDATE `users` SET `email`='new_mail@domain.net' WHERE `name`='MarPlo' AND `id`=2 LIMIT 1";

After the update, the first two rows in "users" table will look like this:
idnamepasswordemailreg_date
1 Marius faith name@domain.net 2011-03-24 09:51:46
2 MarPlo peace new_mail@domain.com 2011-03-27 10:20:58
- Becouse "reg_date" is a TIMESTAMP column (with attribute:   on update CURRENT_TIMESTAMP ), MySQL server has updated its value too, changing it with the current date and time when the update was performed.
If you want an update to not affect the value of a TIMESTAMP column, when you create the table you must not add the "on update" attribute, only the DEFAULT ( `column_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ).

- If you perform an update query that doesn't actually change any values (becouse the WHERE condition doesn't match any row), you won't see any errors but no rows will be affected.
- It's indicated to not change the value of a primary-key column (in the example above, the "id" column).

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which attribute is used in <a> tag for the address of the link?
src href rel
<a href="http://coursesweb.net/" title="CoursesWeb.net">CoursesWeb.net</a>
Which CSS property sets the type of the text font?
font-family text-decoration font-size
h2 {
  font-family:"Calibri",sans-serif;
}
What instruction selects all the <div> tags with class="cls"?
querySelector("div.cls") getElementsByTagName("div") querySelectorAll("div.cls")
var elm_list = document.querySelectorAll("div.cls");
var nr_elms = elm_list.length;       // number of selected items
alert(nr_elms);
Indicate the function that can be used to get the sum of values in an array.
array_sum() array_diff() array_shift()
$arr =[1, 2, 3, 4);
$arr_sum = array_sum($arr);
echo $arr_sum;       // 10
PHP MySQL - UPDATE

Last accessed pages

  1. Get and Modify content of an Iframe (32494)
  2. The Mastery of Love (7690)
  3. Download HTM resources (90)
  4. HTML5 Quick Tutorial (5041)
  5. Styling HTML table with CSS (412)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (715)
  2. CSS cursor property - Custom Cursors (94)
  3. Read Excel file data in PHP - PhpExcelReader (71)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (67)
  5. PHP Unzipper - Extract Zip, Rar Archives (53)