Activity 1: Insert and Retrieve Data STEP 1: Create A Database As Shown

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 3

ACTIVITY 1: INSERT AND RETRIEVE DATA

STEP 1: Create a database as shown.


Database Name: student
Table Name: stud_info
Field
STUDNO
LNAME
FNAME
COURSE

Data Type
INT
VARCHAR
VARCHAR
VARCHAR

Size
10
30
30
10

STEP 2: Create an input interface.


Save as index.php
<html><head>
<title>INSERTING DATA</title></head>
<body bgcolor='#00FFFF'><font face=arial>
<h2>STUDENT INFORMATION</h2>
<?php
if(!isset($_POST['submit'])){ ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>"
method=POST>
<table border=1 width=30%>
<tr>
<td>ID NO.:
<td><input type=text name=studno size=15 maxlength=10>
<tr>
<td>LASTNAME:
<td><input type=text name=lname size=25>
<tr>
<td>FIRSTNAME:
<td><input type=text name=fname size=25>
<tr>
<td>COURSE:
<td><select name=course>
<option value='BSCS'>BSCS</option>
<option value='BSCOE'>BSCOE</option>
<option value='BSIT'>BSIT</option>
</select>
<tr>
<td>ADDRESS:
<td><input type=text name=address>
<tr>
<td>HOBBIES:
<td><input type=text name=hobbies>
<tr>
<td><input type=submit name=submit value='SAVE'>
<td><input type=reset value=RESET>
</table></form>
<?php } else{
$studno=$_POST['studno'];
$lname=$_POST['lname'];
$fname=$_POST['fname'];
$course=$_POST['course'];
$address=$_POST['address'];
$hobbies=$_POST['hobbies'];
$con=mysql_connect('localhost','root','') or die("Unable to connect!");
mysql_select_db('information') or die("Unable to select database!");
IT327 SCN: PHP & MySQL

Page 1 of 2

$query="INSERT INTO student_info(STUDNO,LNAME,FNAME,COURSE,ADDRESS,HOBBIES)


VALUES('$studno','$lname','$fname','$course','$address','$hobbies')";
mysql_query($query) or die("Unable to perform query".mysql_error());
mysql_close($con);
echo "<h4>|<a href='index.php'>BACK TO MAIN</a>||<a href='view.php'>VIEW RECORD</a>|";
?>
<script>
alert("Successfully Added!");
</script>
<?php
}
?>
</body>
</html>

STEP 3: Create a PHP file to view all records

Save as view.php
<html>
<head><title>RETRIEVING DATA</title></head>
<body bgcolor='#00FFFF'>
<?php
$con=mysql_connect('localhost','root','') or die('Unable to connect!');
mysql_select_db('information') or die('Unable to select database!');
$query="SELECT * FROM student_info";
$result=mysql_query($query) or die("Error in query!".mysql.error());
if(mysql_num_rows($result)>0){
echo "<table border='1' align='center'>";
echo "<caption><h2><b>RECORD SUMMARY</b></h2.</caption>";
echo "<th bgcolor='yellow'>STUDENT NUMBER <th bgcolor='yellow'>LASTNAME <th
bgcolor='yellow'>FIRSTNAME
<th bgcolor='yellow'>COURSE <th bgcolor='yellow'>ADDRESS <th bgcolor='yellow'>HOBBIES";
while($row=mysql_fetch_object($result)){
echo "<tr><td>".$row->STUDNO." <td> ".$row->LNAME." <td> ".$row->FNAME." <td> ".$row>COURSE." <td> ".$row->ADDRESS." <td> ".$row->HOBBIES;
}
}
else
echo "<br> NO RECORD FOUND";
echo "</table>";
mysql_close($con);
echo "<br><br><h3><center><a href=index.php>Go back to main</a></center></h3>";
?></body></html>

IT327 SCN: PHP & MySQL

Page 2 of 2

IT327 SCN: PHP & MySQL

Page 3 of 2

You might also like