Manipulating Table Data

Manipulating Table Data

Since you've practiced executing a few SQL commands that manipulate database objects, you're ready to work with the data in your tables.

Adding Data

Naturally, you'll need to add rows to your tables. To add a purchase to your new purchases table, you'll use an INSERT statement in your query. Figure shows how this is done.

Using a predefined INSERT statement in insert.php

$connection = DB::connect("mysql://$db_username:$db_password@$db_host/$db_database");
if (DB::isError($connection)){
die ("Could not connect to the database: <br />". DB::errorMessage($connection));
$query = "INSERT INTO `purchases` VALUES (NULL,'mdavis',2,NULL)";
$result = $connection->query($query);
if (DB::isError($result)){
die("Could not query the database: <br />". $query." ".DB::errorMessage($result));
echo "Inserted successfully!";

When you call up insert.php, in your browser, you get:

Inserted successfully!

Figure shows that the new row made it to the database by selecting all rows from purchases.

Validating that our new row is in the database

 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows