Hi, I am getting these errors in my code and I am not sure as to why. The errors are as follows: These errors only appea
Posted: Fri May 20, 2022 6:48 pm
Hi, I am getting these errors in my code and I am not sure as to why.
The errors are as follows:
These errors only appear when I enter a product amount that is valid. Sorry that the error message is small. I had to screenshot it and for some reason I could not upscale it. Please just zoom in on the screen to view it more easily.
Exercise 5:
• Create a PHP file named Exercise5.php in the practicals/week10 folder of your TWA web site.
• Combine the code from exercise4.html and exercise4.php into Exercise5.php (this is similar to exercise 3 in week 7 practical exercises) so that the php script and the html form are in the same file.
A. Modify the code so that
i. The form uses postback (ie, the form action is the same file exercise5.php)
ii. Only the form is displayed on first load of the page
iii. The table of products is only displayed when there are records to display
iv. The form and the messages as described in exercise 4 are displayed when appropriate in appropriate locations
v. The value that the user enters in the form is maintained in the text box after form submission
For further information all errors and the table of products must be displayed on the same page as the input, not on a separate page.
This Screen Dump might help clarify better what needs to happen:
As you can see here all error messages display on the same page as the input, as does the Product Table.
Any help would be greatly appreciated.
Thankyou =)
P.s Sorry for the small error message image, I tried to resize it, but for some reason it would not work.
Just zoom in the page
Code:
exercise5.php:
<?php
require_once("conn.php");
$data = false ;
$not_numeric = "";
$msg ="";
if(isset($_POST['submit'])){
$qty = $_POST['quantity']; /* accepeted quantity value from user */
if(!is_numeric($qty)){ /* check entered input is number or not using is_numeric */
$not_numeric = "<span>The value entered for the quantity was not a number.</span>";
$data = false ;
/* if user entered value is not a number then above error msg will display */
}else{
$sql = "SELECT * FROM products where quantity_in_stock > $qty ORDER BY quantity_in_stock ASC";
$result = $mysqli->query($sql);
if (!mysqli_num_rows($result) > 0) {
$data = false;
$msg = "<span>There are no products that have more than $qty in stock.</span>";
/* error msg if entered quantity is not available in stock */
}else{
$data = true;
}
}
}
?>
<?php
if($data){
?>
<h1>Product with stock > <?php echo $qty;?></h1>
<table border="1">
<tr>
<th>Name</th>
<th>Quantity In Stock</th>
<th>Price</th>
</tr>
<?php
while($row = $result->fetch_assoc()){?>
<tr>
<td><?php echo $row["name"]; ?></td> <!-- display result -->
<td><?php echo $row["quantity_in_stock"]; ?></td>
<td><?php echo $row["price"]; ?></td>
</tr>
<?php } ?>
</table>
<?php
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
input[type="text"] {border: 1px solid black;}
</style>
<link rel="stylesheet" href="../css/week10Styles.css">
<title>Week 8 Exercise 4 Form</title>
</head>
<body>
<form id="exercise4Form" method="post" action="">
<h1>Quantity in Stock</h1>
<p>Please enter the quantity to check against stock levels</p>
<p>
<label for="quantity">Quantity: </label>
<input type="text" name="quantity" size="10" id="quantity" maxlength="6" required>
</p>
<p style="color:red;"><?php echo $not_numeric; ?></p>
<p style="color:red;"><?php echo $msg; ?></p>
<p><input type="submit" name="submit"></p>
</form>
</body>
</html>
conn.php
<?php
$dbConn = new mysqli("localhost", "TWA_student", "TWA_2022_Autumn", "electrical");
if($dbConn->connect_error) {
die("Failed to connect to database " . $dbConn->connect_error);
}
?>
week10styles.css:
body {
margin:10px;
padding:0;
font-family: Arial;
font-size:1em;
background-color:#ffffff;
color:#000000;
}
h1, h2 {
padding: 10px;
}
form, #output {
margin:20px;
padding: 10px;
width: 50%;
}
input, textarea {
border-radius:5px;
border:1px solid #ccc;
}
input[type="text"], input[type="password"], textarea, select {
width:250px;
padding:3px;
font-size:1em;
}
input[type="submit"] {
padding:6px;
font-size:1em;
background-color: #7db54a;
color: #ffffff;
}
label {
display:inline-block;
width:200px;
}
table {
border-collapse: collapse;
margin:10px;
}
table, th, td {
border: black solid 1px;
padding:3px;
}
thead {
background-color: #7db54a;
}
.errorMsg {
color: #FF0000;
}
#output {
width :50%;
color: #7db54a;
}
Warning: Undefined variable Smysqli in I: twa twa220 practicals week10 exercise5.php on line 16 Fatal error: Uncaught Error: Call to a member function query() on null in I: twa twa220 practicals week10 exercise5.php:16 Stack trace: #0 {main} thrown in I: twa twa220 practicals week10 exercise5.php on line 16
The following screen dumps provide guidance for expected output for the above scenarios: A(ii) only the form displayed on first page load Quantity in Stock Please enter the quantity to check against stock levels Quantity: Submit Query Quantity in Stock Please enter the quantity to check against stock levels A(ii) table of products is only displayed when there are records to display A(iv) The form and the messages as described in exercise 4 are displayed when appropriate in appropriate locations A(v) The value that the user enters in the form is maintained in the text box after form submission Quantity: 100 Submit Query There are no products that have more than 100 in stock. Quantity in Stock Please enter the quantity to check against stock levels Quantity: hello The value entered for quantity was not a number Submit Query A(ii) table of products is only displayed when there are records to display A(iv) The form and the messages as described in exercise 4 are displayed when appropriate in appropriate locations A(v) The value that the user enters in the form is maintained in the text box after form submission Products with stock > 40 Product Code Name Quantity In Stock Price A0987 Google Home Mini 75 R2345 Samsung 320W Dolby Soundbar 50 549 R2456 JBL Junior Pop Kids Wireless Speaker 50 49.95 A(ii) table of products is only displayed when there are records to display A(iv) The form and the messages as described in exercise 4 are displayed when appropriate in appropriate locations A(v) The value that the user enters in the form is maintained in the text box after form submission Quantity in Stock Please enter the quantity to check against stock levels Quantity: 40 Submit
The errors are as follows:
These errors only appear when I enter a product amount that is valid. Sorry that the error message is small. I had to screenshot it and for some reason I could not upscale it. Please just zoom in on the screen to view it more easily.
Exercise 5:
• Create a PHP file named Exercise5.php in the practicals/week10 folder of your TWA web site.
• Combine the code from exercise4.html and exercise4.php into Exercise5.php (this is similar to exercise 3 in week 7 practical exercises) so that the php script and the html form are in the same file.
A. Modify the code so that
i. The form uses postback (ie, the form action is the same file exercise5.php)
ii. Only the form is displayed on first load of the page
iii. The table of products is only displayed when there are records to display
iv. The form and the messages as described in exercise 4 are displayed when appropriate in appropriate locations
v. The value that the user enters in the form is maintained in the text box after form submission
For further information all errors and the table of products must be displayed on the same page as the input, not on a separate page.
This Screen Dump might help clarify better what needs to happen:
As you can see here all error messages display on the same page as the input, as does the Product Table.
Any help would be greatly appreciated.
Thankyou =)
P.s Sorry for the small error message image, I tried to resize it, but for some reason it would not work.
Just zoom in the page
Code:
exercise5.php:
<?php
require_once("conn.php");
$data = false ;
$not_numeric = "";
$msg ="";
if(isset($_POST['submit'])){
$qty = $_POST['quantity']; /* accepeted quantity value from user */
if(!is_numeric($qty)){ /* check entered input is number or not using is_numeric */
$not_numeric = "<span>The value entered for the quantity was not a number.</span>";
$data = false ;
/* if user entered value is not a number then above error msg will display */
}else{
$sql = "SELECT * FROM products where quantity_in_stock > $qty ORDER BY quantity_in_stock ASC";
$result = $mysqli->query($sql);
if (!mysqli_num_rows($result) > 0) {
$data = false;
$msg = "<span>There are no products that have more than $qty in stock.</span>";
/* error msg if entered quantity is not available in stock */
}else{
$data = true;
}
}
}
?>
<?php
if($data){
?>
<h1>Product with stock > <?php echo $qty;?></h1>
<table border="1">
<tr>
<th>Name</th>
<th>Quantity In Stock</th>
<th>Price</th>
</tr>
<?php
while($row = $result->fetch_assoc()){?>
<tr>
<td><?php echo $row["name"]; ?></td> <!-- display result -->
<td><?php echo $row["quantity_in_stock"]; ?></td>
<td><?php echo $row["price"]; ?></td>
</tr>
<?php } ?>
</table>
<?php
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
input[type="text"] {border: 1px solid black;}
</style>
<link rel="stylesheet" href="../css/week10Styles.css">
<title>Week 8 Exercise 4 Form</title>
</head>
<body>
<form id="exercise4Form" method="post" action="">
<h1>Quantity in Stock</h1>
<p>Please enter the quantity to check against stock levels</p>
<p>
<label for="quantity">Quantity: </label>
<input type="text" name="quantity" size="10" id="quantity" maxlength="6" required>
</p>
<p style="color:red;"><?php echo $not_numeric; ?></p>
<p style="color:red;"><?php echo $msg; ?></p>
<p><input type="submit" name="submit"></p>
</form>
</body>
</html>
conn.php
<?php
$dbConn = new mysqli("localhost", "TWA_student", "TWA_2022_Autumn", "electrical");
if($dbConn->connect_error) {
die("Failed to connect to database " . $dbConn->connect_error);
}
?>
week10styles.css:
body {
margin:10px;
padding:0;
font-family: Arial;
font-size:1em;
background-color:#ffffff;
color:#000000;
}
h1, h2 {
padding: 10px;
}
form, #output {
margin:20px;
padding: 10px;
width: 50%;
}
input, textarea {
border-radius:5px;
border:1px solid #ccc;
}
input[type="text"], input[type="password"], textarea, select {
width:250px;
padding:3px;
font-size:1em;
}
input[type="submit"] {
padding:6px;
font-size:1em;
background-color: #7db54a;
color: #ffffff;
}
label {
display:inline-block;
width:200px;
}
table {
border-collapse: collapse;
margin:10px;
}
table, th, td {
border: black solid 1px;
padding:3px;
}
thead {
background-color: #7db54a;
}
.errorMsg {
color: #FF0000;
}
#output {
width :50%;
color: #7db54a;
}
Warning: Undefined variable Smysqli in I: twa twa220 practicals week10 exercise5.php on line 16 Fatal error: Uncaught Error: Call to a member function query() on null in I: twa twa220 practicals week10 exercise5.php:16 Stack trace: #0 {main} thrown in I: twa twa220 practicals week10 exercise5.php on line 16
The following screen dumps provide guidance for expected output for the above scenarios: A(ii) only the form displayed on first page load Quantity in Stock Please enter the quantity to check against stock levels Quantity: Submit Query Quantity in Stock Please enter the quantity to check against stock levels A(ii) table of products is only displayed when there are records to display A(iv) The form and the messages as described in exercise 4 are displayed when appropriate in appropriate locations A(v) The value that the user enters in the form is maintained in the text box after form submission Quantity: 100 Submit Query There are no products that have more than 100 in stock. Quantity in Stock Please enter the quantity to check against stock levels Quantity: hello The value entered for quantity was not a number Submit Query A(ii) table of products is only displayed when there are records to display A(iv) The form and the messages as described in exercise 4 are displayed when appropriate in appropriate locations A(v) The value that the user enters in the form is maintained in the text box after form submission Products with stock > 40 Product Code Name Quantity In Stock Price A0987 Google Home Mini 75 R2345 Samsung 320W Dolby Soundbar 50 549 R2456 JBL Junior Pop Kids Wireless Speaker 50 49.95 A(ii) table of products is only displayed when there are records to display A(iv) The form and the messages as described in exercise 4 are displayed when appropriate in appropriate locations A(v) The value that the user enters in the form is maintained in the text box after form submission Quantity in Stock Please enter the quantity to check against stock levels Quantity: 40 Submit