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
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.
lab8
databaseA 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'].")";
insert.php
that allows the user to add a new item into the wishlist database table.
insertitem.php
insertitem.php
page that used PHP to get the data from the $_POST array and inserts it into the wishlist table.insertitem.php
page (we wrote the code for this during lab 8)insert.php | insertitem.php |
---|---|
We can use phpMyAdmin to help learn the SQL syntax for writing DELETE
statements to remove row(s) of data from a table.
delete.php
that allows the user to delete an item from the wishlist database table.
deleteitem.php
deleteitem.php
page that used PHP to get item name from the $_POST array and delete that row from the wishlist table.deleteitem.php
pageNote: 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.php | deleteitem.php |
---|---|