0% found this document useful (0 votes)
42 views2 pages

Course Handouts: Advanced Internet Programming: Simple PHP Add, View, Edit, Update, Delete

The document provides code for a simple PHP application that allows users to add, view, edit, update, and delete records from a database table. Index.php contains a form to add new records. Add.php inserts the form data into the database. View.php retrieves and displays all records. Edit.php updates an existing record. Update.php populates the edit form. Delete.php removes a record. The code demonstrates basic CRUD operations for a PHP application with a MySQL database.

Uploaded by

Mekuriaw Mezgebu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views2 pages

Course Handouts: Advanced Internet Programming: Simple PHP Add, View, Edit, Update, Delete

The document provides code for a simple PHP application that allows users to add, view, edit, update, and delete records from a database table. Index.php contains a form to add new records. Add.php inserts the form data into the database. View.php retrieves and displays all records. Edit.php updates an existing record. Update.php populates the edit form. Delete.php removes a record. The code demonstrates basic CRUD operations for a PHP application with a MySQL database.

Uploaded by

Mekuriaw Mezgebu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Course Handouts: Advanced Internet Programming By: Engr. Julius P.

Esclamado, MIT

SIMPLE PHP ADD, VIEW, EDIT, UPDATE, DELETE

INDEX.PHP 

<html>
<body>
<div style="border:1px solid;"> 

<form action="add.php" method="post">


Fullname <input type="text" name="name"><br>
Age <input type="text" name="age"><br>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form>    
</div>
</body>
</html>

ADD.PHP 
<?php
    mysql_connect("localhost","root","") or die (mysql_error());
    mysql_select_db("datamex") or die (mysql_error());
    echo "connected";

$name= ($_POST['name']);
$age = ($_POST['age']);

mysql_query("insert users set name='".$name."', age='".$age."'");


echo "<script>alert('Record successfuly saved.');window.location.href='paragview.php';</script>";
?>

VIEW.PHP
<?php
$connect = mysql_connect("localhost","root","") or die("Cannot connect to server");
mysql_select_db("datamex",$connect) or die("Cannot connect to the database");

$query = mysql_query("select * from users order by id asc");


?>
<table border="1">
<tr>
    <td>Name</td>
    <td>Age</td>
    <td>Action</td>
</tr>
<?php

if(mysql_num_rows($query)>0){ 
    while($row= mysql_fetch_array($query)){ ?>
    <tr>
        <td><?=$row['name']?></td>
        <td><?=$row['age']?></td>
        <td><a href="update.php?id=<?=$row['id']?>">[Edit]</a>&nbsp;<a href="delete.php?id=<?=$row['id']?>">[Delete]</a></td>
    </tr>
<?php        
        
    }
        
}
else{

1
Course Handouts: Advanced Internet Programming By: Engr. Julius P. Esclamado, MIT

    
    echo "no record";
}

?>
<tr><td colspan="3"><a href="index.php" >[Add new record]</a>
</td></tr>
</table>

EDIT.PHP 

<?php
$connect = mysql_connect("localhost","root","") or die("Cannot connect to server");
mysql_select_db("datamex",$connect) or die("Cannot connect to the database");

$name= ($_POST['name'])?$_POST['name']:'';
$age= ($_POST['age'])?$_POST['age']:'';
$id    = ($_POST['id'])?$_POST['id']:'';

if($name=="" || $age==""){
    echo "<script>alert('Complete all field');history.back();</script>";
    
}else{
    mysql_query("update users set name='".$name."', age='".$age."' where id='".$id."'");
    echo "<script>alert('Record successfuly updated.');window.location.href='view.php';</script>";
}
?>

UPDATE.PHP 

<?php
$connect = mysql_connect("localhost","root","") or die("Cannot connect to server");
mysql_select_db("datamex",$connect) or die("Cannot connect to the database");

$query = mysql_query("select * from users where id='".$_GET['id']."'");


$row = mysql_fetch_array($query);
?>
<html>
<body>
<div style="border:1px solid;"> 

<form action="edit.php" method="post">


<input type='hidden' name="id" value="<?=($_GET['id'])?$_GET['id']:''?>">
Fullname <input type="text" name="name" value="<?=$row['name']?>"><br>
Age <input type="text" name="age" value="<?=$row['age']?>"><br>
<input type="submit" value="Update">
<input type="reset" value="Reset">
</form>    
<a href="index.php" >[Back to home]</a>
</div>
</body>
</html>

DELETE.PHP
<?php
$connect = mysql_connect("localhost","root","") or die("Cannot connect to server");
mysql_select_db("datamex",$connect) or die("Cannot connect to the database");
$id    = $_GET['id'];
    mysql_query("delete from users where id='".$id."'");
    echo "<script>alert('Record successfuly updated.');window.location.href='view.php';</script>";
?>

You might also like