|
Posted by Steve on 10/17/06 15:16
"linda" <newsaccount@tiscali.co.uk> wrote in message
news:4533f42a$1_2@mk-nntp-2.news.uk.tiscali.com...
|I very new to php could someone please explain how to write an error
message
| into my AddNew.php below. id is set to unique so at the moment if someone
| adds a product and the id is already in the database, it just doesn't
enter
| anything. Ideally I would like to display an error message, with a back
| button to correct the id.
|
| So far I've tried running a SELECT query with an if statement prior to
the
| INSERT but I just don't know enough php to get this to work. I would be
| very greatful for any info and advice from those in this newsgroup.
|
| Many, many thanks in advance,
| Linda
linda,
here's a basic maintenence script i have for managing "people"...forget
about the require_once files and just look at the programmatic flow. you'll
need to change the db calls so that they are appropriate for your setup.
notice there is some validation in javascript but you also need to add the
same validation in php since not all your visitors/customers will have js
enabled. sorry for the text-wrapping. and contrary to the dipshit comment
made by ikciu, this is NOT a stupid question at all.
hth,
me
===========
<?
$pageTitle = 'People';
$fullHeader = false;
$securityEnabled = true;
require_once 'relative.path.php';
require_once $relativePath . 'site.cfg.php';
$add = isset($_REQUEST['add']);
$back = isset($_POST['back']);
$confirm = isset($_POST['confirm']);
$delete = isset($_REQUEST['delete']);
$edit = isset($_REQUEST['edit']);
$action = $add ? 'add'
: 'edit';
$method = isset($_POST['method']) ? $_POST['method']
: '';
$errors = array();
$id = isset($_REQUEST['id']) ?
$_REQUEST['id'] : 0;
$personFirstName = isset($_REQUEST['personFirstName']) ?
$_REQUEST['personFirstName'] : '';
$personMiddleName = isset($_REQUEST['personMiddleName']) ?
$_REQUEST['personMiddleName'] : '';
$personLastName = isset($_REQUEST['personLastName']) ?
$_REQUEST['personLastName'] : '';
$personUserName = isset($_REQUEST['personUserName']) ?
$_REQUEST['personUserName'] : '';
$personPassword = isset($_REQUEST['personPassword']) ?
$_REQUEST['personPassword'] : '';
$personRePassword = isset($_REQUEST['personRePassword']) ?
$_REQUEST['personRePassword'] : '';
$personEmail = isset($_REQUEST['personEmail']) ?
$_REQUEST['personEmail'] : '';
$personPhoto = isset($_REQUEST['personPhoto']) ?
$_REQUEST['personPhoto'] : '';
$setSiteAccess = isset($_REQUEST['setSiteAccess']) ?
$_REQUEST['setSiteAccess'] : false;
$copyUserPermissions = isset($_REQUEST['copyUserPermissions']) ?
$_REQUEST['copyUserPermissions'] : false;
$passwordMismatch = strtolower($personPassword) !=
strtolower($personRePassword);
if ($back)
{
$add = false;
$delete = false;
$edit = false;
$method = '';
}
if ($confirm)
{
$add = false;
$delete = false;
$edit = false;
$method = '';
$sql = "
DELETE
FROM people
WHERE Id = '" . $db->prepare($id) . "'
";
$db->execute($sql);
header('location:' . $_SERVER['PHP_SELF']);
exit;
}
if (!($delete || $confirm) && $method == 'put')
{
$sql = "
SELECT COUNT(*) PersonExists
FROM people
WHERE Id != '" .
$db->prepare($id) . "'
AND LOWER(UserName) = LOWER('" .
$db->prepare($personUserName) . "')
";
unset($records);
$records = $db->execute($sql);
$personExists = $records[0]['PERSONEXISTS'] ? true : false;
if (!$personFirstName)
{
$errors['personFirstName'] = 'FIRST NAME is required and cannot be
blank.';
}
if (!$personLastName)
{
$errors['personLastName'] = 'LAST NAME is required and cannot be
blank.';
}
if (!$personPassword)
{
$errors['personPassword'] = 'PASSWORD is required and cannot be
blank.';
}
if ($personPassword && ($personPassword != $personRePassword))
{
$errors['personPassword'] = 'The PASSWORD does not match the PASSWORD
CONFIRMATION.';
}
if ($personExists)
{
$errors['personUserName'] = 'A person with this USER NAME is already
being used.';
}
if ($personEmail && !isEmail($personEmail))
{
$errors['personEmail'] = 'Invalid EMAIL ADDRESS.';
}
if (!count($errors))
{
if ($action == 'add')
{
$sql = "
INSERT INTO people
(
FirstName ,
MiddleName ,
LastName ,
UserName ,
Password ,
Email
)
VALUES
(
'" . $db->prepare($personFirstName) . "' ,
'" . $db->prepare($personMiddleName) . "' ,
'" . $db->prepare($personLastName) . "' ,
'" . $db->prepare($personUserName) . "' ,
'" . $db->prepare($personPassword) . "' ,
'" . strtolower($db->prepare($personEmail)) . "'
)
";
} else {
$sql = "
UPDATE people
SET FirstName = '" . $db->prepare($personFirstName)
.. "' ,
MiddleName = '" . $db->prepare($personMiddleName)
.. "' ,
LastName = '" . $db->prepare($personLastName)
.. "' ,
UserName = '" . $db->prepare($personUserName)
.. "' ,
Password = '" . $db->prepare($personPassword)
.. "' ,
Email = '" .
strtolower($db->prepare($personEmail)) . "'
WHERE Id = '" . $id . "'
";
}
$db->execute($sql);
header('location:' . $_SERVER['PHP_SELF']);
exit;
}
}
require_once $site->includeDirectory . 'head.inc.php';
?>
<br>
<div class="bullet" style="background:white no-repeat url('<?=
$site->imagesDirectory ?>bullet.jpg');">
People
</div>
<hr>
<br>
<?
if ($add || $edit)
{
?>
<script language="javascript">
var skipValidation = false;
function validate()
{
if (skipValidation){ return true; }
var warning = new String();
var el = record.personFirstName;
if (warning.length == 0 && trim(el.value) == '')
{
warning = "FIRST NAME is required.";
}
var el = record.personLastName;
if (warning.length == 0 && trim(el.value) == '')
{
warning = "LAST NAME is required.";
}
var el = record.personUserName;
if (warning.length == 0 && trim(el.value) == '')
{
warning = "USER NAME is required.";
}
var el = record.personEmail;
if (warning.length != 0 && !isEmail(el.value))
{
warning = "Invalid EMAIL.";
}
if (warning.length)
{
alert(warning);
el.focus()
el.select();
return false;
}
return true;
}
</script>
<?
if (!count($errors))
{
$sql = "
SELECT Id ,
FirstName ,
MiddleName ,
LastName ,
UserName ,
Password ,
Email
FROM people
WHERE id = '" . $db->prepare($id) . "'
";
unset($records);
$records = $db->execute($sql);
$id = $records[0]['ID'];
$personFirstName = $records[0]['FIRSTNAME'];
$personMiddleName = $records[0]['MIDDLENAME'];
$personLastName = $records[0]['LASTNAME'];
$personUserName = $records[0]['USERNAME'];
$personPassword = $records[0]['PASSWORD'];
$personRePassword = $records[0]['PASSWORD'];
$personEmail = $records[0]['EMAIL'];
}
if (count($errors))
{
$displayedErrors = array_unique(array_values($errors));
?>
<div style="color:#660000; font-size:10pt; font-weight:bold;">
ERROR
</div>
<hr style="background-color:#660000; color:#660000;">
<ol>
<?= '<li style="color:#660000; font-size:8pt;">' . implode('<li
style="color:#660000; font-size:8pt;">' . "\r\n", $displayedErrors) ?>
</ol>
<hr style="background-color:#660000; color:#660000;">
<br>
<br>
<?
}
if ($delete)
{
?>
<div style="color:#CC0000; font-size:10pt; font-weight:500;
margin-bottom:20px;">
Are you sure you want to delete this record?
<br>
If so, click the "Confirm" button below. Otherwise, click the "Back"
button below to return.
</div>
<?
}
?>
<form name="record" method="post" onsubmit="return validate();">
<table style="width:600px;">
<tr>
<td class="label" style="vertical-align:top;">
First Name
<span style="color:#660000; font-size:10pt;"><?=
(isset($errors['personFirstName']) ? '*' : '') ?></span>
</td>
<td colspan="2">
<input class="value"
name="personFirstName"
maxlength="255"
type="text"
autocomplete="off"
value="<?= $personFirstName ?>"
>
</td>
</tr>
<tr>
<td class="label">
Middle Name
<span style="color:#660000; font-size:10pt;"><?=
(isset($errors['personMiddleName']) ? '*' : '') ?></span>
</td>
<td colspan="2">
<input class="value"
name="personMiddleName"
maxlength="255"
type="text"
autocomplete="off"
value="<?= $personMiddleName ?>"
>
</td>
</tr>
<tr>
<td class="label">
Last Name
<span style="color:#660000; font-size:10pt;"><?=
(isset($errors['personLastName']) ? '*' : '') ?></span>
</td>
<td colspan="2">
<input class="value"
name="personLastName"
maxlength="255"
type="text"
autocomplete="off"
value="<?= $personLastName ?>"
>
</td>
</tr>
<tr>
<td class="label">
User Name
<span style="color:#660000; font-size:10pt;"><?=
(isset($errors['personUserName']) ? '*' : '') ?></span>
</td>
<td colspan="2">
<input class="value"
name="personUserName"
maxlength="255"
type="text"
autocomplete="off"
value="<?= $personUserName ?>"
>
</td>
</tr>
<tr>
<td class="label">
Password
<span style="color:#660000; font-size:10pt;"><?=
(isset($errors['personPassword']) ? '*' : '') ?></span>
</td>
<td colspan="2">
<input class="value"
name="personPassword"
maxlength="255"
type="text"
autocomplete="off"
value="<?= $personPassword ?>"
>
</td>
</tr>
<tr>
<td class="label">
Password Confirmation
<span style="color:#660000; font-size:10pt;"><?=
(isset($errors['personRePassword']) ? '*' : '') ?></span>
</td>
<td colspan="2">
<input class="value"
name="personRePassword"
maxlength="255"
type="text"
autocomplete="off"
value="<?= $personRePassword ?>"
>
</td>
</tr>
<tr>
<td class="label">
Email
<span style="color:#660000; font-size:10pt;"><?=
(isset($errors['personEmail']) ? '*' : '') ?></span>
</td>
<td colspan="2">
<input class="value"
name="personEmail"
maxlength="255"
type="text"
autocomplete="off"
value="<?= $personEmail ?>"
style="text-transform:lowercase;"
>
</td>
</tr>
<tr><td colspan="3"> </td></tr>
<tr><td colspan="3"> </td></tr>
</table>
<?
if ($delete)
{
?>
<input name="confirm" type="submit" style="cursor:'hand';
width:100px;" value="Confirm " onclick="skipValidation=true;">
<?
} else {
?>
<input name="<?= $action ?>" type="submit" style="cursor:'hand';
width:100px;" value="Save ">
<input name="delete" type="submit" style="cursor:'hand';
width:100px;" value="Delete " onclick="skipValidation=true;">
<?
}
?>
<input name="back" type="submit" style="cursor:'hand';
width:100px;" value="Back " onclick="skipValidation=true;">
<input name="id" type="hidden" value="<?= $id ?>">
<input name="method" type="hidden" value="put">
</form>
<script language="javascript">
record.personFirstName.focus();
record.personFirstName.select();
</script>
<?
echo $sessionFooter;
exit;
}
$lastSort = isset($_REQUEST['lastSort']) ?
$_REQUEST['lastSort'] : '';
$sort = isset($_REQUEST['sort']) ? $_REQUEST['sort']
: 'LASTNAME';
$sortDirection = isset($_REQUEST['sortDirection']) ?
$_REQUEST['sortDirection'] : 'ASC';
if ($sort == $lastSort){ $sortDirection = $sortDirection == 'ASC' ? 'DESC' :
'ASC'; }
$lastSort = $sort;
$group = $_REQUEST['group'];
$page = $_REQUEST['page'];
$pages = $_REQUEST['pages'];
$recordsPerPage = $_REQUEST['recordsPerPage'];
if (!is_numeric($group)){ $group = 0; }
if (!is_numeric($page)){ $page = 1; }
if (!is_numeric($pages)){ $pages = 5; }
if (!is_numeric($recordsPerPage)){ $recordsPerPage = 10; }
$sql = "
SELECT COUNT(*) RecordCount
FROM people
";
unset($records);
$records = $db->execute($sql);
$recordCount = $records[0]['RECORDCOUNT'];
$currentPage = $page;
$group = floor((($page - 1) * $recordsPerPage) / ($recordsPerPage *
$pages));
$navigation = array();
$page = $group * $pages + 1;
$lastPage = $page + $pages;
$maxPages = ceil($recordCount / $recordsPerPage);
$range = ($currentPage * $recordsPerPage) - $recordsPerPage;
if ($group)
{
$navigation[] = '<a class="navigation" href="??page=' . ($page - 1) .
'&sortDirection=' . $sortDirection . '&sort=' . $sort . '"
title="Previous"><<</a>';
}
for ($index = 0; $index < $pages; $index++)
{
$navigation[] = '<a class="navigation" href="?page=' . $page .
'&sortDirection=' . $sortDirection . '&sort=' . $sort . '" title="Page ' .
$page . '">' . $page. '</a>';
if ($page == $maxPages){ break; }
$page++;
}
if ($lastPage < $maxPages)
{
$navigation[] = '<a class="navigation" href="?page=' . $page .
'&sortDirection=' . $sortDirection . '&sort=' . $sort . '"
title="Next">>></a>';
}
$navigation[] = '<span style="color:#666666; padding-left:25px;
font-size:7.25pt;">[Page ' . $currentPage . ' of ' . $maxPages . ']</span>';
?>
<table style="width:730px;">
<th style="background-color:#336699; border-bottom:1px solid
lightsteelblue; padding:5px; text-align:center; width:100px;">
<a
class="menuItem"
href="<?= $_SERVER['PHP_SELF'] ?>?add=1"
style="background-color:#336699; border:none; color:white;
font-weight:600; text-decoration:underline;"
>Add Person</a>
</th>
<th
style="border-bottom:1px solid lightsteelblue; border-right:1px solid
lightsteelblue; cursor:pointer; font-size:8pt;"
title="Click to sort"
onclick="document.location='<?= $_SERVER['PHP_SELF'] ?>?lastSort=<?=
$lastSort ?>&sortDirection=<?= $sortDirection ?>&sort=LASTNAME'"
>Last Name</th>
<th
style="border-bottom:1px solid lightsteelblue; border-right:1px solid
lightsteelblue; cursor:pointer; font-size:8pt;"
title="Click to sort"
onclick="document.location='<?= $_SERVER['PHP_SELF'] ?>?lastSort=<?=
$lastSort ?>&sortDirection=<?= $sortDirection ?>&sort=FIRSTNAME'"
>First Name</th>
<th
style="border-bottom:1px solid lightsteelblue; border-right:1px solid
lightsteelblue; cursor:pointer; font-size:8pt;"
title="Click to sort"
onclick="document.location='<?= $_SERVER['PHP_SELF'] ?>?lastSort=<?=
$lastSort ?>&sortDirection=<?= $sortDirection ?>&sort=MIDDLENAME'"
>Middle Name</th>
<th
style="border-bottom:1px solid lightsteelblue; border-right:1px solid
lightsteelblue; cursor:pointer; font-size:8pt;"
title="Click to sort"
onclick="document.location='<?= $_SERVER['PHP_SELF'] ?>?lastSort=<?=
$lastSort ?>&sortDirection=<?= $sortDirection ?>&sort=EMAIL'"
>Email</th>
<?
$sql = "
SELECT Id ,
FirstName ,
MiddleName ,
LastName ,
UserName ,
Email
FROM people
ORDER BY " . $sort . " " . $sortDirection . "
LIMIT " . $range . ", " . $recordsPerPage . "
";
unset($records);
$records = $db->execute($sql);
foreach ($records as $record)
{
$description = trim($record['LASTNAME'] . ', ' . $record['FIRSTNAME'] . '
' . substr($record['MIDDLENAME'], 0, 1));
?>
<tr>
<td style="font-size:8pt; text-align:right;">
<a
class="menuItem"
href="<?= $_SERVER['PHP_SELF']?>?edit=1&id=<?= $record['ID'] ?>"
style="font-size:7.25pt; font-weight:normal;
text-decoration:underline; white-space:nowrap;"
title="EDIT [ <?= $description ?> ]"
>Edit ▷</a>
<br>
<a
class="menuItem"
href="<?= $_SERVER['PHP_SELF']?>?edit=1&delete=1&id=<?=
$record['ID'] ?>"
style="font-size:7.25pt; font-weight:normal;
text-decoration:underline; white-space:nowrap;"
title="DELETE [ <?= $description ?> ]"
>Delete ▷</a>
</td>
<td style="width:150px;"><?= $record['LASTNAME'] ?></td>
<td style="font-size:8pt;"><?= $record['FIRSTNAME'] ?></td>
<td style="font-size:8pt;"><?= $record['MIDDLENAME'] ?></td>
<td style="font-size:8pt;"><?= $record['EMAIL'] ?></td>
</tr>
<?
}
?>
</table>
<br>
<hr>
<br>
<?
if (!count($records))
{
echo 'There are currently no records to display.';
} else {
?>
<div style="float:right; margin-right:15px;">
<?
echo implode("\r\n", $navigation);
?>
</div>
<br clear="all">
<br>
<?
}
echo $sessionFooter;
?>
Navigation:
[Reply to this message]
|