Lab 9: PHP and MySQL

Extend lab8's solution

This lab will builds off the work done for lab8. You should copy the lab8 files and database to folder lab9.

In addition to lab8's users.php, wishlist.php, and lab8.php, you will add the functionality for

Insert a new row (record) into a table

In lab 8, we used phpMyAdmin to add new rows (records) to a table.

We can use phpMyAdmin to help learn the SQL syntax for writing INSERT statements.

  1. Start phpMyAdmin and open the lab8 database
  2. Use phpMyAdmin Browse & Search tab to view the rows in the wishlist table
  3. Use phpMyAdmin as if you wanted to add another row
    • We will not add a row manually, we are just using the tool to help with SQL syntax
      • I will sometimes copy the SQL into the Windows clipboard (Ctrl-A, Ctrl-C) to paste into PHP code

A code snippet to insert a row into the wishlist table is given below:

$sql = "INSERT INTO wishlist (userid, item, price) VALUES ('xx', 'xy', 'xz')";

if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
} else {
   echo "Error: " . $sql . "" . mysqli_error($conn);
}

Typically, the data to be added is stored as a variable, often entered by the user in a HTML Form.

Rather than binding a literal value, we will bind a parameter

$sql = "INSERT INTO wishlist (userid, item, price) VALUES (".$_POST['userid'].", '".$_POST['item']."', ".$_POST['price'].")";

Use SQL to add a record to the wishlist table

  1. Create the PHP page insert.php that allows the user to add a new item into the wishlist database table.
    • Set the action of the form to call insertitem.php
  2. Create the insertitem.php page that used PHP to get the data from the $_POST array and inserts it into the wishlist table.
  3. Display the wishlist table (with the data added) on the insertitem.php page (we wrote the code for this during lab 8)
insert.phpinsertitem.php
Lab 9 insert screen snapshot apples inserted

Delete a row (record) from a table

We can use phpMyAdmin to help learn the SQL syntax for writing DELETE statements to remove row(s) of data from a table.

  1. Use phpMyAdmin SQL tab to manually enter SQL to perform a deletion
  2. Click the Run SQL Button to perform the operation

Use SQL to delete a record from a table

  1. Create the PHP page delete.php that allows the user to delete an item from the wishlist database table.
    • Set the action of the form to call deleteitem.php
  2. Create the deleteitem.php page that used PHP to get item name from the $_POST array and delete that row from the wishlist table.
  3. Display the wishlist table (with the data deleted) on the deleteitem.php page

Note: Deleting row data is done in a similar manner to inserting row data.

$sql = "DELETE FROM wishlist WHERE userid=".$_POST['userid']." AND item ='".$_POST['item']."'";

if (mysqli_query($conn, $sql)) {
    echo "Record deleted successfully";
} else {
   echo "Error deleting record: " . mysqli_error($conn);
}

lab9delete.phpdeleteitem.php
delete form deleteitem (7K)