Friday, September 11, 2015

កូដ PHP កែសម្រួលទិន្នន័យ

                                    

1. ចូលទៅ http://127.0.0.1/phpMyAdmin/


2.  បង្កើតមូលដ្ឋានទិន្នន័យមួយឈ្មោះ work


3. បង្កើតតារាងមួយឈ្មោះ  employee  ដែលមានចំនួន 4 ជួរឈរ


4. បំពេញទិន្នន័យ ក្នុងតារាង employee ដូចរូបខាងក្រោម


5. បញ្ចូលទិន្នន័យក្នុងតារាង


6. បន្ទាប់ពី បង្កើតមូលដ្ឋានទិន្នន័យរួចហើយ ឥឡូវចាប់ផ្តើមសរសេរកូដ

     ចូលទៅ C/Apperv/www  ហើយបង្កើត folder មួយឈ្មោះ Edit 


7. សរសេរកូដ config.php  ដើម្បីតភ្ជាប់ទៅមូលដ្ឋានទិន្នន័យ

<?php

/* Database Connection */

$sDbHost = 'localhost';  
$sDbName = 'work'; 
$sDbUser = 'root';  
$sDbPwd = '1234567';  

$dbConn = mysql_connect ($sDbHost, $sDbUser, $sDbPwd) or die ('MySQL connect failed. ' . mysql_error()); 
mysql_select_db($sDbName,$dbConn) or die('Cannot select database. ' . mysql_error()); 

?>

អនុវត្តតាម កូដខាងលើ ហើយ save ឈ្មោះថា config.php នៅក្នុង folder Edit  ។

8. សរសេរកូដ view.php ដើម្បីបង្ហាញទិន្នន័យដែលអ្នកចង់បង្ហាញ

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>

<body>
<?php

include('config.php');

$result = mysql_query("SELECT * FROM employee")  
or die(mysql_error());

echo "<table border='1' cellpadding='10'>";
echo "<tr>
<th><font color='Red'>Id</font></th>
<th><font color='Red'>Name</font></th>
<th><font color='Red'>Address</font></th>

<th><font color='Red'>Edit</font></th>

</tr>";

while($row = mysql_fetch_array( $result ))  
{

echo "<tr>";
echo '<td><b><font color="#663300">' . $row['id'] . '</font></b></td>';
echo '<td><b><font color="#663300">' . $row['name'] . '</font></b></td>';
echo '<td><b><font color="#663300">' . $row['address'] . '</font></b></td>';

echo '<td><b><font color="#663300"><a href="edit.php?id=' . $row['id'] . '">Edit</a></font></b></td>';

echo "</tr>";

}

echo "</table>"; 
?>

</body>
</html>

អនុវត្តតាម កូដខាងលើ ហើយ save ឈ្មោះថា view.php នៅក្នុង folder Edit  ។

9. សរសេរកូដ edit.php ដើម្បីកែសម្រួលទិន្នន័យដែលអ្នកចង់កែសម្រួល

<?php
function valid($id, $name, $address,$city, $error) 
{
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 

<html> 
<head>
<title>Edit Records</title>   
</head>
<body>
<?php



if ($error != '')  
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>


<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>"/>  

<table border="1">
<tr>
<td colspan="2"><b><font color='Red'>Edit Records </font></b></td>  
</tr>
<tr>
<td width="179"><b><font color='#663300'>Name<em>*</em></font></b></td> 
<td><label>
<input type="text" name="name" value="<?php echo $name; ?>" /> 
</label></td>
</tr>

<tr>
<td width="179"><b><font color='#663300'>Address<em>*</em></font></b></td>
<td><label>
<input type="text" name="address" value="<?php echo $address; ?>" />
</label></td>
</tr>

<tr>
<td width="179"><b><font color='#663300'>City<em>*</em></font></b></td>
<td><label>
<input type="text" name="city" value="<?php echo $city; ?>" />
</label></td>
</tr>

<tr align="Right">
<td colspan="2"><label>
<input type="submit" name="submit" value="Edit Records">
</label></td>
</tr>
</table>
</form>
</body>
</html>
<?php
}

include('config.php');

if (isset($_POST['submit'])) 
{

if (is_numeric($_POST['id']))
{

$id = $_POST['id'];
$name = mysql_real_escape_string(htmlspecialchars($_POST['name']));  
$address = mysql_real_escape_string(htmlspecialchars($_POST['address']));  
$city = mysql_real_escape_string(htmlspecialchars($_POST['city']));  


if ($name == '' || $address == '' || $city == '') 
{

$error = 'ERROR: Please fill in all required fields!';  


valid($id, $name, $address,$city, $error); 
}
else
{


mysql_query("UPDATE employee SET name='$name', address='$address' ,city='$city' WHERE id='$id'")  
or die(mysql_error());

header("Location: view.php"); 
}
}
else
{

echo 'Error!';
}
}
else

{

if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0) 
{

$id = $_GET['id'];
$result = mysql_query("SELECT * FROM employee WHERE id=$id")   
or die(mysql_error());
$row = mysql_fetch_array($result);  

if($row)  
{

$name = $row['name'];
$address = $row['address'];
$city = $row['city'];

valid($id, $name, $address,$city,'');
}
else  
{
echo "No results!"; 
}
}
else

{
echo 'Error!'; 
}
}
?>

អនុវត្តតាម កូដខាងលើ ហើយ save ឈ្មោះថា edit.php នៅក្នុង folder Edit  ។

10. ចូលទៅ   web browser ហើយសរសេរ URL : http://127.0.0.1/Edit/view.php

11. សូមចុចលើពាក្យ Edit ដើម្បី កែសម្រួលទិន្នន័យ 


12. ប្រព័ន្ឋនឹងបង្ហាញតារាង និង ទិន្នន័យ ឱ្យអ្នកកែសម្រួល


13. កែសម្រួលទិន្នន័យរបស់អ្នក ហើយចុចលើ Edit Records


14. បន្ទាប់មកទិន្នន័យរបស់អ្នក  ត្រូវបានកែសម្រួលថ្មីក្នុងតារាង

សូមអរគុណសម្រាប់ការអានរបស់លោកអ្នក!


EmoticonEmoticon