Created
March 4, 2014 22:27
-
-
Save ethnt/9357105 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# helpers.php | |
# Ethan Turkeltaub, Matt Maffa | |
$debug = true; | |
# Shows the records in prints | |
function show_records($dbc) { | |
# Create a query to get the name and price sorted by price | |
$query = 'SELECT number, fname, lname FROM presidents ORDER BY number ASC' ; | |
# Execute the query | |
$results = mysqli_query( $dbc , $query ) ; | |
check_results($results) ; | |
# Show results | |
if( $results ) | |
{ | |
# But...wait until we know the query succeeded before | |
# starting the table. | |
echo '<H1>Dead Presidents</H1>' ; | |
echo '<TABLE border="1">'; | |
echo '<TR>'; | |
echo '<TH>Number</TH>'; | |
echo '<TH>First Name</TH>'; | |
echo '<TH>Last Name</TH>'; | |
echo '</TR>'; | |
# For each row result, generate a table row | |
while ( $row = mysqli_fetch_array( $results , MYSQLI_ASSOC ) ) | |
{ | |
echo '<TR>' ; | |
echo '<TD>' . $row['number'] . '</TD>' ; | |
echo '<TD>' . $row['fname'] . '</TD>' ; | |
echo '<TD>' . $row['lname'] . '</TD>' ; | |
echo '</TR>' ; | |
} | |
# End the table | |
echo '</TABLE>'; | |
# Free up the results in memory | |
mysqli_free_result( $results ) ; | |
} | |
} | |
# Inserts a record into the prints table | |
function insert_record($dbc, $fname, $lname, $number) { | |
$query = 'INSERT INTO presidents(fname, lname, number) VALUES ("' . $fname . '", "' . $lname . '", ' . $number . ' )'; | |
show_query($query); | |
$results = mysqli_query($dbc,$query) ; | |
check_results($results) ; | |
return $results ; | |
} | |
# Shows the query as a debugging aid | |
function show_query($query) { | |
global $debug; | |
if($debug) | |
echo "<p>Query = $query</p>" ; | |
} | |
# Checks the query results as a debugging aid | |
function check_results($results) { | |
global $dbc; | |
if($results != true) | |
echo '<p>SQL ERROR = ' . mysqli_error( $dbc ) . '</p>' ; | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
ipresidents.php | |
Ethan Turkeltaub, Matt Maffa | |
--> | |
<!DOCTYPE html> | |
<html> | |
<?php | |
# Connect to MySQL server and the database | |
require( 'includes/connect_db.php' ) ; | |
# Includes these helper functions | |
require( 'includes/helpers.php' ) ; | |
if ($_SERVER[ 'REQUEST_METHOD' ] == 'POST') { | |
$fname = $_POST['fname'] ; | |
$lname = $_POST['lname'] ; | |
$number = $_POST['number'] ; | |
if(!empty($number) && !empty($fname) && !empty($lname)) { | |
$result = insert_record($dbc, $fname, $lname, $number) ; | |
#echo "<p>Added " . $result . " new print(s) ". $name . " @ $" . $price . " .</p>" ; | |
} | |
else | |
echo '<p style="color:red">Please input a name and a price!</p>' ; | |
} | |
# Show the records | |
show_records($dbc); | |
# Close the connection | |
mysqli_close( $dbc ) ; | |
?> | |
<!-- Get inputs from the user. --> | |
<form action="ipresidents.php" method="POST"> | |
<table> | |
<tr> | |
<td>First name:</td><td><input type="text" name="fname"></td> | |
</tr> | |
<tr> | |
<td>Last name:</td><td><input type="text" name="lname"></td> | |
</tr> | |
<tr> | |
<td>Number:</td><td><input type="text" name="number"></td> | |
</tr> | |
</table> | |
<p><input type="submit" ></p> | |
</form> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment