Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

NB: The database management system installed on the weblab server is MariaDB, wh

ID: 3712306 • Letter: N

Question

NB: The database management system installed on the weblab server is MariaDB, which uses the MySQL calls; you will need to use mysqli_ API functions for this assignment. Just follow the examples in the link that's in Part 1 and you'll be OK.

Part 1; From databases to forms: Read Database Access with PHP. Copy your form from Lab Exercise6 to l7p1.php and change it to read the states from a database table instead of putting them into a PHP array yourself.

The database is named weblab and the table of states is named state_t. The table was created as follows:

Display the state name in the drop-down, but transmit the state abbreviation through the form. You do this by using a value attribute on

the <option> element. The form area for State will look something like the following. Of course, you have to build this using PHP, and not just type it in.

You will not use the state_zone attribute. In "real life" it would be used for calculating shipping, maybe.

Present the state names in alphabetical order on your form. The easy way to do this is to have the database management system sort them for you using an ORDER BY clause in your SQL. For those of you who took Database long ago and far away (or not at all!), a suitable query for populating the array is this:

Make a link to this program from your index page.

This is my code. I can not get it to show.

<!DOCTYPE html>

<html lang="en">

<head>

<title>Tools Order Form</title>

<script>

function validateForm() {

var a=document.getElementById("plyr_qty").value;

var b=document.getElementById("chsl_qty").value;

var c=document.getElementById("hoe_qty").value;

if(isNaN(a)){

alert("Enter a numeric value for Plier Quantity");

return false;

}else if(isNaN(b)){

alert("Enter a numeric value for Chisel Quantity");

return false;

}else if(isNaN(c)){

alert("Enter a numeric value for Hoe Quantity");

return false;

}else{

var price= (a*15)+(b*20)+(c*30);

var total=price*1.07;

if (confirm("The total cost of order is: "+total) == true)

{

return true;

}

else{ return false;

}

}

}

</script>

</head>

<body>

<h1>Tool Invoice Form</h1>

<form name="myForm" action="/formtest.php"

onsubmit="return validateForm()" method="post">

<label>Customer's Name:

<input type="text" name="Customer's Name" size="30"/>

</label>

<br>

<br>

<label> Street Address:

<input type="text" name="street"

size="30"/>

</label>

<br>

<br>

<label>City:

<input type="text" name="city"

size="30"/>

</label>

<br>

<br>

<label>State:

<select name="Choose your state"/>

<?php

$db=mysqli_connect(null,null,null,'weblab')

or die("Can't connect to DB:" . mysqli_connect_error());

$q = "select state_abbr, state_name ";

$q .= "from state_t ";

$q .= "order by state_name;";

$dbResult = mysqli_query($db,$q);

or die("Database query error" . mysqli_error($db));

$num = mysqli_num_rows($dbResult);

if ($num == 0) {

echo '<tr><td colspan="2">';

echo 'Database query retrieved zero rows.</td></tr>';

}

while ($row = mysqli_fetch_assoc($dbResult)) {

$state_abbr = $row['state_abbr'];

$state_name = $row['state_name'];

echo "<tr><td><b>$role</b></td>";

echo "<td>$actor</td></tr> ";

}

?>

</label>

<br>

<br>

<label>Zip:

<input type="text" name="zip" size="7"/>

</label>

<br>

<br>

<table>

<tr>

<th>Product Name</th>

<th>Price</th>

<th>Quantity</th>

</tr>

<tr>

<td>Pliers</td>

<td>$15.00</td>

<td>

<input type="text" id="plyr_qty" name="pliers" size="2"/>

</td>

</tr>

<tr>

<td>Chisels</td>

<td>$20.00</td>

<td>

<input type="text" id="chsl_qty" name="chisel" size="2"/>

</td>

</tr>

<tr>

<td>Hoe</td>

<td>$30.00</td>

<td>

<input type="text" id="hoe_qty" name="hoe" size="2"/>

</td>

</tr>

</table>

<h2>Payment Method:</h2>

<label>

<input type="radio" name="payment" value="visa" checked="checked"/>

Visa

</label>

<br />

<label>

<input type="radio" name="payment" value="mc"/>

Master Card

</label>

<br />

<label>

<input type="radio" name="payment" value="discover"/>

Discover

</label>

<br />

<label>

<input type="radio" name="payment" value="american express"/>

American Express

</label>

<br />

<p>

<input type="submit" value="Submit Order"/>

<input type="reset" value="Clear Order Form"/>

</p>

</form>

</body>

</html>

Explanation / Answer

ANS:-

Given that,

<!DOCTYPE html>

