CRUD – PHP + MySQL

BY IN CS - Database Comments Off on CRUD – PHP + MySQL ,

For learning Option Database, we will try to learn to develop a Web Application. For this purpose, we will use PHP for side server script, JavaScript for client side script and MySQL for the database

There are lots of PHP IDE, you can even use a notepad++ for the IDE or other software such as Dreamweaver, NuSphere etc.

PHP is a scripting language that can both embedded in HTML or writing an HTML code

There are lots of PHP Tutorial over the internet that you can simply learn from it. In this article, I will show you the basic CRUD for your database. Make sure you have created MySQL Database and at least one table. Take a look on the following SQL Query:

CREATE TABLE IF NOT EXISTS `siswa` (
`no_induk` varchar(5) NOT NULL,
`password` varchar(10) NOT NULL,
`nama_siswa` varchar(50) NOT NULL,
`alamat` varchar(100) NOT NULL,
`gender` varchar(10) NOT NULL,
`telepon` varchar(20) NOT NULL,
`tanggal_lahir` date NOT NULL
)

Here’s the way to connect to the database:

<?php

$host=’localhost’;
$user=’root’;
$passw=”;

$db=’tes’;

@mysql_connect($host,$user,$passw);
@mysql_select_db($db);

?>

Here’s the way to display the records:

<?php

require_once “koneksi.php”;

$sql=”SELECT * FROM salary”;
$result=mysql_query($sql);

echo “
<table border=1>
<tr>
<td>Staff No.</td>
<td>Name</td>
<td>Base Salary</td>
<td>Working Day</td>
<td>Salary per day</td>
<td>Tax</td>
<td>Total</td>
<td>&nbsp;</td>
</tr>”;

while ($data=mysql_fetch_array($result))
{
?>

<tr>
<td><?php echo $data[‘staffnumber’] ?></td>
<td><?php echo $data[‘staffname’] ?></td>
<td align=right><?php echo $data[‘basesalary’] ?></td>
<td align=right><?php echo $data[‘workingdays’] ?></td>
<td align=right><?php echo $data[‘salaryperday’] ?></td>
<td align=right><?php echo $data[‘tax’] ?></td>
<td align=right><?php echo $data[‘totalsalary’] ?></td>
<td>
<a href=”edit.php?s=<?php echo $data[‘staffnumber’] ?>”>Edit</a>
<a href=”delete.php?s=<?php echo $data[‘staffnumber’] ?>”>Delete</a>
</td>
</tr>

<?php
}
?>

</table>

And the following can be used to edit the record

<?php

require_once “koneksi.php”;

if (isset($_POST[‘submit’]))
{
$sql=”UPDATE salary SET staffname=’”.$_POST[‘staffname’].”‘,basesalary=”.$_POST[‘basesalary’].”,workingdays=”.$_POST[‘workingdays’].”,salaryperday=”.$_POST[‘salaryperday’].”,tax=”.$_POST[‘tax’].”,totalsalary=”.$_POST[‘totalsalary’].” WHERE staffnumber=’”.$_POST[‘staffnumber’].”‘”;
$result=mysql_query($sql);
//echo $sql;
echo “Data berhasil diupdate”;
}
else
{

$sql=”SELECT * FROM salary WHERE staffnumber=’”.$_GET[‘s’].”‘”;
$result=mysql_query($sql);

echo “
<form method=POST action=edit.php>
<table border=1>
<tr>
<td>Staff No.</td>
<td>Name</td>
<td>Base Salary</td>
<td>Working Day</td>
<td>Salary per day</td>
<td>Tax</td>
<td>Total</td>
</tr>”;

$data=mysql_fetch_array($result);

?>

<tr>
<td><input type=”text” size=”5″ name=”staffnumber” value=”<?php echo $data[‘staffnumber’] ?>” readonly></td>
<td><input type=”text” name=”staffname” value=”<?php echo $data[‘staffname’] ?>” ></td>
<td align=right><input type=”text” name=”basesalary” size=”5″ value=”<?php echo $data[‘basesalary’] ?>” ></td>
<td align=right><input type=”text” name=”workingdays” size=”5″ value=”<?php echo $data[‘workingdays’] ?>” ></td>
<td align=right><input type=”text” name=”salaryperday” size=”5″ value=”<?php echo $data[‘salaryperday’] ?>” ></td>
<td align=right><input type=”text” name=”tax” size=”5″ value=”<?php echo $data[‘tax’] ?>” ></td>
<td align=right><input type=”text” name=”totalsalary” size=”5″ value=”<?php echo $data[‘totalsalary’] ?>” ></td>
</tr>
<tr>
<td colspan=”7″>
<input type=”submit” name=”submit” value=”Submit”>
</td>
</tr>
</table>
</form>

<?php
}
?>




Comments are closed.