Changing data in a database (updates, additions)

Note that no link to the file is provided since clicking on the link would actually add data to the database. The file name is update.php in database directory.


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<!--
Changing data in a database
Author: Elena Machkasova
Last Modifed: 4/7/09
-->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>
Updating data in a database
</title>
</head>
<body>
<pre>
<?php
function showerror()
{
        die("Error ". mysql_errno(). " : " .mysql_error());
}

function display_posts($connection) {
        $q1 = "SELECT post_author, post_title, post_content, post_date FROM wp_posts WHERE post_type = 'post';";

        if (! ($result = @mysql_query($q1, $connection))) {
                showerror();
        }

        while($row = @mysql_fetch_array($result,MYSQL_NUM))
        {

                foreach($row as $attribute)
                {
                print "$attribute\n";
                }
                print "<hr/>\n"; // added for readability
        }

        @mysql_free_result($result);
}

// note: the passowrd is available in the file 
// ~elenam/public_html/1101_spring09/database/update.php
$password = "";

// connect to the server
if (! ($connection = @mysql_connect("localhost","1101readwrite",$password)))
        die ("connection to the database failed");

// select a database
if (!@mysql_select_db("1101spr09", $connection)) showerror();

// WARNING: EXTREME CARE MUST BE TAKEN WHEN INSERTING DATA
// INTO THE DATABASE OR UPDATING INFORMATION
print "BEFORE THE UPDATE:\n";

display_posts($connection);

// old title: 
//$title = "Welcome to CSci 1101!";
$newtitle = "Hello there!";
$update_q1 = "UPDATE wp_posts SET post_title ='$newtitle' WHERE ID = 1;";

if (! (@mysql_query($update_q1, $connection))) {
        showerror();
}

print "AFTER UPDATE\n";

display_posts($connection);

$insert_q1 = "INSERT INTO wp_posts SET post_title = 'Testing 1-2-3',
        post_author = 2, post_date = NOW(), post_content = 'Just a test'";


if (! (@mysql_query($insert_q1, $connection))) {
        showerror();
}
display_posts($connection);

?>
</pre>
</body>
</html>

Locking tables

The script below updates comment count to the actual number of comments


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<!--
Changing data in a database
Author: Elena Machkasova
Last Modifed: 4/7/09
-->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>
Updating data in a database
</title>
</head>
<body>
<?php
function showerror()
{
        die("Error ". mysql_errno(). " : " .mysql_error());
}


// note: the passowrd is available in the file 
// ~elenam/public_html/1101_spring08/database/update.php
$password = "";

// connect to the server
if (! ($connection = @mysql_connect("localhost","1101readwrite",$password)))
        die ("connection to the database failed");

// select a database
if (!@mysql_select_db("1101spr09", $connection)) showerror();

// WARNING: EXTREME CARE MUST BE TAKEN WHEN INSERTING DATA
// INTO THE DATABASE OR UPDATING INFORMATION

// LOCKING THE TABLES: count the number of comments,
// update the wp_posts table

$lock_q = "LOCK TABLES wp_comments READ, wp_posts WRITE;";
$unlock_q = "UNLOCK TABLES;";

if (! (@mysql_query($lock_q, $connection))) {
        showerror();
}

// get the comment count for post with ID 15

$count_comment_q = "SELECT COUNT(*) FROM wp_comments WHERE comment_post_ID = 15;";

if (! ($result = @mysql_query($count_comment_q, $connection))) {
  showerror();
}

// process the comment count. Note that it is a table with one element

if ($row = @mysql_fetch_array($result,MYSQL_NUM)) {
  // successfully got the result:
  $comment_count = $row[0];
  print "<p>The post has $comment_count comments</p>";
} else {
  // failed to get the result:
  showerror();
}

// update the comment count:

$update_q1 = "UPDATE wp_posts SET comment_count = $comment_count WHERE ID = 15;";

if (! (@mysql_query($update_q1, $connection))) {
        showerror();
}

// Unlock tables:

if (! (@mysql_query($unlock_q, $connection))) {
        showerror();
}

print "<p>Update successful. Check your blog at <a href=\"http://csci1101sp09.morris.umn.edu/wordpress/?p=15#comments\">http://csci1101sp09.morris.umn.edu/wordpress/?p=15#comments</a></p>";

?>
</pre>
</body>
</html>
http://csci1101sp09.morris.umn.edu/~elenam/1101_spring09/database/update_with_lock.php

UMM CSci 1101