Adding a Record Using the Admin Browser

 

Introduction.  Now we are going to augment our functionality by incorporating some capacity to add records.  We will basically make use of the forms we used on the client side with some slight modifications.  Though these need to be kept in a separate web for the security we are going to implement in the next webpage.

So, when the admin presses the Add button in the Admin Browser they will be redirected to a page

  • add_user.php
    • form for entering data

Then the entries on this form will be processed by a page called

  • write_to_user_registration.php

The changes that need to be made to the admin_browser.php are quite minimal.

  • put an Add submit button just to the left of the Update button
  • redirect the Admin to the add_user.php page

As usual the new code is highlighted in red.

 

<?php
// if the admin has pressed the Add button
// they need to be redirected to the add_user.php form
if ($cmdAdd != "")
{

header("location:add_user.php");

}
// assign the values for database access

$host = "localhost";
$user = "your_user_name";
$password = "your_password";
$db_name = "database_name";
$table_name = "user_registration";

session_start( );
// initializing the record count
// and session variable to keep track of current record

if (!isset($_SESSION['current_row']))
{

$_SESSION['current_row'] = 1;

}

// connecting to the database on battcave.com
$link = mysql_connect($host, $user, $password);
// constructing the query string
$query_string = "SELECT * FROM $table_name";
// executing the query
$result_set = mysql_db_query($db_name, $query_string, $link);
// obtaining the number of rows in the record set
$number_rows = mysql_num_rows($result_set);
// Now we have our code that handles the command buttons
// the block of code to move the admin to the previous record
//
// if the First button has been pressed

if ($cmdFirst != "")
{

// setting the row counter to 1
$_SESSION['current_row'] = 1;

}
// the block of code to move the admin to the previous record
// if the Previous button has been pressed

if ($cmdPrevious != "")
{

// making certain the admin is not already at the first record
if ($_SESSION['current_row'] == 1)
{

echo("<font size=5>You are already at the first record!</font>");

}
else
{

$_SESSION['current_row'] = $_SESSION['current_row'] - 1;

}

}
// the block of code to move the admin to the next record
// if the Next button has been pressed

if ($cmdNext != "")
{

// making certain the admin is not already at the last record
if ($_SESSION['current_row'] == $number_rows)
{

echo("<font size=5>You are already at the last record!</font>");

}
else
{

$_SESSION['current_row'] = $_SESSION['current_row'] + 1;

}

}
// if the Last button has been pressed
if ($cmdLast != "")
{

// setting the row counter to the last record
$_SESSION['current_row'] = $number_rows;

}
// if the Update button has been pressed
if ($cmdUpdate != "")
{

// ensuring the checkboxes have some value
if ($chk_php != 1) $chk_php = "";
if ($chk_jsp != 1) $chk_jsp = "";
if ($chk_mysql != 1) $chk_mysql = "";
// setting a variable to use for the update query
$user_id_number = $_SESSION['current_user_id'];
// developing the update query string based on the user_id
$update_string = "UPDATE $table_name SET
first_name = '$txt_first_name',
last_name = '$txt_last_name',
email = '$txt_email',
password = '$txt_password',
php_interest = '$chk_php',
jsp_interest = '$chk_jsp',
mysql_interest = '$chk_mysql',
credit_card = '$sel_credit_card',
education = '$rb_education'
WHERE (user_id = '$user_id_number')";
// updating the entry
$update_set = mysql_db_query($db_name, $update_string, $link);
// giving the admin a message that the update occurred
echo("<font size=4><b>The update was completed</b></font>");
// change the current record and display based on where you are
if ($_SESSION['current_row'] == 1)
{

$_SESSION['current_row'] == 2;

}
else
{

$_SESSION['current_row'] = $_SESSION['current_row'] - 1;

}

}
// if the Delete button has been pressed
if ($cmdDelete != "")
{

// setting a variable to use for the delete query
$user_id_number = $_SESSION['current_user_id'];
// developing the deletion query string based on the user_id
$delete_string = "DELETE FROM $table_name WHERE (user_id = '$user_id_number')";
// deleting the entry
$delete_set = mysql_db_query($db_name, $delete_string, $link);
// decrementing the total number of entries due to the deletion
$number_rows--;
// change the current record and display based on where you are
if ($_SESSION['current_row'] == 1)
{

$_SESSION['current_row'] = 2;

}
else
{

$_SESSION['current_row'] = $_SESSION['current_row'] - 1;

}

}
// a variable for printing
$current_row_number = $_SESSION['current_row'];
// go to this current row
mysql_data_seek($result_set, $_SESSION['current_row']);
// and retrieve it into an array
$row = mysql_fetch_row($result_set);
// if the Find button has been pressed
if ($cmdFind != "")
{

// appending a % at the beginning and end
// in order to get best use of LIKE function
$txt_find = '%'.$txt_find.'%';
// developing the find query string based on the user_id
$find_string = "SELECT * FROM $table_name WHERE (last_name LIKE '$txt_find')";
// executing the query
$find_set = mysql_db_query($db_name, $find_string, $link);
// change the current record and display based on what you find
if (mysql_num_rows($find_set) > 0)
{

$row = mysql_fetch_row($find_set);
// I need to figure out a way to determine the current row number
// So far I have not found an appropriate built-in function

}
else
{

echo("<font size=4><b>Couldn't find such a record</b></font>");

}

}
?>
<html>
<head>
<title>Admin Browser for User Registration Web</title>
</head>

