0% found this document useful (0 votes)
31 views38 pages

PHP and Mysql - Unit-V

Uploaded by

gantarojamani3
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
31 views38 pages

PHP and Mysql - Unit-V

Uploaded by

gantarojamani3
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 38

Unit-5

Interacting with MySQL using PHP

1. Discuss about MYSQL and its Features.

MYSQL:

 MySQL is one of the most popular relational database systems being


used on the Web today.
 It is freely available and easy to install, however if you have
installed WAMP server it already there on your machine.
 MySQL is easy to use, yet extremely powerful, fast, secure, and scalable.
 MySQL runs on a wide range of operating systems, including UNIX or
Linux, Microsoft Windows, Apple Mac OS X, and others.
 MySQL supports standard SQL (Structured Query Language).

MySQL Features:

 RDBMS: MySQL is a relational database management system.


 Easy to use: MySQL is easy to use. You have to get only the basic
knowledge of SQL. You can build and interact with MySQL with only a
few simple SQL statements.
 Secure: MySQL consist of a solid data security layer that protects
sensitive data from intruders. Passwords are encrypted in MySQL.
 Client / Server Architecture: MySQL follows a client / server
architecture. There is a database server (MySQL) and arbitrarily many
clients (application programs), which communicate with the server; that
is, they query data, save changes, etc.
 Free to download: MySQL is free to use and you can download it
from MySQL official website.
 Scalability: MySQL can handle almost any amount of data, up to as much
as 50 million rows or more. The default file size limit is about 4 GB.
However, you can increase this number to a theoretical limit of 8 TB of
data.

2. Explain the Advantages and Disadvantages of MYSQL?

Advantages / merits of MySQL:


 It’s easy to use: While a basic knowledge of SQL is required – and most
relational databases require the same knowledge – MySQL is very easy
to use. With only a few simple SQL statements, you can build and
interact with MySQL.
 It’s Fast: In the interest of speed, MySQL designers made the decision
to offer fewer features that other major database competitors, such as
Sybase* and Oracle*. However, despite having fewer features that the
other commercial database products, MySQL still offers all of the
features required by most database developers.
 It’s Secure: MySQL includes solid data security layer that protects
sensitive data from intruders. Rights can be set to allow some or all
privileges to individuals. Passwords are encrypted in MySQL.
 It’s Scalability: MySQL can handle almost any amount of data, up to as
much as 50 million rows or more. The default file size limit is about 4 GB.
However, you can increase this number to a theoretical limit of 8 TB of
data.
 It Manages memory very well: MySQL server has been thoroughly
tested to prevent memory leaks.

Disadvantages / Demerits of MySQL:

 MySQL does not support a very large database size as efficiently.


 MySQL does not support ROLE, COMMIT, and Stored procedures
in versions less than 5.0.
 Transactions are not handled very efficiently.
 There are a few stability issues.
 It suffers from poor performance scaling.
 The development is not community driven so it has lagged behind.
 The functionality tends to be heavily dependent on the add-ons.
 Developers may find some of its limitations very frustrating.

3. Difference between MySQL Vs MySQLi?

There are too many differences between these PHP database extensions.
These differences are based on some factors like performance, library
functions, features, benefits, and others.
MySQL MySQLi

MySQL extension added in PHP MySQLi extension added in PHP 5.5


version 2.0. and deprecated as of PHP and will work on MySQL 4.1.3 or
5.5.0. above.

Does not support prepared MySQLi supports prepared


statements. statements.

MySQL provides the procedural MySQLi provides both procedural


interface. and object-oriented interfaces.

MySQL extension does not support MySQLi supports store procedures.


stored procedures.

MySQL extension lags in security and MySQLi extension with enhanced


other special features, comparatively. security and improved debugging.

Transactions are handled by SQL MySQLi supports transactions


queries only. through API.

Extension directory: ext/mysql. Extension directory: ext/mysqli.

4. Discuss about connecting to MySQL with PHP.

 PHP is the most popular scripting language for web development.


 It is free, open source and server – side (the code is executed on the
server).
 MYSQL is a Relational Database Management System (RDBMS) that uses
Structured Query Language (SQL).
 It is also free and open source.
 The combination of PHP and MYSQL gives unmet options to create just
about any kind of website – from small contact form to large corporate
portal.

Making a Connection:

The Basic syntax for a connection to MYSQL is as follows:

$mysqli = mysqli_connect(“hostname”,”username”,”password”,”database”);

The value of $mysqli is the result of the function and is used in later
functions for communicating with MySQL.

With sample values inserted, the connection code looks like this:

$mysqli = mysqli_connect(“localhost”,”ashaz”,”password”,”TestDB”);

Example Program:

<?php

$mysqli = new mysqli(“localhost”,”ashaz”,”password”,”TestDB”);

If(mysqli_connect_errno())

printf(“Connect failed: %s\n”, mysqli_connect_error());

exit();

else

printf(“Host information: %s\n”,mysqli_get_host_info($mysqli));

?>
In the above example, it is creates a new connection in line 2 and then tests to
see whether an error occurred. If an error occurred, line 5 prints an error
message and uses the mysqli_connect_error() function to print the message. If
no error occurs, line 8 prints a message that includes host information
resulting from calling the mysqli_get_host_info() function.

Executing Queries:

 Half the battle in executing MySQL queries using PHP is knowing how to
write the SQL.
 The mysqli_query() function in PHP is used to send your SQL query to
MySQL.

Example Program:

<?php

$mysqli = new mysqli(“localhost”,”ashaz”,”password”,”TestDB”);

If(mysqli_connect_errno())

printf(“Connect failed: %s\n”, mysqli_connect_error());

exit();

else

$sql = “CREATE TABLE testable

(id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,

testField VARCHAR(75))”;

$res=mysqli_query($mysqli,$sql);

if($res == TRUE)

{
echo “Table testTable successfully created”;

else

printf(“Could not create table: %s\n”, mysqli_error($mysql));

mysqli_close($mysqli);

?>

 In lines 10 – 12, the text that makes up the SQL statement is assigned to
the variable $sql.
 The mysqli_query function returns a value of true or false, and this value
is checked in the if..else statement beginning in line 15.
 If the value of $res is true, a success message is printed to the screen.
 However, if the value of $res is not true and the table was not created,
an error message appears, generated by the mysqli_error() function.
 The mysqli_close() function is used to close the connection.

5. Discuss about working with MySQL Data.

 Inserting, updating, deleting and retrieving data all revolve around the
use of the mysqli_query() function to execute the basic SQL queries.
 The basic query commands like INSERT, UPDATE, and DELETE queries,
no additional scripting is required after the query has been executed
because you’re not displaying any results (unless you want to).
 When using SELECT queries, you have a few options for displaying the
data retrieved by your query.

Inserting Data with PHP:

The easiest method for inserting data at this stage in the game is to
simply hard – code the INSERT statement.
<?php

$mysqli = new mysqli(“localhost”,”ashaz”,”password”,”TestDB”);

If(mysqli_connect_errno())

printf(“Connect failed: %s\n”, mysqli_connect_error());

exit();

else

$sql = “INSERT INTO testTable(testField) VALUES (‘some value’)”;

$res = mysqli_query($mysqli,$sql);

If($res == TRUE)

echo “A Record has been inserted”;

else

printf(“Could not insert record: %s\n”, mysqli_error($mysqli));

mysqli_close($mysqli);

?>

 Here SQL query stored in the $sql variable on line 10 and text
modifications on lines 14 and 18.
 The connection code and the structure for issuing a query remain
the same.
 In fact, most procedural code for accessing MySQL fails into this same
type of code template.
 Call this script mysqlinsert.php and place it on your webserver.
 Running this script results in the addition of a row to the testTable table.
 You can either make a long list of hard – coded SQL statements and use
mysqli_query() multiple times to execute these statements.

Retrieving Data with PHP:

 Because you have a few rows in your testTable table, you can write
a PHP script to retrieve that data.
 Starting with the basics, we write a script that issues a SELECT query but
doesn’t over when you with result data.
 To get the number of rows then use mysqli_num_rows() function.

Example Program:

<?php

$mysqli = new mysqli(“localhost”,”ashaz”,”password”,”TestDB”);

If(mysqli_connect_errno())

printf(“Connect failed: %s\n”, mysqli_connect_error());

exit();

else

$sql = “SELECT * FROM testTable”;

$res = mysqli_query($mysqli,$sql);

If($res)
{

$numver_of_rows=mysqli_num_rows($res);

printf(“Result set has %d rows: \n”, $number_of_rows);

else

printf(“Could not retrieve records:%s\n”, mysqli_error($mysql));

mysqli_free_result($res);

mysqli_close($mysqli);

?>

 mysqli_num_rows() function to retrieve the number of rows in the


resultset($res), and it places the value in a variable called
$number_of_rows.
 mysqli_free_result() function ensures that all memory associated
with the query and result is freed for use by other scripts.

Update data:

The UPDATE statement is used to modify data in a table.

Syntax:

UPDATE table_name

SET column=value, column1=value1,...

WHERE someColumn=someValue ;

The following example updates some data in the "employee" table:

Example:
<?php

// Database connection establishment

$con=mysqli_connect("example.com","alex","qwerty","db_name");

// Check connection

if (mysqli_connect_errno($con))

echo "MySQL database connection failed: " . mysqli_connect_error();

mysqli_query($con,"UPDATE employee SET salary = `10000`

WHERE FirstName = `Anna` AND LastName = `Weston`");

?>

Delete data:

The DELETE FROM statement is used to delete data from a database table.

Syntax:

DELETE FROM tableName

WHERE someColumn = someValue;

Example:

<?php

// Database connection establishment

$con=mysqli_connect("example.com","alex","qwerty","db_name");

// Check connection

if (mysqli_connect_errno($con))

{
echo "MySQL database connection failed: " . mysqli_connect_error();

mysqli_query($con,"DELETE FROM employee WHERE LastName=`Rodriguez`


");

?>

6. Discuss about planning and creating Database Tables.

When you think of an address book, the obvious fields come to mind: name,
address, telephone number, email address. However, if you look at (or
remember) a paper-based address book, you might note there are several
entries for one person. Maybe that person has three telephone numbers, or
two email addresses, and so forth—whatever didn’t fit in the original template.
In an online address book, a set of related tables helps alleviate the
redundancy and repetition of information and allows you to display all
information in a unified view.

Address Book Table and Field

Names Table Name Field

Names

master_name id, date_added, date_modified, f_name, l_name

address id, master_id, date_added, date_modified, address,


city, state, zipcode, type

telephone id, master_id, date_added, date_modified,

tel_number, type fax id, master_id, date_added, date_modified,

fax_number, type

email id, master_id, date_added, date_modified,

email, type personal_notes id, master_id, date_added,

date_modified, note

The following SQL statement creates the master_name table:

CREATE TABLE master_name


(
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,

date_added DATETIME,

date_modified DATETIME,

f_name VARCHAR (75),

l_name VARCHAR (75)

);

CREATE TABLE address

id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,

master_id INT NOT NULL,

date_added DATETIME,

date_modified DATETIME,

address VARCHAR (255),

city VARCHAR (30),

state CHAR (2),

zipcode VARCHAR (10),

type ENUM (‘home’, ‘work’, ‘other’)

);

CREATE TABLE telephone

id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,

master_id INT NOT NULL,

date_added DATETIME,
date_modified DATETIME,

tel_number VARCHAR (25),

type ENUM (‘home’, ‘work’, ‘other’)

);

CREATE TABLE fax

id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,

master_id INT NOT NULL,

date_added DATETIME,

date_modified DATETIME,

fax_number VARCHAR (25),

type ENUM (‘home’, ‘work’, ‘other’)

);

CREATE TABLE email

id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,

master_id INT NOT NULL,

date_added DATETIME,

date_modified DATETIME,

email VARCHAR (150),

type ENUM (‘home’, ‘work’, ‘other’)

);

CREATE TABLE personal_notes


(

id int NOT NULL PRIMARY KEY AUTO_INCREMENT,

master_id INT NOT NULL UNIQUE,

date_added DATETIME,

date_modified DATETIME,

note TEXT

);

Now that your tables are created, you can work through the forms and scripts
for managing and viewing your records.

7. Discuss about Creating Menu.

Online address book contains several actions, so it makes sense to create a


menu for your links. Below Listing creates a simple menu for all the scripts
which are called mymenu.html

Address Book Menu

<!DOCTYPE html>

<html>

<head>

<title>My Address Book</title>

</head>

<body>

<h1>My Address Book</h1>

<p><strong>Management</strong></p>

<ul>
<li><a href=”addentry.php”>Add an Entry</a></li>

<li><a href=”delentry.php”>Delete an Entry</a></li>

</ul>

<p><strong>Viewing</strong></p>

<ul>

<li><a href=”selentry.php”>Select a Record</a></li>

</ul>

</body>

</html>

8. Discuss about Creating Record Addition Mechanism.

Basic Record Addition Script Called addentry.php

1: <?php

2: if ($_POST[op] != "add") {

3: //haven't seen the form, so show it

4: $display_block = "<h1>Add an Entry</h1>

5: <form method=\"post\" action=\"$_SERVER[PHP_SELF]\">

6: <P><strong>First/Last Names:</strong><br>

7: <input type=\"text\" name=\"f_name\" size=30 maxlength=75>

8: <input type=\"text\" name=\"l_name\" size=30 maxlength=75>

9:

10: <P><strong>Address:</strong><br>

11: <input type=\"text\" name=\"address\" size=30>

12:
13: <P><strong>City/State/Zip:</strong><br>

14: <input type=\"text\" name=\"city\" size=30 maxlength=50>

15: <input type=\"text\" name=\"state\" size=5 maxlength=2>

16: <input type=\"text\" name=\"zipcode\" size=10 maxlength=10>

17:

18: <P><strong>Address Type:</strong><br>

19: <input type=\"radio\" name=\"add_type\" value=\"home\" checked>


home

20: <input type=\"radio\" name=\"add_type\" value=\"work\"> work

21: <input type=\"radio\" name=\"add_type\" value=\"other\"> other

22:

23: <P><strong>Telephone Number:</strong><br>

24: <input type=\"text\" name=\"tel_number\" size=30 maxlength=25>

25: <input type=\"radio\" name=\"tel_type\" value=\"home\" checked> home

26: <input type=\"radio\" name=\"tel_type\" value=\"work\"> work

27: <input type=\"radio\" name=\"tel_type\" value=\"other\"> other

28:

29: <P><strong>Fax Number:</strong><br>

30: <input type=\"text\" name=\"fax_number\" size=30 maxlength=25>

31: <input type=\"radio\" name=\"fax_type\" value=\"home\" checked> home

32: <input type=\"radio\" name=\"fax_type\" value=\"work\"> work

33: <input type=\"radio\" name=\"fax_type\" value=\"other\"> other

34:

35: <P><strong>Email Address:</strong><br>


36: <input type=\"text\" name=\"email\" size=30 maxlength=150>

37: <input type=\"radio\" name=\"email_type\" value=\"home\" checked>


home

38: <input type=\"radio\" name=\"email_type\" value=\"work\"> work

39: <input type=\"radio\" name=\"email_type\" value=\"other\"> other

40:

41: <P><strong>Personal Note:</strong><br>

42: <textarea name=\"note\" cols=35 rows=5 wrap=virtual></textarea>

43: <input type=\"hidden\" name=\"op\" value=\"add\">

44:

45: <p><input type=\"submit\" name=\"submit\" value=\"Add Entry\"></p>

46: </FORM>";

47:

48: } else if ($_POST[op] == "add") {

49: //time to add to tables, so check for required fields

50: if (($_POST[f_name] == "") || ($_POST[l_name] == "")) {

51: header("Location: addentry.php");

52: exit;

53: }

54:

55: //connect to database

56: $conn = mysql_connect("localhost", "joeuser", "somepass")

57: or die(mysql_error());

58: mysql_select_db("testDB",$conn) or die(mysql_error());


59:

60: //add to master_name table

61: $add_master = "insert into master_name values ('', now(), now(),

62: '$_POST[f_name]', '$_POST[l_name]')";

63: mysql_query($add_master) or die(mysql_error());

64:

65: //get master_id for use with other tables

66: $master_id = mysql_insert_id();

67:

68: if (($_POST[address]) || ($_POST[city]) || ($_POST[state]) ||

69: ($_POST[zipcode])) {

70: //something relevant, so add to address table

71: $add_address = "insert into address values ('', $master_id,

72: now(), now(), '$_POST[address]', '$_POST[city]',

73: '$_POST[state]', '$_POST[zipcode]', '$_POST[add_type]')";

74: mysql_query($add_address) or die(mysql_error());

75: }

76:

77: if ($_POST[tel_number]) {

78: //something relevant, so add to telephone table

79: $add_tel = "insert into telephone values ('', $master_id,

80: now(), now(), '$_POST[tel_number]', '$_POST[tel_type]')";

81: mysql_query($add_tel) or die(mysql_error());


82: }

83:

84: if ($_POST[fax_number]) {

85: //something relevant, so add to fax table

86: $add_fax = "insert into fax values ('', $master_id, now(),

87: now(), '$_POST[fax_number]', '$_POST[fax_type]')";

88: mysql_query($add_fax) or die(mysql_error());

89: }

90:

91: if ($_POST[email]) {

92: //something relevant, so add to email table

93: $add_email = "insert into email values ('', $master_id,

94: now(), now(), '$_POST[email]', '$_POST[email_type]')";

95: mysql_query($add_email) or die(mysql_error());

96: }

97:

98: if ($_POST[note]) {

99: //something relevant, so add to notes table

100: $add_note = "insert into personal_notes values ('', $master_id,

101: now(), now(), '$_POST[note]')";

102: mysql_query($add_note) or die(mysql_error());

103: }

104:
105: $display_block = "<h1>Entry Added</h1>

106: <P>Your entry has been added. Would you like to

107: <a href=\"addentry.php\">add another</a>?</p>";

108: }

109: ?>

110: <HTML>

111: <HEAD>

112: <TITLE>Add an Entry</TITLE>

113: </HEAD>

114: <BODY>

115: <? print $display_block; ?>

116: </BODY>

117: </HTML>
9. Discuss about Viewing Records.

Selecting and Viewing a Record

1: <?php

2: //connect to database

3: $conn = mysql_connect("localhost", "joeuser", "somepass")

4: or die(mysql_error());

5: mysql_select_db("testDB",$conn) or die(mysql_error());

6:

7: if ($_POST[op] != "view") {

8: //haven't seen the form, so show it

9: $display_block = "<h1>Select an Entry</h1>";

10:

11: //get parts of records

12: $get_list = "select id, concat_ws(', ', l_name, f_name) as display_name

13: from master_name order by l_name, f_name";

14: $get_list_res = mysql_query($get_list) or die(mysql_error());

15:

16: if (mysql_num_rows($get_list_res) < 1) {

17: //no records

18: $display_block .= "<p><em>Sorry, no records to select!</em></p>";

19:

20: } else {

21: //has records, so get results and print in a form


22: $display_block .= "

23: <form method=\"post\" action=\"$_SERVER[PHP_SELF]\">

24: <P><strong>Select a Record to View:</strong><br>

25: <select name=\"sel_id\">

26: <option value=\"\">? Select One ?</option>";

27:

28: while ($recs = mysql_fetch_array($get_list_res)) {

29: $id = $recs['id'];

30: $display_name = stripslashes($recs['display_name']);

31:

32: $display_block .= "<option value=\"$id\">

33: $display_name</option>";

34: }

35: $display_block .= "

36: </select>

37: <input type=\"hidden\" name=\"op\" value=\"view\">

38: <p><input type=\"submit\" name=\"submit\"

39: value=\"View Selected Entry\"></p>

40: </FORM>";

41: }

42:

43: } else if ($_POST[op] == "view") {

44:
45: //check for required fields

46: if ($_POST[sel_id] == "") {

47: header("Location: selentry.php");

48: exit;

49: }

50:

51: //get master_info

52: $get_master = "select concat_ws(' ', f_name, l_name) as display_name

53: from master_name where id = $_POST[sel_id]";

54: $get_master_res = mysql_query($get_master);

55: $display_name = stripslashes(mysql_result($get_master_res,

56: 0,'display_name'));

57: $display_block = "<h1>Showing Record for $display_name</h1>";

58: //get all addresses

59: $get_addresses = "select address, city, state, zipcode, type

60: from address where master_id = $_POST[sel_id]";

61: $get_addresses_res = mysql_query($get_addresses);

62:

63: if (mysql_num_rows($get_addresses_res) > 0) {

64:

65: $display_block .= "<P><strong>Addresses:</strong><br>

66: <ul>";

67:
68: while ($add_info = mysql_fetch_array($get_addresses_res)) {

69: $address = $add_info[address];

70: $city = $add_info[city];

71: $state = $add_info[state];

72: $zipcode = $add_info[zipcode];

73: $address_type = $add_info[type];

74:

75: $display_block .= "<li>$address $city $state $zipcode

76: ($address_type)";

77: }

78:

79: $display_block .= "</ul>";

80: }

81:

82: //get all tel

83: $get_tel = "select tel_number, type from telephone where

84: master_id = $_POST[sel_id]";

85: $get_tel_res = mysql_query($get_tel);

86:

87: if (mysql_num_rows($get_tel_res) > 0) {

88:

89: $display_block .= "<P><strong>Telephone:</strong><br>

90: <ul>";
91:

92: while ($tel_info = mysql_fetch_array($get_tel_res)) {

93: $tel_number = $tel_info[tel_number];

94: $tel_type = $tel_info[type];

95:

96: $display_block .= "<li>$tel_number ($tel_type)";

97: }

98:

99: $display_block .= "</ul>";

100: }

101:

102: //get all fax

103: $get_fax = "select fax_number, type from fax where

104: master_id = $_POST[sel_id]";

105: $get_fax_res = mysql_query($get_fax);

106:

107: if (mysql_num_rows($get_fax_res) > 0) {

108:

109: $display_block .= "<P><strong>Fax:</strong><br>

110: <ul>";

111:

112: while ($fax_info = mysql_fetch_array($get_fax_res)){

113: $fax_number = $fax_info[fax_number];


114: $fax_type = $fax_info[type];

115:

116: $display_block .= "<li>$fax_number ($fax_type)";

117: }

118:

119: $display_block .= "</ul>";

120: }

121:

122: //get all email

123: $get_email = "select email, type from email where

124: master_id = $_POST[sel_id]";

125: $get_email_res = mysql_query($get_email);

126:

127: if (mysql_num_rows($get_email_res) > 0) {

128:

129: $display_block .= "<P><strong>Email:</strong><br>

130: <ul>";

131:

132: while ($email_info = mysql_fetch_array($get_email_res)) {

133: $email = $email_info[email];

134: $email_type = $email_info[type];

135:

136: $display_block .= "<li>$email ($email_type)";


137: }

138:

139: $display_block .= "</ul>";

140: }

141:

142: //get personal note

143: $get_notes = "select note from personal_notes where

144: master_id = $_POST[sel_id]";

145: $get_notes_res = mysql_query($get_notes);

146:

147: if (mysql_num_rows($get_notes_res) == 1) {

148: $note = nl2br(stripslashes(mysql_result($get_notes_res,0,'note')));

149:

150: $display_block .= "<P><strong>Personal Notes:</strong><br>$note";

151: }

152:

153: $display_block .= "<br><br><P align=center>

154: <a href=\"$_SERVER[PHP_SELF]\">select another</a></p>";

155: }

156: ?>

157: <HTML>

158: <HEAD>

159: <TITLE>My Records</TITLE>


160: </HEAD>

161: <BODY>

162: <? print $display_block; ?>

163: </BODY>

164: </HTML>

10. Discuss about Creating the Record Deletion Mechanism.

Selecting and Deleting a Record

//check for required fields

46: if ($_POST[sel_id] == "") {

47: header("Location: delentry.php");

48: exit;

49: }
50:

51: //issue queries

52: $del_master = "delete from master_name where id = $_POST[sel_id]";

53: mysql_query($del_master);

54:

55: $del_address = "delete from address where id = $_POST[sel_id]";

56: mysql_query($del_address);

57:

58: $del_tel = "delete from telephone where id = $_POST[sel_id]";

59: mysql_query($del_tel);

60:

61: $del_fax = "delete from fax where id = $_POST[sel_id]";

62: mysql_query($del_fax);

63:

64: $del_email = "delete from email where id = $_POST[sel_id]";

65: mysql_query($del_email);

66:

67: $del_note = "delete from personal_notes where id = $_POST[sel_id]";

68: mysql_query($del_master);

69:

70: $display_block = "<h1>Record(s) Deleted</h1>

71: <P>Would you like to

72: <a href=\"$_SERVER[PHP_SELF]\">delete another</a>?</p>";


