Programming Test in PHP for Potential Internship

Programming Test in PHP for Potential Internship
0

#1

This is a lot of code. I apologize. I’m not very experienced in PHP. I’m not sure if this topic is even very relevant to the Forum.

Creating a test PHP form that is supposed to take only one input, a date entered by user. Using a provided array of all states in US (contains all states and, for each, the number of days to ship to that state), am to insert into MySQL database for EACH STATE (I’m using a foreach loop): the state, user’s input date, number of days to ship, and arrival date (determined by adding days to ship to input date). Finally, I need to display the database table of ALL states in HTML upon submission of form.

I think I’m on the right track here, but don’t know how to test if it works, since I’m not using a real database.

Is it OK to put SQL insert query inside of my foreach loop? If not, how else can I get the four columns of data for each state?

Edit: I am aware it’s MySQLi now, and I need to prevent SQL injection.

<!DOCTYPE html>
<html>
<head>
<title>Determining Package Arrival Date</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=Inconsolata" rel="stylesheet">
<style>
body {
	text-align: center;
	font-family: 'Inconsolata', monospace;
	font-size: 20px;
	text-align: center;
	background: #065E6A;
	color: white;
	width: 100vw;
	height: 100vh;
}
h1 {
	font-size: 30px;
	margin: 10px;
	padding: 10px;
}
form {
	padding: 25px;
}
input {
	border-radius: 10px;
	font-size: 15px;
	background: white;
	color: #065E6A;
	padding: 5px;
}
</style>
</head>
<body>
	<h1>Determine Your Package's Arrival Date</h1>
	<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
      <label>Date: <input type="date" name="date" /></label>
      <input type="submit" value="Submit" />
    </form>
    <br>
    <table class="striped">
            <tr class="header">
                <td>State</td>
                <td>Leaves on</td>
                <td>Days in Transit</td>
                <td>Arrives on</td>
            </tr>

            <?php
               while ($row = mysql_fetch_array($result)) {
                   echo "<tr>";
                   echo "<td>".$row[state]."</td>";
                   echo "<td>".$row[chosen_date]."</td>";
                   echo "<td>".$row[days_in_transit]."</td>";
                   echo "<td>".$row[arrival_date]."</td>";
                   echo "</tr>";
               }
            ?>
        </table>
<?php

// Check that form has been submitted
if (count($_POST)>0) {

	/* Attempt MySQL server connection, assuming you are running MySQL server with default setting (user 'root' with no password) */
	$server = mysql_connect("localhost", "root", "");
 
	// Check connection
	if($server === false){
    	die("ERROR: Could not connect. " . mysql_connect_error());
	}
 
 	// Create table
 	$sql_table = "CREATE TABLE transit_check_log (
	ID BIGINT(6) UNSIGNED AUTO_INCREMENT, 
	state VARCHAR(128),
	chosen_date VARCHAR(128),
	days_in_transit VARCHAR(128),
	arrival_date VARCHAR(128),
	ip_address VARCHAR(128)
	)";

	$chosen_date = $_POST['date'];
	$ip_address = $_SERVER['REMOTE_ADDR'];
	//$details = json_decode(file_get_contents("http://ipinfo.io/{$ip_address}/json"));
	//$state =  $details->region;

	$states = Array(
		'Colorado'			=> 3,
		'Alaska'			=> 7,
		'Kansas'			=> 4,
		'Arizona'			=> 5,
		'California'		=> 6,
		'Delaware'			=> 2,
		'Illinois'			=> 2,
		'Massachusetts'		=> 1,
		'North Carolina'	=> 3,
		'North Dakota'		=> 4,
		'Utah'				=> 4,
		'Virginia'			=> 2,
		'Vermont'			=> 1,
		'West Virginia'		=> 2,
		'Wyoming'			=> 4,
		'Nebraska'			=> 4,
		'Connecticut'		=> 1,
		'Washington, DC'	=> 2,
		'Montana'			=> 4,
		'Mississippi'		=> 3,
		'New Mexico'		=> 4,
		'Florida'			=> 3,
		'Georgia'			=> 3,
		'Hawaii'			=> 6,
		'New York'			=> 1,
		'Ohio'				=> 2,
		'Oklahoma'			=> 3,
		'Oregon'			=> 5,
		'Washington'		=> 5,
		'Wisconsin'			=> 2,
		'Pennsylvania'		=> 1,
		'Rhode Island'		=> 1,
		'Alabama'			=> 2,
		'Arkansas'			=> 3,
		'South Carolina'	=> 2,
		'South Dakota'		=> 3,
		'Maryland'			=> 2,
		'Iowa'				=> 4,
		'Indiana'			=> 3,
		'Kentucky'			=> 3,
		'Louisiana'			=> 3,
		'Idaho'				=> 4,
		'Tennessee'			=> 2,
		'Maine'				=> 2,
		'Michigan'			=> 3,
		'Minnesota'			=> 3,
		'Texas'				=> 4,
		'New Hampshire'		=> 1,
		'New Jersey'		=> 1,
		'Missouri'			=> 3,
		'Nevada' 			=> 5
	);

	foreach( $states as $key => $value ){

		$state = $key;
		$arrival_date = date('Y-m-d', strtotime($chosen_date. " + {$value} days"));
		$days_in_transit = $value;

		// Attempt insert query execution
		$sql = "INSERT INTO transit_check_log (state, chosen_date, days_in_transit, arrival_date, ip_address) VALUES ('$state', '$chosen_date', '$days_in_transit', '$arrival_date', '$ip_address')";

		if(mysql_query($server, $sql)){
		    echo "Records inserted successfully.";
		} else{
		    echo "ERROR: Could not able to execute $sql. " . mysql_error($server);
		}
		
	}
 	
	// Pull data from MySQL
	$result = mysql_query("SELECT * FROM transit_check_log");

	$row = mysql_fetch_array($result);

	// Close connection
	//mysql_close($server);

}
// End check that form submitted
?>
</body>
</html>

#2

I’m not sure I understand what you mean here. Do you mean to say you have no database at all, or are you just saying there is no real data in it so far? Why aren’t you using a real database?
What environment are you using for your tests?
I am certainly not up to speed as far as PHP and SQL are concerned, but a few years ago, working with a local XAMPP server was what pretty much everybody was doing, and it used to work like a charm for me.


#3

I am not sure how helpful this comment will be, but i would break you project up into chunks. Practice writing to a DB, then reading from the DB, then implementing the full scope of your project.
That’s what I have been doing for the back end FCC projects.


#4

In short: I was incorrectly assuming it would not be necessary to create a database to test this problem. I was in too much of a hurry and referencing a several-years-old PHP book last night when, without internet access, I wrote this code. Of course now I’m not sure what I was thinking.

Of course I should want to actually test the program before I submit it as complete. (I was not at all looking to begin applying for jobs yet; that’s another story. I’m not sure an internship requiring I write lots of PHP is a good idea, anyway.)

I am now using Cloud9 to run/test PHP code; conveniently, they offer MySQL setup.