<body bgcolor = "00bbdd" text="004466" link="004466" vlink="007799">
<form action="admin_browser.php" method=post>
<h2>Admin Browser for User Registration</h2>
<table>
<tr>
<td><font size = 4 color=004466>First Name:</font>
</td>
<td><input type=text name="txt_first_name" size=20 value = "<?php echo $row[1]; ?>">
</td>
</tr>
<tr>
<td><font size = 4 color=004466>Last Name:</font>
</td>
<td><input type=text name="txt_last_name" size=20 value = "<?php echo $row[2]; ?>">
</td>
</tr>
<tr>
<td><font size = 4 color=004466>EMail Address:</font>
</td>
<td><input type=text name="txt_email" size=50 value = "<?php echo $row[3]; ?>">
</td>
</tr>
<tr>
<td><font size = 4 color=004466>Password:</font>
</td>
<td><input type=password name="txt_password" size=50 value = "<?php echo $row[4]; ?>">
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td></td>
</tr>
<tr>
<td><font size = 4 color=004466>Interests:</font>
</td>
<td><input type=checkbox name="chk_php" value=1
<?php if (($row[5]) == 1 ) echo 'checked'; ?>>
<font size = 4 color=004466>PHP</font>
</td>
</tr>
<tr>
<td>
</td>
<td><input type=checkbox name="chk_jsp" value=1
<?php if (($row[6]) == 1 ) echo 'checked'; ?>>
<font size = 4 color=004466>JSP</font>
</td>
</tr>
<tr>
<td>
</td>
<td><input type=checkbox name="chk_mysql" value=1
<?php if (($row[7]) == 1 ) echo 'checked'; ?>>
<font size = 4 color=004466>MySQL</font>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td></td>
</tr>
<tr>
<td><font size = 4 color=004466>Credit Card:</font>
</td>
<td><select name="sel_credit_card">
<option value="Discover" <?php if (($row[8]) == 'Discover') echo 'selected'; ?>>Discover
<option value="MasterCard" <?php if (($row[8]) == 'MasterCard') echo 'selected'; ?>>Mastercard
<option value="Visa" <?php if (($row[8]) == 'Visa') echo 'selected'; ?>>Visa
</select>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td></td>
</tr>
<tr>
<td><font size = 4 color=004466>Education:</font>
</td>
<td><input type=radio name="rb_education" value="NoCollege"
<?php if (($row[9]) == 'NoCollege') echo 'checked'; ?>>
<font size = 4 color=004466>No College</font><br>
<input type=radio name="rb_education" value="College"
<?php if (($row[9]) == 'College') echo 'checked'; ?>>
<font size = 4 color=004466>College</font><br>
<input type=radio name="rb_education" value="Masters"
<?php if (($row[9]) == 'Masters') echo 'checked'; ?>>
<font size = 4 color=004466>Masters</font><br>
<input type=radio name="rb_education" value="PhD"
<?php if (($row[9]) == 'PhD') echo 'checked'; ?>>
<font size = 4 color=004466>Ph.D.</font>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td></td>
</tr>
<tr>
<td align = right>
<?php echo("<font size=4>Record $current_row_number</font>"); ?>
</td>
<td align = left>
<?php echo("<font size=4>of $number_rows</font>"); ?>
</td>
</tr>
<tr>
<td align = right>
<input type = submit name="cmdFirst" value="First">
<input type = submit name="cmdPrevious" value="Previous">
</td>
<td align = left>
<input type = submit name="cmdNext" value="Next">
<input type = submit name="cmdLast" value="Last">
</td>
</tr>

<tr>
<td align = right>
<input type = submit name="cmdAdd" value="Add">
<input type = submit name="cmdUpdate" value="Update">
</td>
<td align = left>
<input type = submit name="cmdDelete" value="Delete">
</td>
</tr>
<tr>
<td align = right>
<input type = submit name="cmdFind" value="Find Last Name">
</td>
<td align = left>
<input type = txt name="txt_find" size=15>
</td>
</tr>
</table>
</form>
<?php
// establishing a session variable that can be used to
// obtain the user_id of the record currently displayed
// to be accessed when we return
// this will be important for deletions and modifications

