Forum Moderators: coopster
<?php
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$termnew1 = (isset($_POST['termnew']) and ! empty($_POST['termnew']))?mysql_real_escape_string($_POST['termnew']):null;
$ComputerNamenew1 = (isset($_POST['ComputerNamenew']) and ! empty($_POST['ComputerNamenew']))?mysql_real_escape_string($_POST['ComputerNamenew']):null;
if (! ($termnew1 and $ComputerNamenew1 )) { die("Required variables not present"); }
$updated="UPDATE Inventory SET ComputerName = '$ComputerNamenew1' WHERE AssetTag = '$termnew1'";
$result = mysql_query($updated) or die("Cannot execute update: " . mysql_error());
if($result){
echo("<br>Computer name changed successfully");
} else{
echo("<br>COMPUTER NAME NOT CHANGED - ERROR");
}
?> Iwant to add a redirect to the home page on a successful change, after the confirmation message and a redirect to the previous page on an error.
// Do not use @, this supresses errors. Error trapping is your friend.
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
mysql_select_db($database) or die( "Unable to select database");
//
if ($_POST['some_form_value']) {
$errors = error_checking_function();
if ($errors) { output_form_function($errors); }
else {
process_form_function();
output_success_message();
}
}
else { output_form_function(null); }
function error_checking_function() {
$errors = null;
// The associative array allows us to assign "plain English"
// values to the fields. It really should be a global
// array passed to the function, as it can be used in
// process_form below.
$requireds = array(
'termnew' => 'New Term',
'ComputerNamenew' = 'New Computer Name'
);
foreach ($requireds as $req => $english) {
if (! isset($_POST[$req]) or (isset($_POST[$req]) and empty($_POST[$req]))) {
$errors .= "<li>The $english field is required.</li>\n";
}
}
return $errors;
}
function output_form_function($errs) {
// You'd use a template system or output full HTML here.
// These variables should be in an array, but anyway . . .
//
$tn = (isset($_POST['termnew']))?$_POST['termnew']:null;
$cn = (isset($_POST['ComputerNamenew']))?$_POST[ComputerNamenew']:null;
//
$form = '<form method="post" action="yourscript.php">';
if ($errs) {
$form .= '<p class="errors">There are some errors in your form:</p><ul>' .
$errs . '</ul>';
}
//
$form .= '<p><Label for="termnew">New Term</label>
<input type="text" name="termnew" id="termnew" value="' . $tn . '"></p>
<p><Label for="ComputerNamenew">New Computer Name</label>
<input type="text" name="ComputerNamenew" id="ComputerNamenew" value="' . $tn . '"></p>
<p><input type="submit" value="Submit"></p>
</form>
';
echo $form;
exit;
}
function process_form_function() {
// We've already checked if they are empty so . . . .
$updated="UPDATE Inventory SET ComputerName = '" .
mysql_real_escape_string($_POST['ComputerNamenew']) . "' WHERE AssetTag = '" .
mysql_real_escape_string($_POST['termnew']). "'";
$result = mysql_query($updated) or die("Cannot execute update: " . mysql_error());
}
function output_success_message() {
// Customize as needed, see output form above.
echo 'Ta dah.';
exit;
}