ดาวน์โหลดซอร์สโค้ด
list.php<?php include "mysql_connect.php";?>
<html>
<head>
<title>PHP MySQL กับการบันทึกข้อมูล : SUNZANDESIGN.BLOGSPOT.COM</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="css/bootstrap.min.css">
<style>
.highlight {
background-color: #FFFF88;
}
</style>
</head>
<body>
<div class="container">
<div class="header clearfix">
<h3 class="text-muted">PHP MySQL การค้นหาข้อมูล : SUNZANDESIGN.BLOGSPOT.COM </h3>
</div>
<form class="form-horizontal" method="POST" action="list.php">
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">ค้นหา : </label>
<input type="text" class="form-control" name="txt_keyword" placeholder="ค้นหา">
<input type="submit" value="ค้นหา" />
</div>
</form>
<div class="row">
<?php
$search_text = isset($_POST['txt_keyword']) ? $_POST['txt_keyword'] : '';
$data = array();
$sql = "SELECT * FROM comments
WHERE `name` LIKE '%$search_text%'
OR detail LIKE '%$search_text%'";
//echo $sql;
if ($result = $conn->query($sql)) {
//printf("Select returned %d rows.\n", $result->num_rows);
while($row = $result->fetch_array(MYSQLI_ASSOC)){
//print_r($row);echo '<br>';
$data[] = $row;
}
/* free result set */
$result->close();
}
$conn->close();
//echo '<pre>', print_r($data, true), '</pre>';
?>
<table class="table table-bordered">
<thead>
<tr>
<th>รหัส</th>
<th>ชื่อ</th>
<th>ความคิดเห็น</th>
<th>เพศ</th>
<th>การจัดการ</th>
</tr>
</thead>
<tbody>
<?php
foreach($data as $row){
if($row['sex'] == 1){
$sex = 'ชาย';
}else if($row['sex'] == 2){
$sex = 'หญิง';
}else{
$sex = 'ไม่ระบุ';
}
?>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['detail'];?></td>
<td><?php echo $sex;?></td>
<td>
<a href='form.php?id=<?php echo $row['id'];?>'>แก้ไข</a> | ลบ
</td>
</tr>
<?php }?>
</tbody>
</table>
</div>
</div><!-- container -->
<footer class="footer">
<br/><br/>
<div class="container">
<i>ติดตามความเคลื่อนไหวได้ที่ :: <a href='https://www.facebook.com/ToBeDeveloper'>https://www.facebook.com/ToBeDeveloper</a></i>
</div>
</footer>
</div> <!-- /container -->
<script src="js/jquery.min.js"></script>
<script src="js/highlight.js"></script>
<script>
$("td.detail").highlight("<?php echo $search_text;?>");
</script>
</body>
</html>
form.php
<?php include "mysql_connect.php";?>
<?php
//print_r($_GET);
$id = (int)isset($_GET['id']) ? $_GET['id'] : '';
$row = array();
if($id > 0){
$sql = "SELECT * FROM comments
WHERE `id` = $id";
echo $sql,'<br/>';
if ($stmt = $conn->query($sql)) {
$row = $result->fetch_array(MYSQLI_ASSOC);
}else{
//echo $conn->error;
}
}
if(empty($row)){
$row['name'] = '';
$row['sex'] = '';
$row['detail'] = '';
}
print_r($row);
$checked1 = '';
$checked2 = '';
if($row['sex']==1) $checked1 = 'checked';
if($row['sex']==2) $checked2 = 'checked';
?>
<html>
<head>
<title>PHP MySQL กับการบันทึกข้อมูล : SUNZANDESIGN.BLOGSPOT.COM</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="css/bootstrap.min.css">
<style>
.highlight {
background-color: #FFFF88;
}
</style>
</head>
<body>
<div class="container">
<div class="header clearfix">
<h3 class="text-muted">PHP MySQL กับการบันทึกข้อมูล : SUNZANDESIGN.BLOGSPOT.COM </h3>
</div>
<div class="row">
<form class="form-horizontal" method="POST" action="save.php">
<input type="hidden" value="<?php echo $id;?>" name="id"/>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">ชื่อ : </label>
<div class="col-sm-10">
<input type="text" class="form-control" name="txt_name" value="<?php echo $row['name'];?>" id="inputEmail3" placeholder="ระบุชื่อผู้โพสต์">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">รายละเอียด : </label>
<div class="col-sm-10">
<textarea name="txt_detail" class="form-control" rows="3"><?php echo $row['detail'];?></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<label class="radio-inline">
<input type="radio" name="sex" id="inlineRadio1" <?php echo $checked1;?> value="1"> ชาย
</label>
<label class="radio-inline">
<input type="radio" name="sex" id="inlineRadio2" <?php echo $checked2;?> value="2"> หญิง
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-success">บันทึกข้อมูล</button>
</div>
</div>
</form>
</div>
</div>
<footer class="footer">
<div class="container">
<i>ติดตามความเคลื่อนไหวได้ที่ :: <a href='https://www.facebook.com/ToBeDeveloper'>https://www.facebook.com/ToBeDeveloper</a></i>
</div>
</footer>
</div> <!-- /container -->
<script src="js/jquery.min.js"></script>
<script src="js/highlight.js"></script>
<script>
$("td.detail").highlight("<?php echo $search_text;?>");
</script>
<?php mysqli_close($conn);?>
</body>
</html>
save.php
<?php
include 'mysql_connect.php';
$conn->select_db("dev_database");
$name = $_POST['txt_name'];
$detail = $_POST['txt_detail'];
$sex = $_POST['sex'];
$date = date('Y-m-d H:i:s');
$id = isset($_POST['id']) ? $_POST['id'] : '';
if($id!=''){
$sql = "UPDATE `comments`
SET `name`='$name'
, `detail`='$detail'
, `sex` = '$sex'
, `date_modify` = '$date'
WHERE id = $id";
}else{
$sql = "INSERT INTO `comments`
(`name`, `detail`, `sex`, `date_modify`)
VALUES
(NULL, '$name', '$detail', '$sex', '$date');";
}
echo $sql;
$query = $conn->query($sql);
if (!$query) {
printf("Error : ", $conn->error);
exit();
}
printf ("New Record has id %d.\n", $conn->insert_id);
?>
ความคิดเห็น
แสดงความคิดเห็น