$_SESSION['current_user_id'] = $row[0];
mysql_close($link);
?>
</body>
</html>

 

Since these changes are so minimal we won't discuss them further.

The form should now look like the following.  Notice there are no changes to the form other than there is an additional Update button in the same row with the Delete button.

 

 

So you should modify the code for your server's connections to MySQL and then upload it and move around.

With hardly any more explanation, since we essentially used the same files on the client side, you need the following file add_user.php.

 

<html>
<head>
<title>HTML Form for Adding a User</title>
</head>

<body bgcolor = "00bbdd" text="004466" link="004466" vlink="007799">
<form action="write_to_user_registration.php" method=post>
<table>
<tr>
<td><font size = 4 color=004466>First Name:</font>
</td>
<td><input type=text name="txt_first_name" size=20>
</td>
</tr>
<tr>
<td><font size = 4 color=004466>Last Name:</font>
</td>
<td><input type=text name="txt_last_name" size=20>
</td>
</tr>
<tr>
<td><font size = 4 color=004466>EMail Address:</font>
</td>
<td><input type=text name="txt_email" size=50>
</td>
</tr>
<tr>
<td><font size = 4 color=004466>Password:</font>
</td>
<td><input type=password name="txt_password" size=50>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td></td>
</tr>
<tr>
<td>&nbsp;</td>
<td></td>
</tr>
<tr>
<td><font size = 4 color=004466>Interests:</font>
</td>
<td>
<input type=checkbox name="chk_php" value=1>
<font size = 4 color=004466>PHP</font>
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type=checkbox name="chk_jsp" value=1>
<font size = 4 color=004466>JSP</font>
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type=checkbox name="chk_mysql" value=1>
<font size = 4 color=004466>MySQL</font>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td></td>
</tr>
<tr>
<td><font size = 4 color=004466>Credit Card:</font>
</td>
<td><select name="sel_credit_card">
<option value="Discover">Discover
<option value="MasterCard">Mastercard
<option value="Visa">Visa
</select>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td></td>
</tr>
<tr>
<td><font size = 4 color=004466>Education:</font>
</td>
<td>
<input type=radio name="rb_education" value="NoCollege">
<font size = 4 color=004466>No College</font><br>
<input type=radio name="rb_education" value="College">
<font size = 4 color=004466>College</font><br>
<input type=radio name="rb_education" value="Masters">
<font size = 4 color=004466>Masters</font><br>
<input type=radio name="rb_education" value="PhD">
<font size = 4 color=004466>Ph.D.</font>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td></td>
</tr>
<tr>
<td colspan = 2 align = center><input type = submit name="submit" value="submit">
</td>
</tr>
</table>
</form>
</body>
</html>

 

Now we need our processing script that will become apparent only if something goes  wrong during the INSERT query.  You should call this file write_to_user_registration.php.  The one significant change, a redirection back to the Admin Browser if everything inserts correctly, is highlighted in red.

 

<?php
// obtaining and prepping the input data
$txt_first_name = trim($txt_first_name);
$txt_last_name = trim($txt_last_name);
$txt_email = trim($txt_email);
// ensuring the checkboxes have some value
if ($chk_php != 1) $chk_php = "";
if ($chk_jsp != 1) $chk_jsp = "";
if ($chk_mysql != 1) $chk_mysql = "";

// assign the values for database access
$host = "localhost";
$user = "desaighuweb";
$password = "globekid";
$db_name = "desaighuweb";
$table_name = "user_registration";

// connecting to the database on OneStop.net
$link = mysql_connect($host, $user, $password);
// constructing the query string
$query_string = "INSERT INTO $table_name VALUES
('0', '$txt_first_name', '$txt_last_name', '$txt_email', '$txt_password',
'$chk_php', '$chk_jsp', '$chk_mysql', '$sel_credit_card', '$rb_education', Now( ))";

if (mysql_db_query($db_name, $query_string, $link))
{

header("location:admin_browser.php");

}
else
{

print("<p align='center'><font size = 4>The addition could not be completed</font></p>");
print("<p align='center'><a href='admin_browser.php'><font size = 4><b>Admin Browser</b></font></a></p>");

}

mysql_close($link);
?>
<html>
<head>
<title>Writing to a Table in MySQL Using PHP</title>
</head>

<body bgcolor = "00bbdd" text="004466" link="004466" vlink="007799">
</body>
</html>