Home
Posts filed under ភាសា PHP
Showing posts with label ភាសា PHP. Show all posts
Showing posts with label ភាសា PHP. Show all posts
Thursday, September 24, 2015
Friday, September 11, 2015
Thursday, September 10, 2015
កូដ PHP បញ្ចូលទិន្នន័យថ្មី
1. ចូលទៅ http://127.0.0.1/phpMyAdmin/
2. បង្កើតមូលដ្ឋានទិន្នន័យមួយឈ្មោះ work
3. បង្កើតតារាងមួយឈ្មោះ employee ដែលមានចំនួន 4 ជួរឈរ
4. បំពេញទិន្នន័យ ក្នុងតារាង employee ដូចរូបខាងក្រោម
5. បញ្ចូលទិន្នន័យក្នុងតារាង
6. បន្ទាប់ពី បង្កើតមូលដ្ឋានទិន្នន័យរួចហើយ ឥឡូវចាប់ផ្តើមសរសេរកូដ
ចូលទៅ C/Apperv/www ហើយបង្កើត folder មួយឈ្មោះ insert
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 insert ។
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'>City</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">' . $row['city'] . '</font></b></td>';
echo "</tr>";
}
echo "</table>";
?>
<p><a href="insert.php">add new employee</a></p>
</body>
</html>
អនុវត្តតាម កូដខាងលើ ហើយ save ឈ្មោះថា view.php នៅក្នុង folder insert ។
9. សរសេរកូដ insert.php ដើម្បីបញ្ចូលទិន្នន័យថ្មីដែលអ្នកចង់បញ្ចូល
<?php
function valid($name, $address,$city, $error)
{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Insert Records</title>
</head>
<body>
<?php
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<form action="" method="post">
<table border="1">
<tr>
<td colspan="2"><b><font color='Red'>Insert 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="Insert Records">
</label></td>
</tr>
</table>
</form>
</body>
</html>
<?php
}
include('config.php');
if (isset($_POST['submit']))
{
$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 = 'Please enter the details!';
valid($name, $address, $city,$error);
}
else
{
mysql_query("INSERT employee SET name='$name', address='$address', city='$city'")
or die(mysql_error());
header("Location: view.php");
}
}
else
{
valid('','','','');
}
?>
អនុវត្តតាម កូដខាងលើ ហើយ save ឈ្មោះថា insert.php នៅក្នុង folder insert ។
10. ចូលទៅ web browser ហើយសរសេរ URL : http://127.0.0.1/insert/view.php
11. សូមចុចលើ add new employee
12. ប្រព័ន្ឋនឹងបង្ហាញតារាងឱ្យអ្នក បញ្ចូលទិន្នន័យថ្មី
13. សូមបំពេញទិន្នន័យរបស់អ្នក ហើយចុចលើ Insert Records
14. បន្ទាប់មកទិន្នន័យរបស់អ្នក ត្រូវបញ្ចូលក្នុងតារាង
សូមអរគុណសម្រាប់ការអានរបស់លោកអ្នក!
Tuesday, September 1, 2015
Subscribe to:
Posts (Atom)