<html>
   <head>
       <title>Lab 8</title>
       <meta charset="UTF-8">
       <link rel="shortcut icon" href="http://webdev.spsu.edu/favicon.ico" type="image/x-icon" />
       <link rel="stylesheet" type="text/css" href="l2p4.css" />
   </head>
   <body>
       <h1>Fruit for Sale!</h1>
       <h2>Price is based on unit weight</h2>
       <form action="http://webdev.spsu.edu/formtest.php" method="post" id="form">
           <table>
               <tr>
                   <th>Fruit</th>
                   <th>Price per Pound ($/lb)</th>
                   <th>Weight</th>
                   <th>Quantity</th>
               </tr>
               <?php
                   $db = pg_connect("dbname=webdev");
                   $query = "select fruit_item_no, fruit_name, fruit_price, fruit_weight";
                   $query .= " from fruit_t order";
                   $query .= " by fruit_name;";              
                   $dbResult = pg_query($query);

                   if(!$dbResult){
                       die("Database error....");
                   }
                      
                   $num = pg_num_rows($dbResult);

                   if($num == 0) {
                       echo '<tr><td colspan="2">';
                       echo 'Database query retrieved nothing!</td></tr>';
                   }

                   $i = 0;
                   while($i < $num) {
                       $fruit_item_no = pg_Result($dbResult, $i, 'fruit_item_no');
                       $name = pg_Result($dbResult, $i, 'fruit_name');
                       $price = pg_Result($dbResult, $i, 'fruit_price');
                       $weight = pg_Result($dbResult, $i, 'fruit_weight');

                       echo '<tr><td>'.$name.'</td><td>'.$price.'</td><td>'.$weight.'.</td><td>';
                       echo '<input type="text" name="'.$fruit_item_no.'" id="'.$fruit_item_no.'" size="10" /></td></tr>';
                       $i++;
                   }
               ?>
           </table>

           <divi class="custInfo">
               <label>First Name
                   <input type="text" id="fname" name="firstName" size="30" />
               </label>
               <label>Last Name
                   <input type="text" id="lname" name="lastName" size="30" />
               </label>
  
               <br />          
  
               <label>Street Address
                   <input type="text" id="streetAdr" name="streetAddress" size="45" />
               </label>

               <br />

               <label>City
                   <input type="text" id="city" name="City" size="30" />
               </label>

              
               <label>State</label>
              
               <select id="state" name="State">
                   <?php
                       $db=pg_connect("dbname=webdev");
                       $query = "select state_abbr, state_name from state_t order by state_name";

                       $dbResult = pg_query($query);

                       if(!$dbResult){
                           die("Database error....");
                       }
                      
                       $num = pg_num_rows($dbResult);

                       if($num == 0) {
                           echo '<p>';
                           echo 'Database query retrieved nothing!</p>';
                       }

                       $i = 0;
                       while($i < $num) {
                           $state = pg_Result($dbResult, $i, 'state_name');
                           $abbr = pg_Result($dbResult, $i, 'state_abbr');

                           if($state == "Georgia") {
                               echo '<option value="'.$abbr.'" selected="selected">'.$state.'</option>';
                               echo " ";
                           } else {
                               echo '<option value="'.$abbr.'">'.$state.'</option>';
                               echo " ";
                           }
                           $i++;
                       }
                   ?>
               </select>
               <label>Zip Code
                       <input type="text" id="zip" name="zipCode" size="10" />
               </label>

              
               <br />
              
               <label>Payment:
                   <br />
                   <input type="radio" name="radio" name="payment" value="visa" />Visa
                   <input type="radio" name="radio" name="payment" value="master" />Mastercard
                   <input type="radio" name="radio" name="payment" value="discover" />Discover
                   <input type="radio" name="radio" name="payment" value="amex" />American Express
               </label>

               <br />
               <br />
               <input type="submit" value="Submit" />
           </div>
       </form>

       <br /><br />
       <p>Part 3:</p>
       <p>I used it for my name and id field for the text box. This way when I go to look up the fruit again later on I already have the primary key and thus have to supply less information.
           I can send back the primary key and the quantity and everything else I can look up. Now if prices change frequently this may not be a good way to do it initially since then I could possibly give them
           the wrong price but only fairly staticly priced items it should be fine.
       </p>

       <p>Part 4:</p>
       <p>To get the validation to work I would have to connect to the database and figure out what possible fields I have, based on how many unique ID's I have. From there it wouldn't require much to change, put the various ID's into an
           array. Then loop over them and check to make sure it's a number.
       </p>

       <p>Part 5:</p>
       <p>The small time I was in your course I had no problems with it. I was hoping to learn more PHP and database driven sites than what we did. I was also hoping to learn more frameworks and the backend of stuff more. I know this wasn't meant to be an advanced web programming course and thus basics have to be covered.
           I know you would like a list but overall this was my only complaint which isn't much of one to begin with. Hope you have a great summer!
       </p>
   </body>
</html>