73: }

74: ?>

75: <HTML>

76: <HEAD>

77: <TITLE>My Records</TITLE>

78: </HEAD>

79: <BODY>

80: <? print $display_block; ?>

81: </BODY>

82: </HTML>

11. Discuss about Adding Sub – entities to a Record.

1: <?php

2: if (($_POST[op] != "add") || ($_GET[master_id] != "")) {

3: //haven't seen the form, so show it


4: $display_block = "

5: <h1>Add an Entry</h1>

6: <form method=\"post\" action=\"$_SERVER[PHP_SELF]\">";

7:

8: if ($_GET[master_id] != "") {

9: //connect to database

10: $conn = mysql_connect("localhost", "joeuser", "somepass")

11: or die(mysql_error());

12: mysql_select_db("testDB",$conn) or die(mysql_error());

13:

14: //get first, last names for display/tests validity

15: $get_names = "select concat_ws(' ', f_name, l_name) as

16: display_name from master_name where id = $_GET[master_id]";

17: $get_names_res = mysql_query($get_names) or die(mysql_error());

18:

19: if (mysql_num_rows($get_names_res) == 1) {

20: $display_name = mysql_result($get_names_res,0,'display_name');

21: }

22: }

23:

24: if ($display_name != "") {

25: $display_block .= "<P>Adding information for

26: <strong>$display_name</strong>:</p>";
27: } else {

28: $display_block .= "

29: <P><strong>First/Last Names:</strong><br>

30: <input type=\"text\" name=\"f_name\" size=30 maxlength=75>

31: <input type=\"text\" name=\"l_name\" size=30 maxlength=75>";

32: }

33: $display_block .= "<P><strong>Address:</strong><br>

34: <input type=\"text\" name=\"address\" size=30>

35:

36: <P><strong>City/State/Zip:</strong><br>

37: <input type=\"text\" name=\"city\" size=30 maxlength=50>

38: <input type=\"text\" name=\"state\" size=5 maxlength=2>

39: <input type=\"text\" name=\"zipcode\" size=10 maxlength=10>

40:

41: <P><strong>Address Type:</strong><br>

42: <input type=\"radio\" name=\"add_type\" value=\"home\" checked>


home

43: <input type=\"radio\" name=\"add_type\" value=\"work\"> work

44: <input type=\"radio\" name=\"add_type\" value=\"other\"> other

45:

46: <P><strong>Telephone Number:</strong><br>

47: <input type=\"text\" name=\"tel_number\" size=30 maxlength=25>

48: <input type=\"radio\" name=\"tel_type\" value=\"home\" checked> home

49: <input type=\"radio\" name=\"tel_type\" value=\"work\"> work


50: <input type=\"radio\" name=\"tel_type\" value=\"other\"> other

51:

52: <P><strong>Fax Number:</strong><br>

53: <input type=\"text\" name=\"fax_number\" size=30 maxlength=25>

54: <input type=\"radio\" name=\"fax_type\" value=\"home\" checked> home

55: <input type=\"radio\" name=\"fax_type\" value=\"work\"> work

56: <input type=\"radio\" name=\"fax_type\" value=\"other\"> other

57:

58: <P><strong>Email Address:</strong><br>

59: <input type=\"text\" name=\"email\" size=30 maxlength=150>

60: <input type=\"radio\" name=\"email_type\" value=\"home\" checked>


home

61: <input type=\"radio\" name=\"email_type\" value=\"work\"> work

62: <input type=\"radio\" name=\"email_type\" value=\"other\"> other

63:

64: <P><strong>Personal Note:</strong><br>

65: <textarea name=\"note\" cols=35 rows=5 wrap=virtual></textarea>

66: <input type=\"hidden\" name=\"op\" value=\"add\">

67: <input type=\"hidden\" name=\"master_id\" value=\"$_GET[master_id]\">

68:

69: <p><input type=\"submit\" name=\"submit\" value=\"Add Entry\"></p>

70: </FORM>";

71:

72: } else if ($_POST[op] == "add") {


73: //time to add to tables, so check for required fields

74: if ((($_POST[f_name] == "") || ($_POST[l_name] == "")) &&

75: ($_POST[master_id] == "")) {

76: header("Location: addentry.php");

77: exit;

78: }

79:

80: //connect to database

81: $conn = mysql_connect("localhost", "joeuser", "somepass")

82: or die(mysql_error());

83: mysql_select_db("testDB",$conn) or die(mysql_error());

84:

85: if ($_POST[master_id] == "") {

86: //add to master_name table

87: $add_master = "insert into master_name values ('', now(),

88: now(), '$_POST[f_name]', '$_POST[l_name]')";

89: mysql_query($add_master) or die(mysql_error());

90: //get master_id for use with other tables

91: $master_id = mysql_insert_id();

92: } else {

93: $master_id = $_POST[master_id];

94: }

