0

Test me!

So a head hunter sent me an email about a php job here in Austin. Here is an excerpt:

<strong>PHP Test  Instructions</strong>
If you are interested in the freelance PHP  position, please complete and submit the attached PHP test within 24 hours  of receiving it. Inside the attachment you will find the instructions to  the test. This test should only take you about 30 minutes to an hour to complete. If you have any questions about  the PHP position or test, please feel free to contact me.

Once I have received and reviewed your  test, I will contact you with the results of your test.
Good luck! ...


Attached was a zip file containing two files, a .sql and index.php. Inside of the index.php file, I found this:

<?php
//Database Connection
$msdb = mysql_connect("localhost", "root", "");
mysql_select_db("test", $msdb) or die(mysql_error());
/*
GENERAL INFORMATION:
Below is the current table structure for 'members':
=====
ID: id (autoincrement)
firstname
lastname
phonenumber
=====

PUT YOUR NAME HERE PLEASE:
PART 1:
-------
* Below, write the PHP code to insert a first name, last name, and phone number  into the 'members' table.  Use an HTML form to collect this information, then store it to the 'members' table.
//
*/
/*
PART 2:
-------
* Below, write the PHP code to retrieve the previously inserted record from the  'tests' table and display it in the browser.
//
*/
/*
?>

I am about to hit the sack and figured this should be quick. Let me see if I can finish this before I fall asleep…
So they want first name, last name and phone number in an HTML form? No problem:

<html>
<body>
<p>
   Please provide the following information
</p>
<form action="insert.php" method="post">
   First Name: <input name="first" type="text" />
   Last Name: <input name="last" type="text" />
   Phone Number: <input name="phone" type="text" />
   <input type="submit" />
</form>

</body>
</html>

Now we have to dump the inputs into the database. Using the info they gave me from above, insert.php would look like this:

<?php
$msdb = mysql_connect("localhost","root","");
if (!$msdb)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("test", $msdb);

$sql="INSERT INTO members (firstname, lastname, phonenumber)
VALUES ('$_POST&#91;first&#93;','$_POST&#91;last&#93;','$_POST&#91;phone&#93;')";

if (!mysql_query($sql,$msdb))
  {
  die('Error: ' . mysql_error());
  }
echo "Thanks, I got it.";

mysql_close($msdb)
?>

Now we need to get data from the DB back to the browser

<?php
$msdb = mysql_connect("localhost","test","");
if (!$msdb)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("test", $msdb);

$result = mysql_query("SELECT * FROM members");

echo "<table border='1'>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Phone Number</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['first'] . "</td>";
  echo "<td>" . $row['last'] . "</td>";
  echo "<td>" . $row['phonenumber']. "</td>";
  echo "</tr>";
  }
echo "</table>";
mysql_close($msdb);
?>

Does this work? Don’t know, did not test it, but it should. Though I will not send this in for a job, it was fun to play with.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *