Presentation is loading. Please wait.

Presentation is loading. Please wait.

>> PHP: Insert Query & Form Processing. Insert Query Step 1: Define Form Variables Step 2: Make DB Connection Step 3: Error Handling Step 4: Define the.

Similar presentations


Presentation on theme: ">> PHP: Insert Query & Form Processing. Insert Query Step 1: Define Form Variables Step 2: Make DB Connection Step 3: Error Handling Step 4: Define the."— Presentation transcript:

1 >> PHP: Insert Query & Form Processing

2 Insert Query Step 1: Define Form Variables Step 2: Make DB Connection Step 3: Error Handling Step 4: Define the Query Step 5: Run and Process the Query Web-based Systems | Misbhauddin 2

3 Insert Query - Definition Step 4: Define the Query – INSERT INTO dbname VALUES (value1, value2, … ………., valueN) – INSERT INTO dbname SET column1 = value1, column2=value2,…………..column=valueN Web-based Systems | Misbhauddin 3 Note: Strings in ‘ ‘(single quotes)

4 Insert Query - Run Step 5: Run the Query – $result = mysqli_query(); Process the Result – What is the result? It is a Boolean – If $result==1 => Success – Else => Failure Web-based Systems | Misbhauddin 4 Note: Strings in ‘ ‘(single quotes)

5 Processing File 1.Create a new file called add-process.php 2.Open the file and accept the submitted values from the form (using the $_POST variable) 3.Write Insert Query to add the item to the database TRY NOW Will this work? TRY NOW 1.Open the add-item.php file 2.Go down to the form tag 3.Add the attributes 1.method 2.action

6 Processing method=“GET” (Access using the $_GET Array) method=“POST” (Access using the $_POST Array) FORM Passing & Processing Form Variables Re-direction after Form Submission add-item.php add-process.php add-thanks.php action=“add-process.php” header(“Location: contact-thanks.php” action=“add-item.php” Approach 1 Approach 2 Approach 3

7 Redirecting After a Form Submission Approach 1: Within the process file 1.Do as before 2.Go to the add-process.php file 3.Under the previous written code, create tags for new page a.Step 1: Create $title variable b.Step 2: Create $section variable c.Step 3: Include the header.php d.Step 4: Write the div tag with class “content” e.Step 5: Write a div tag with classes “section page” f.Step 6: Write a heading 1 tag with text “Add Item”. g.Step 7: Write a paragraph tag with text “Item Added to the Database Successfully” h.Step 8: Include the footer.php TRY NOW ISSUE Using back can resubmit the form again and send another email

8 Redirecting After a Form Submission Approach 2: Redirect to a new page 1.Create a new file add-thanks.php 2.Copy all the code from the previous add-process.php file we wrote to display the confirmation into this new page. TRY NOW 1.Open the file add-process.php 2.Add the following line at the end header(‘Location: add-thanks.php’); TRY NOW For this to work, we need to add a redirect in the add-process.php Method for the server and the browser to communicate before any HTML gets passed

9 Redirecting After a Form Submission Approach 3: On the same page 1.Open the file add-item.php 2.In the form tag 1.Change the action attribute to “add-item.php” 3.Open the file add-thanks.php and copy all the code to the beginning of the add-item.php file 4.Modify the header command and change the page to add-item.php. 5.Add a command exit; after the header function TRY NOW Avoids execution of other php code in the file Mind Exercise How can we display the confirmation message and remove the form within the same add-item.php file?

10 isset() PHP method Determines whether a certain variable has already been declared by PHP Returns a Boolean value true if the variable has already been set and false otherwise or if the variable is set to the value NULL Accepts an arbitrary amount of parameters Returns true if all the variables have been defined otherwise, it returns false USAGE if(isset($_GET[“status”])) { }

11 11 Selective Display : Last Class Web-Based Systems - Misbhauddin Display this only when item is added to the database Display this only when the query failed Display this in the beginning HINT Use isset() function Use the header function with GET Declare a status variable Use it as conditional in the HTML Body


Download ppt ">> PHP: Insert Query & Form Processing. Insert Query Step 1: Define Form Variables Step 2: Make DB Connection Step 3: Error Handling Step 4: Define the."

Similar presentations


Ads by Google