95:
96: if (($_POST[address]) || ($_POST[city]) || ($_POST[state]) ||

97: ($_POST[zipcode])) {

98: //something relevant, so add to address table

99: $add_address = "insert into address values ('', $master_id,

100: now(), now(), '$_POST[address]', '$_POST[city]',

101: '$_POST[state]', '$_POST[zipcode]', '$_POST[add_type]')";

102: mysql_query($add_address) or die(mysql_error());

103: }

104:

105: if ($_POST[tel_number]) {

106: //something relevant, so add to telephone table

107: $add_tel = "insert into telephone values ('', $master_id,

108: now(), now(), '$_POST[tel_number]', '$_POST[tel_type]')";

109: mysql_query($add_tel) or die(mysql_error());

110: }

111:

112: if ($_POST[fax_number]) {

113: //something relevant, so add to fax table

114: $add_fax = "insert into fax values ('', $master_id, now(),

115: now(), '$_POST[fax_number]', '$_POST[fax_type]')";

116: mysql_query($add_fax) or die(mysql_error());

117: }

118:
119: if ($_POST[email]) {

120: //something relevant, so add to email table

121: $add_email = "insert into email values ('', $master_id,

122: now(), now(), '$_POST[email]', '$_POST[email_type]')";

123: mysql_query($add_email) or die(mysql_error());

124: }

125:

126: if ($_POST[note]) {

127: //something relevant, so add to notes table

128: $add_note = "replace into personal_notes values ('', $master_id,

129: now(), now(), '$_POST[note]')";

130: mysql_query($add_note) or die(mysql_error());

131: }

132:

133: $display_block = "<h1>Entry Added</h1>

134: <P>Your entry has been added. Would you like to

135: <a href=\"addentry.php\">add another</a>?</p>";

136: }

137: ?>

138: <HTML>

139: <HEAD>

140: <TITLE>Add an Entry</TITLE>

141: </HEAD>
142: <BODY>

143: <? print

$display_block; ?> 144:

</BODY>

145: </HTML>

You might also like