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

- Question - Write down the missing code according to the comments included in t

ID: 3904294 • Letter: #

Question

- Question - Write down the missing code according to the comments included in the code

list.php

<?php

print"

<!doctype html>

<html lang="en">

<head>

<meta charset="utf-8">

<title>I Must Sort This Out!</title>

</head>

<body>

";

print"

<div><p>Enter the words you want alphabetized with each individual word separated by a space:</p>";

print"

<form action="list_sql.php" method="post">

<textarea rows = "5" cols = "50" name = "words"></textarea><br>

<input type="submit" name="submit" value="Alphabetize!">

</form>

";

print"

</div>

</body>

</html>

";

?>

list_sql.php

<!doctype html>

<html lang="en">

<head>

<meta charset="utf-8">

<title>I Have This Sorted Out</title>

</head>

<body>

<?php // Script 7.7 - list.php

/* This script receives a string in $_POST['words']. It then turns it into an array,

sorts the array alphabetically, and reprints it. */

$string_wordsA=$_POST['words'];

// Turn the incoming string into an array:

$words_array = explode(' ' , $_POST['words']);

// Sort the array:

sort($words_array);

// Turn the array back into a string:

$string_words = implode('<br>', $words_array);

$string_wordsB = implode(' ', $words_array);

// Print the results:

print "<p>Origianl version is : <br /> $string_wordsA <br />An alphabetized version of your list is: <br />$string_words</p>";

/*

add code that will save id, original string, sorted string, and date in the database

*/

mysqli_close($dbc); // Close the connection.

}

?>

</body>

</html>

create_database.php

<?php

$dbhost = 'localhost';

$dbuser = 'root';

$dbpass = '';

$dbc = mysql_connect($dbhost, $dbuser, $dbpass);

if(! $dbc ) {

die('Could not connect: ' . mysql_error());

}

echo 'Connected successfully <br />';

$sql = 'CREATE Database csc395_5';

$retval = mysql_query( $sql, $dbc );

if(! $retval ) {

die('Could not create database: ' . mysql_error());

}

echo "Database test_db created successfully ";

if (@mysql_select_db('csc395_5', $dbc))

{

print '<p>The database has been selected!</p>';

}

else

{

print '<p>Could not select the database because:<br />' .mysql_error() . '.</p>';

}

$query = 'CREATE TABLE str_sort(id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,

str_orig varchar(100),

str_sort varchar(100),

date_entered DATETIME NOT NULL

)CHARACTER SET utf8;';

$retval = mysql_query( $query, $dbc );

// Execute the query:

if($retval)

{

print '<p>The table has been created!</p>';

} else {

print '<p>Could not create the table because:<br>' . mysqli_error($dbc) . '.</p><p>The query being run was: ' . $query . '</p>';

}

mysql_close($dbc);

?>

Explanation / Answer

If you have any doubts, please give me comment...

<!doctype html>

<html lang="en">

<head>

<meta charset="utf-8">

<title>I Have This Sorted Out</title>

</head>

<body>

<?php // Script 7.7 - list.php

/* This script receives a string in $_POST['words']. It then turns it into an array,

sorts the array alphabetically, and reprints it. */

$string_wordsA=$_POST['words'];

// Turn the incoming string into an array:

$words_array = explode(' ' , $_POST['words']);

// Sort the array:

sort($words_array);

// Turn the array back into a string:

$string_words = implode('<br>', $words_array);

$string_wordsB = implode(' ', $words_array);

// Print the results:

print "<p>Origianl version is : <br /> $string_wordsA <br />An alphabetized version of your list is: <br />$string_words</p>";

$dbhost = 'localhost';

$dbuser = 'root';

$dbpass = '';

$dbc = mysql_connect($dbhost, $dbuser, $dbpass);

if(! $dbc ) {

die('Could not connect: ' . mysql_error());

}

echo 'Connected successfully <br />';

$sql = 'CREATE Database IF NOT EXISTS csc395_5';

$retval = mysql_query( $sql, $dbc );

if(! $retval ) {

die('Could not create database: ' . mysql_error());

}

echo "Database test_db created successfully ";

if (@mysql_select_db('csc395_5', $dbc))

{

print '<p>The database has been selected!</p>';

}

else

{

print '<p>Could not select the database because:<br />' .mysql_error() . '.</p>';

}

$query = 'CREATE TABLE IF NOT EXISTS str_sort(id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,

str_orig varchar(100),

str_sort varchar(100),

date_entered DATETIME NOT NULL

)CHARACTER SET utf8;';

$retval = mysql_query( $query, $dbc );

// Execute the query:

if($retval)

{

print '<p>The table has been created!</p>';

$sql = "INSERT INTO str_sort(str_orig, str_sort, date_entered) VALUES('".$string_wordsA."', '".$string_wordsB."', 'NOW()'";

$qretval = mysql_query($sql, $dbc);

if($qretval)

echo "<p>Successfully added to database</p>";

else{

echo "<p>someting goes wrong while insert</p>";

}

} else {

print '<p>Could not create the table because:<br>' . mysqli_error($dbc) . '.</p><p>The query being run was: ' . $query . '</p>';

}

mysqli_close($dbc); // Close the connection.

}

?>

</body>

</html>