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

using an ordinary ASCII text editor such as nanoor vi and you will need to use m

ID: 3859760 • Letter: U

Question

using an ordinary ASCII text editor such as nanoor vi and you will need to use mysqli_ API functions for this

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:

Part 2, More databases to forms: The database weblab has a table of tools named tool_t. The table was created as follows:

Change your order form to construct the item names, prices and weights by extracting the items from the database rather than hard-coding them. Display every item from the database. Do not assume that there will be a given number of items.

In the database table definition above, numeric(6, 2) means the item has a total of six digits, of which two are to the right of the decimal point.

Your order form should display the following items for each tool:

Name

Price

Shipping Weight

Each idem of your form should have a box to allow the customer to enter quantity as you did in earlier assignments.

You will not need the tool_picture or tool_description attributes. Present the tool names in alphabetical order. The following is a suitable query for retrieving from the database:

This is a modification of l7p1.php, and I will test it when I test Part 1. Your program will still be named l7p1.php.

Note: This change will "break" your JavaScript validation routines if you still have them in your form. In real life, you would have to fix this. However, in real life, you would have started with the database-driven form, so you would not have to back-track. You do not have to change your validation routines, but see Part 4 below.

Part 3; Thinking about database items: Answer the following question: The query you were given in Part 2 retrieved the tool_item_no attribute from the database. Did you do anything with it? Why, or why not? Hint: Is it remotely possible that two different tools may have the same name? Another hint: See Part 4.

Put your answer on a Web page, l7p3.html

Part 4; Thinking about form data validation: Explain in no more than a paragraph each what you would have to do with your JavaScript validation to make it work with the database-driven order form. You may put your answer on the same page as Part 3. Please be sure I can tell which answer is which.

Break the Code into Part 1,2&3

Explanation / Answer

Part : 1

<?php
//--- This is the way to connect MySQLi Procedural
$servername = "localhost"; //your server name
$username = "username"; //user name
$password = "password";//password name
$dbname = "weblab"; // your DB name

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT state_abbr, state_name from state_t ORDER BY state_name"; /* By default it will sort state_name by ascending. For descending you need to add query like SELECT state_abbr, state_name from state_t ORDER BY state_name DESC */
$state_result = mysqli_query($conn, $sql);
?>

<!DOCTYPE html>

<html>

<head>

<title>Form</title>

<script type="text/javascript">

function calculateTotal()

{

}

</script>

</head>

<body>

<main>

<form name="total" id="total" action="http://weblab.kennesaw.edu/formtest.php">

Number of Chiesel:<br>

<input type="number" step="1" value="0" id="Chiesel quantity"><br>

Number of Hoes:<br>

<input type="number" step="1" value="0" id="Hoe quantity"><br>

Number of Saw:<br>

<input type="number" step="1" value="0" id="Saw quantity"><br>

Customer name:<br>

<input type="text" name="Fullname" ><br>

Shipping Address:<br>

<input type="text" name="Address" ><br><br>

State:

<select name="states">
    <option selected="selected">--- Select State---</option>
    <?php
       if (mysqli_num_rows($state_result) > 0) {
           while($a_state = mysqli_fetch_assoc($state_result)) {
   ?>
               <option value="<?php echo $a_state['state_abbr'];?>" > <?php echo $a_state['state_name'];?> </option>
   <?php
           }
       }
    ?>
</select>

<br>

<input type="radio" name="card" value="discovery" checked>

discovery<br>

<input type="radio" name="card" value="mastercard">

MasterCard<br>

<input type="radio" name="card" value="visa">

visa<br>

</form>

<input type="button" align="center" value="Submit"/>

<br><br>

<input type="text" id="test" align="center">

<br>

</main>

</body>

</html>

part : 2
<?php
//--- This is the way to connect MySQLi Procedural
$servername = "localhost"; //your server name
$username = "username"; //user name
$password = "password";//password name
$dbname = "weblab"; // your DB name

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "select tool_item_no, tool_name, tool_price,tool_weight from tool_t order by tool_name";
$tool_result = mysqli_query($conn, $sql);
?>
<!DOCTYPE html>

<html>

<head>

<title>Order Form</title>

</head>

<body>

<main>

<form name="total" id="total" action="your php file">

<?php
       if (mysqli_num_rows($tool_result) > 0) {
           while($a_tools = mysqli_fetch_assoc($tool_result)) {
   ?>
   <input type="hidden" value="<?php echo $a_tools['tool_item_no'];?>" name="tool_item_no" id="tool_item_no">
Name:<br>

<input type="text" step="1" value="<?php echo $a_tools['tool_name'];?>" id="tool_name"><br>

Price<br>

<input type="number" step="1" value="<?php echo $a_tools['tool_price'];?>" id="tool_price"><br>

Shipping Weight:<br>

<input type="number" step="1" value="<?php echo $a_tools['tool_weight'];?>" id="tool_weight"><br>
<?php
           }
       }
    ?>
</form>

<input type="button" align="center" value="Submit" />


</main>

</body>

</html>

Part : 3

Did you do anything with it? Why, or why not?
    Yes, since tool_item_no is a primary key value so definetly it becomes an unique value for each and every tools. If anything wants to update or delete based on tools then it will possibile to do based on tool_item_no and also it's easy to identify tools.

Is it remotely possible that two different tools may have the same name?
    It is possible to have same tool name, because in table, tool_name is not mentioned anything like it's a unique value. So may have a chance to get same name for more than on tool. In this type of cases primary key should be considered as a unique value. Here tool_item_no is a primary key.

Part : 4

Form validation with javascript:
For Name, this field should not be empty and it does not allow numeric values, since it's a text or char.
For Price, this field should not be empty and it should greater than zero. Since it's a numeric field.
For Shipping Weight, this field should not be empty and it should greater than zero. Since it's a numeric field.

PHP Validation:
Can validate same combination of name, price and weight should not present in DB. It used to avoid dublicate entires or orders.