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

For this exercise, I have already figured out number 1 and 2. I just can\'t figu

ID: 3599093 • Letter: F

Question

For this exercise, I have already figured out number 1 and 2. I just can't figure out number 3. Please help. THANK YOU SO MUCH :)

1.

In the "form_generator.php" file, write PHP scripts so that you can query to the "sakila" database, and from the "customer" table, select the 10th customer when sorted the "customer" table based on the "last_name" in alphabetical order. Also, join "address" and "city" tables when querying to the "customer" table. Save the query result in a PHP variable so that you can use it in the next task.

2.

In the "form_generator.php" file, below the DB query scripts, write PHP scripts so that a form is dynamically generated. Make five inputs that allow users to enter customer information. Names of the inputs for the form include:

- "first_name" (text type)

- "last_name" (text type)

- "email" (email type)

- "address" (text type")

- "city" (text type")

Also, there needs to be a "submit" button, and when submitted, the "form_display.php" file needs to be called so it can receive the data.

When generating this form, use the 10th customer data that you retrieved from Task 1 so that you can set the default values for the five inputs. Ideally, if a user opens the "form_generator.php" file on the browser, the user should be able to see the five inputs and a submit button, and these five inputs should already have values it them (the values are the 10th customer information).

3.

Write PHP scripts in the "form_display.php" file so that the submitted data is displayed as a table (after the user clicks the submit button on the "form_generator.php" page). The first column of the table is for the titles (first name, last name, ...), and the second column needs to show data that was submitted by the user.

If the user didn't change anything from the default values, the original values that came from the DB will show up in this page. If the user changed any of the values in the form, the modified values will show up in the display page. After making the table for displaying the submission result, style the table using CSS, so that the title column and the value column have diffent colors and fonts.

Explanation / Answer

//form_generator.php

CREATE TABLE IF NOT EXISTS `sakilas` (

`id` int(11) NOT NULL,

`first_name` varchar(60) NOT NULL,

`last_name` varchar(60) NOT NULL,

`email` varchar(60) NOT NULL,

`website` varchar(60) NOT NULL,

`address` varchar(60) NOT NULL,

`city` text NOT NULL

)AUTO_INCREMENT=1 ;

ALTER TABLE `users_data`

ADD PRIMARY KEY (`id`);

ALTER TABLE `users_data`

MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

<!DOCTYPE HTML>  

<html>

<head>

<style>

.error {color: #FF0000;}

table {

font-family: arial, sans-serif;

border-collapse: collapse;

width: 100%;

}

td, th {

border: 1px solid #dddddd;

text-align: left;

padding: 8px;

}

tr:nth-child(even) {

background-color: #dddddd;

}

</style>

</head>

<body>  

<?php

// define variables and set to empty values

$nameErr = $emailErr = $genderErr = $websiteErr = "";

$name = $email = $gender = $comment = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {

if (empty($_POST["first_name"])) {

$nameErr = "first_name is required";

} else {

$name = test_input($_POST["first_name"]);

// check if first_name only contains letters and whitespace

if (!preg_match("/^[a-zA-Z ]*$/",$name)) {

$nameErr = "Only letters and white space allowed";

}

}

if (empty($_POST["last_name"])) {

$last_name = "last_name is required";

} else {

$last_name = test_input($_POST["last_name"]);

// check if last_name only contains letters and whitespace

if (!preg_match("/^[a-zA-Z ]*$/",$last_name)) {

$last_name = "Only letters and white space allowed";

}

}

if (empty($_POST["email"])) {

$emailErr = "Email is required";

} else {

$email = test_input($_POST["email"]);

// check if e-mail address is well-formed

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {

$emailErr = "Invalid email format";

}

}

  

if (empty($_POST["website"])) {

$website = "";

} else {

$website = test_input($_POST["website"]);

// check if URL address syntax is valid (this regular expression also allows dashes in the URL)

if (!preg_match("/(?:(?:https?|ftp)://|www.)[-a-z0-9+&@#/%?=~_|!:,.;]*[-a-z0-9+&@#/%=~_|]/i",$website)) {

$websiteErr = "Invalid URL";

}

}

if (empty($_POST["address"])) {

$address = "";

} else {

$address = test_input($_POST["address"]);

}

if (empty($_POST["city"])) {

$city = "city is required";

} else {

$city = test_input($_POST["city"]);

// check if city only contains letters and whitespace

if (!preg_match("/^[a-zA-Z ]*$/",$city)) {

$city = "Only letters and white space allowed";

}

}

}

$mysql_host = "localhost";

$mysql_username = "root";

$mysql_password = "";

$mysql_database = "sakila";

$mysqli = new mysqli($mysql_host, $mysql_username, $mysql_password, $mysql_database);

  

//Output any connection error

if ($mysqli->connect_error) {

die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);

}

  

$statement = $mysqli->prepare("INSERT INTO sakilas (first_name, last_name, email,website,address,city) VALUES(?, ?, ?, ?, ?, ?)"); //prepare sql insert query

//bind parameters for markers, where (s = string, i = integer, d = double, b = blob)

$statement->bind_param('$first_name', $last_name, $email, $website,$address,$city); //bind values and execute insert query

  

if($statement->execute()){

print "Hello " . $first_name . "!, your message has been saved!";

}else{

print $mysqli->error; //show mysql error if any

}

function test_input($data) {

$data = trim($data);

$data = stripslashes($data);

$data = htmlspecialchars($data);

return $data;

}

?>

<h2>PHP Form Validation Example</h2>

<p><span class="error">* required field.</span></p>

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  

first_name: <input type="text" name="first_name" value="<?php echo $name;?>">

<span class="error">* <?php echo $nameErr;?></span>

<br><br>

last_name: <input type="text" name="last_name" value="<?php echo $last_name;?>">

<span class="error">* <?php echo $last_name;?></span>

<br><br>

E-mail: <input type="text" name="email" value="<?php echo $email;?>">

<span class="error">* <?php echo $emailErr;?></span>

<br><br>

Website: <input type="text" name="website" value="<?php echo $website;?>">

<span class="error"><?php echo $websiteErr;?></span>

<br><br>

address: <textarea name="address" rows="5" cols="40"><?php echo $address;?></textarea>

<br><br>

city: <input type="text" name="city" value="<?php echo $city;?>">

<span class="error">* <?php echo $city;?></span>

<br><br>

<input type="submit" name="submit" value="Submit">  

</form>

//for display the data

//form_display.php

<table>

<tr>

<th>first name</th>

<th>last name</th>

<th>email</th>

<th>website</th>

<th>address</th>

<th>city</th>

</tr>

<?php

$conn = mysql_connect("localhost","root","");

$db = mysql_select_db("sakila",$conn);

?>

<?php

$sql = "select * from sakilas";

$query = mysql_query($sql);

while($row = mysql_fetch_array($query)){

echo "<tr>";

echo "<td>".$row['first_name']."</td>";

echo "<td>".$row['last_name']."</td>";

echo "<td>".$row['email']."</td>";

echo "<td>".$row['website']."</td>";

echo "<td>".$row['address']."</td>";

echo "<td>".$row['city']."</td>";

echo "</tr>";

}

?>

  

</table>

</body>

</html>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote