Skip to content

Instantly share code, notes, and snippets.

@KimiyukiYamauchi
Last active December 30, 2015 18:59
Show Gist options
  • Save KimiyukiYamauchi/7871557 to your computer and use it in GitHub Desktop.
Save KimiyukiYamauchi/7871557 to your computer and use it in GitHub Desktop.
<?php
// Load PEAR MDB2
require 'MDB2.php';
// Load the form helper functions
require 'formhelpers.php';
// Connect to the database
require 'connect_mysql.php';
$db = connect_mysql();
// Set up automatic error handling
$db->setErrorHandling(PEAR_ERROR_DIE);
// The main page logic:
// - If the form is submitted, validate and then process or redisplay
// - If it's not submitted, display
if ($_POST['_submit_check']) {
// If validate_form() returns errors, pass them to show_form()
if ($form_errors = validate_form()) {
show_form($form_errors);
} else {
// The submitted data is valid, so process it
process_form();
}
} else {
// The form wasn't submitted, so display
show_form();
}
function show_form($errors = '') {
// If the form is submitted, get defaults from submitted parameters
if ($_POST['_submit_check']) {
$defaults = $_POST;
} else {
// Otherwise, set our own defaults: price is $5
$defaults = array('price' => '5.00');
}
// If errors were passed in, put them in $error_text (with HTML markup)
if (is_array($errors)) {
$error_text = '<tr><td>You need to correct the following errors:';
$error_text .= '</td><td><ul><li>';
$error_text .= implode('</li><li>',$errors);
$error_text .= '</li></ul></td></tr>';
} else {
// No errors? Then $error_text is blank
$error_text = '';
}
// Jump out of PHP mode to make displaying all the HTML tags easier
?>
<form method="POST" action="<?php print $_SERVER['PHP_SELF']; ?>">
<table>
<?php print $error_text ?>
<tr><td>名前:</td>
<td><?php input_text('ename', $defaults) ?></td></tr>
<tr><td>よみ:</td>
<td><?php input_text('yomi', $defaults) ?></td></tr>
<tr><td>職種:</td>
<td><?php input_text('job', $defaults) ?></td></tr>
<tr><td>上司:</td>
<td><?php input_text('mgr', $defaults) ?></td></tr>
<tr><td>入社:</td>
<td><?php input_text('hiredate', $defaults) ?></td></tr>
<tr><td>給与:</td>
<td><?php input_text('sal', $defaults) ?></td></tr>
<tr><td>歩合:</td>
<td><?php input_text('comm', $defaults) ?></td></tr>
<tr><td>部門:</td>
<td><?php input_text('deptno', $defaults) ?></td></tr>
<tr><td colspan="2" align="center"><?php input_submit('save','追加'); ?>
</td></tr>
</table>
<input type="hidden" name="_submit_check" value="1"/>
</form>
<?php
} // The end of show_form()
function validate_form() {
$errors = array();
/*
// dish_name is required
if (! strlen(trim($_POST['dish_name']))) {
$errors[] = 'Please enter the name of the dish.';
}
// price must be a valid floating point number and
// more than 0
if (floatval($_POST['price']) <= 0) {
$errors[] = 'Please enter a valid price.';
}
*/
return $errors;
}
function process_form() {
// Access the global variable $db inside this function
global $db;
$max_empno = $db->queryOne('select max(empno) from employees');
$empno = $max_empno + 1;
$sth = $db->prepare('INSERT INTO employees (empno, ename, yomi, job, mgr, hiredate, sal, comm, deptno)
VALUES (?,?,?,?,?,?,?,?,?)');
// var_dump(array($empno, $_POST['ename'], $_POST['yomi'], $_POST['job'], $_POST['mgr'], $_POST['hiredate'], $_POST['sal'], $_POST['comm'],$_POST['deptno']));
$sth->execute(array($empno, $_POST['ename'], $_POST['yomi'], $_POST['job'], $_POST['mgr'], $_POST['hiredate'], $_POST['sal'], $_POST['comm'],$_POST['deptno']));
print 'Added ' . htmlentities($_POST['ename'], ENT_QUOTES, 'UTF-8') .
' to the database.';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment