/    Sign up×
Community /Pin to ProfileBookmark

Admin register a new user

Hi guys,

Can anyone help me; I have created a registration form (can be use for create or modify) and login form (Admin). What I am trying to do is; once the admin log in he/she can create / register a new user which contains:

-Firstname
-Surname
-Address
-Mobile
-Dept Name
-Username
-Password
-Repeat Password

My DB will look like this:

[B]Table PERSONS:[/B] id, firstname, surname, address, mobile, dept_id, username, password.

[B]Table USER[/B]: id, username, password

[B]Table DEPT[/B]: id, dept_name

Can anyone help me how am I going to related the USER table into the PERSONS so when admin register a new user – the data will be created the into database as well as the data can be extracted for modification. Any suggestion?

Here are my code:

[B]register.php[/B]

[code=php]
<?php
require ‘includes/application_top.php’;

if (!isset($_POST[‘name’]) && isset($_GET[‘id’])) {

$mode = “Modifying”;
// Get data from DB

$q = “SELECT * FROM `persons` WHERE `ID` = ‘”.$_GET[‘id’].”‘”;
$result = mysql_query($q) or die (mysql_error());
$row = mysql_fetch_array($result);

$name = $row[‘firstname’];
$surname = $row[‘surname’];
$address = $row[‘address’];
$dept = $row[‘dept_id’];
$mobile = $row[‘mobile’];

}else if (!isset($_POST[‘name’]) && !isset($_GET[‘id’])) {

$mode = “Register”;

// Data is empty

$name = $surname = $address = $dept = $mobile = “”;

} else {

$errors = array();
if ($_POST[‘name’] == “”) $errors[] = “Name”;
if ($_POST[‘surname’] == “”) $errors[] = “Surname”;
if ($_POST[‘mobile’] == “” || !is_numeric ($_POST[‘mobile’])) $errors[] = “Mobile No”;

if (count($errors)) {

$errormsg = “Please fill the blank info:<br/ >”.implode(‘<br />’,$errors);

$mode = $_POST[‘mode’];
$name = $_POST[‘name’];
$surname = $_POST[‘surname’];
$address = $_POST[‘address’];
$dept = $_POST[‘dept’];
$mobile = $_POST[‘mobile’];

} else {
foreach ($_POST as $key => $val) {

$_SESSION[$key] = $val;
}
header(“Location: confirmPage.php”);

}

}

?>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>Modify Document</title>
</head>

<body>

<form action=”<?php echo $_SERVER[‘PHP_SELF’]; ?>” method=”post”>
<?php if (isset($errormsg)) echo “<div id=”error_message” style=”color:red;”>$errormsg</div>”; ?>
<div align=”center”>

<table width=”370″ border=”0″>
<h1> <?php echo $mode; ?> A User </h1>
<p><font color=”orangered” size=”+1″><tt><b>*</b></tt></font>
indicates a required field</p>
<tr>
<th width=”200″ height=”35″ align=”left” scope=”row” >First Name
<font color=”orangered” size=”+1″><tt><b>*</b></tt></font> </th>
<td width=”160″><input type=”text” name=”name” value=”<?php echo $name;?>” size=”25″/></td>
</tr>

<tr>
<th height=”35″ align=”left”> Surname
<font color=”orangered” size=”+1″><tt><b>*</b></tt></font> </th>
<td>
<input type=”text” name=”surname” value=”<?php echo $surname; ?>” size=”25″/></td>
</tr>

<tr>
<th height=”35″ align=”left”> Address</th>
<td>
<input type=”text” name=”address” value=”<?php echo $address; ?>” size=”25″/></td>
</tr>

<tr>
<th height=”35″ align=”left”>
Choose a username <font color=”orangered” size=”+1″><tt>*</tt></font></th>
<td>
<input name=”username” type=”text” maxlength=”100″ size=”25″ /> </td>
</tr>

<tr>
<th height=”35″ align=”left”>
Choose a password
<font color=”orangered” size=”+1″><tt><b>*</b></tt></font> </th>
<td>
<input name=”password” type=”password” maxlength=”100″ size=”25″ /> </td>
</tr>

<tr>
<th height=”35″ align=”left”>
Repeat your password
<font color=”orangered” size=”+1″><tt><b>*</b></tt></font> </th>
<td>
<input name=”repeatpassword” type=”password” maxlength=”100″ size=”25″ /> </td>
</tr>

<tr>
<th height=”35″ align=”left”>Department</th>
<td>

<select name=”dept”>
<option value=””>Select..</option>

<?php

$data = mysql_query (“SELECT * FROM `dept` ORDER BY `id` DESC”) or die (mysql_error());
while($row_dept = mysql_fetch_array( $data )) {

?>
<option value=”<?php echo $row_dept[‘id’] ;?>” <?php if($row_dept[‘id’]==$dept){echo ‘ selected=”selected”‘;}?>>
<?php echo $row_dept[‘dept_name’] ;?> </option>

<?php
}
?>
</select> </td>
</tr>

<tr>
<th height=”35″ align=”left”>Mobile</th>
<td><input type=”text” name=”mobile” value=”<?php echo $mobile; ?>” size=”25″/></td>
</tr>

<tr>
<td align=”right” colspan=”2″>
<hr noshade=”noshade” />
</td>
</tr>

</table>
<br/>

<a href=”index.php”>
<input type=”button” name=”back” value=”Back” /></a>

<input type=”hidden” name=”id” value=”<?php echo isset($_GET[‘id’]); ?>”>
<input type=”hidden” name=”mode” value=”<?php echo $mode; ?>”>

<input type=”submit” value=”<?php echo ($mode == “Register”) ? ‘Register’ : ‘Modify’; ?>”/>

</div>
</form>

</body>
</html>
[/code]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@Skylight_LadyFeb 24.2011 — Are you sure ID is displayed in the table "persons" as capital letters?

SELECT * FROM persons WHERE ID[/QUOTE]

You should have a user_type added to the table with a value of "admin" or "user" to seperate your admin user from your users.

If you are going to relate a user table to persons table then something like the following will work:

[code=php]$q = "SELECT * FROM user, persons WHERE user.id = persons.ID AND persons.ID = '".$_GET['id']."'";[/code]
×

Success!

Help @siabanie spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...