Hello, I am having problems with my code for a particular part of an assignment.
ID: 3718716 • Letter: H
Question
Hello, I am having problems with my code for a particular part of an assignment. I have included the instructions for the assignment below and my code. I am having problems with part 2 of the assignment, I cannot access the database and I have included a screenshot of the error message that I am receiving. Thank you in advance for the help.
My code:
<!DOCTYPE HTML>
<html>
<head>
<title> Lab 7 Part 1 </title>
<style>
.fieldset-auto-width {
display: inline-block;
}
</style>
</head>
<body>
<h1>Tools Order form</h1>
<form name="form1" action="/formtest.php"
method = "post">
<table>
<tr><th>Name</th><th>Price</th><th>Weight</th></tr>
<?php
$conn = mysqli_connect (null, null, null, 'weblab');
if (!$conn) {
die ("Can't connect to DB:" . 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);
?>
<?php
$num = mysqli_num_rows($conn);
if ($num == 0) {
echo '<tr><td colspan="2">';
echo 'Database query retrieved zero rows.</td></tr>';
}
while ($row = mysqli_fetch_assoc($conn)) {
$Name = $row['Name'];
$Price = $row['Price'];
$Weight = $row['Weight'];
echo "<tr><td><b>$Name</b></td>";
echo "<td>$Price</td></tr> ";
}
?>
</table>
<p>
<label for="firstName">First Name:</label>
<input type="text" name="firstName" id="firstName" maxLength="25" required/>
</p>
<p>
<label for="lastName">Last Name:</label>
<input type="text" name="lastName" id="lastName" maxLength="25" required/>
</p>
<p>
<label for="address">Address:</label>
<input type="text" name="address" id="address" size="20" required/>
</p>
<?php
$conn= mysqli_connect (null, null, null, 'weblab');
if (!$conn) {
die("Can't connect to DB:" . mysqli_connect_error());
}
$sql = "SELECT state_abbr, state_name from state_t ORDER BY state_name";
$state_result = mysqli_query($conn,$sql);
?>
<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>
</label>
<p>
<strong>Please select a payment type.</strong>
</p>
<fieldset class="fieldset-auto-width">
<label for="Visa">Visa</label>
<input type="radio" name="paymentType" id="Visa" value="Visa" />
<label for="AE">American Express</label>
<input type="radio" name="paymentType" id="AE" value="AE" />
<label for="Discover">Discover Card</label>
<input type="radio" name="paymentType" id="Discover" value="Discover" />
<label for="Master">Master Card</label>
<input type="radio" name="paymentType" id="Master" value="Master" />
</fieldset>
<input type="submit" value="Submit" >
</form>
</body>
</html>
Assignment Instructions:
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 valueattribute 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.
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:
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.
Error Message on web page:
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, object given in /home/public_html/l7p1.php on line 32
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, object given in /home/public_html/l7p1.php on line 38
Explanation / Answer
If you have any doubts, please give me comment...
<!DOCTYPE HTML>
<html>
<head>
<title> Lab 7 Part 1 </title>
<style>
.fieldset-auto-width {
display: inline-block;
}
</style>
</head>
<body>
<h1>Tools Order form</h1>
<form name="form1" action="/formtest.php"
method = "post">
<table>
<tr><th>Name</th><th>Price</th><th>Weight</th></tr>
<?php
$conn = mysqli_connect (null, null, null, 'weblab');
if (!$conn) {
die ("Can't connect to DB:" . 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);
?>
<?php
$num = mysqli_num_rows($conn, $tool_result);
if ($num == 0) {
echo '<tr><td colspan="2">';
echo 'Database query retrieved zero rows.</td></tr>';
}
while ($row = mysqli_fetch_assoc($conn, $tool_result)) {
$Name = $row['Name'];
$Price = $row['Price'];
$Weight = $row['Weight'];
echo "<tr><td><b>$Name</b></td>";
echo "<td>$Price</td></tr> ";
}
?>
</table>
<p>
<label for="firstName">First Name:</label>
<input type="text" name="firstName" id="firstName" maxLength="25" required/>
</p>
<p>
<label for="lastName">Last Name:</label>
<input type="text" name="lastName" id="lastName" maxLength="25" required/>
</p>
<p>
<label for="address">Address:</label>
<input type="text" name="address" id="address" size="20" required/>
</p>
<?php
$conn= mysqli_connect (null, null, null, 'weblab');
if (!$conn) {
die("Can't connect to DB:" . mysqli_connect_error());
}
$sql = "SELECT state_abbr, state_name from state_t ORDER BY state_name";
$state_result = mysqli_query($conn,$sql);
?>
<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>
</label>
<p>
<strong>Please select a payment type.</strong>
</p>
<fieldset class="fieldset-auto-width">
<label for="Visa">Visa</label>
<input type="radio" name="paymentType" id="Visa" value="Visa" />
<label for="AE">American Express</label>
<input type="radio" name="paymentType" id="AE" value="AE" />
<label for="Discover">Discover Card</label>
<input type="radio" name="paymentType" id="Discover" value="Discover" />
<label for="Master">Master Card</label>
<input type="radio" name="paymentType" id="Master" value="Master" />
</fieldset>
<input type="submit" value="Submit" >
</form>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.