Transcript JavaScript #4 - UWEC Computer Science Department
Creating PHPs to Insert, Update, and Delete Data
CS 320
Approach For Inserting Data
Create first page (HTML or PHP) to collect user inputs using an HTML form Passes inputs as
parameters
to the second page Create second page (PHP) to execute the INSERT query and display appropriate response (success or failure) depending on the query outcome Tip: write the insert query with its values hard coded before trying to write it using the values the user submits
Inserting New Data
Page 1 ProductInput.html
Page 2 ProductInsert.php
insert succeeds insert fails *Complete HTML and PHP pages are available in D2L
Review: Web
From ProductList.phpDisplaying retrieved values in form inputs: Receive the prod_id value as a parameter and store it as a variable Create a new form and specify prod_id as a hidden input Retrieve the current values for the selected prod_id's other values Create text inputs whose values are the current prod_id record values See: ViewProductRecord.php
Save prod_id parameter as a variable and retrieve current product values: <
?php
//save passed parameter as a variable $prod_id = $_REQUEST['prod_id']; //Specify the query $query = "SELECT prod_desc, prod_cost, prod_price FROM candy_product " .
"WHERE prod_id = $prod_id"; //Store the result $result = mysql_query($query) or die("SQL Error: " . mysql_error() . "
");
?
> From ViewProductRecord.php
Create a form, add prod_id as a hidden form input, display other values as form text input values:
From ViewProductRecord.phpUpdate the CANDY_PRODUCT table and display success message //save passed parameters as variables $prod_id = $_REQUEST['prod_id']; $prod_desc = $_REQUEST['prod_desc']; $prod_cost = $_REQUEST['prod_cost']; $prod_price = $_REQUEST['prod_price']; Place character data type value in single quotes //Create UPDATE query $query = "UPDATE candy_product " .
"SET prod_desc = '$prod_desc', " .
"prod_cost = $prod_cost, " .
"prod_price = $prod_price " .
"WHERE prod_id = $prod_id"; //Execute the query $result = mysql_query($query) or die("SQL Error: " . mysql_error() . "
"); //Display page content echo $msg = "Record successfully updated"; //Add code to retrieve and display CANDY_PRODUCT table contents //here so user can see result of update From UpdateProduct.php
Selecting Data Using a Hyperlink Table
Page 1: Product_Hyperlink_Table.php
Page 2: ViewProductRecord.php
Display single record layout for selected record Page 3: UpdateProduct.php
Confirmation (show message, updated product information or error message)
Deleting Data
From a business standpoint, be very cautious about allowing users to delete data Better to archive You can't delete parent records that are referenced as foreign keys
Deleting Data
Page 1 ProductListDelete.html
Page 2 ProductDelete.php
Delete succeeds Delete fails
PHP With DELETE Command
<
?php
//Put code here to connect to MySQL and specify the database //save passed parameter as variable $prod_id = $_REQUEST['prod_id']; //Create DELETE query $query = "DELETE FROM candy_product " .
"WHERE prod_id = $prod_id"; //Execute the query $result = mysql_query($query) or die("SQL Error: " . mysql_error() . "
"); //Display page content echo $msg = "Record successfully deleted"; //Add code to retrieve and display CANDY_PRODUCT table contents //here so user can see result of delete From ProductDelete.php
PHP/Database Debugging Strategies
Develop incrementally!
Add one line of code Test Repeat Use method="GET" to view parameter names and values View the page source code Make sure you are testing the most current version of your PHP page Display SQL queries directly on the Web page Then run the query in MySQL Query Workbench
To debug a query, comment out all other code and display it on the Web page:
"VALUES ('$prod_desc',$prod_cost,$prod_price)"; //Store the result //$result = mysql_query($query) //Show alternate failures message when you are done with development //or die("SQL Error: " . mysql_error() . "
"); //Display success message //echo $msg = "Record successfully inserted"; echo $query ?>
Additional PHP Resources
Tutorials Basic PHP http://www.w3schools.com/PHP/php_intro.asp
MySQL http://www.freewebmasterhelp.com/tutorials/phpmysql/1 Search Google for specific topics…