การใช้คำสั่งในการ UPDATE ข้อมูล

ในการ UPDATE ข้อมูลเราจะทำการสร้าง link จากไฟล์ list.php ที่ได้จากการ INSERT ข้อมูลในตัวอย่างที่เเล้ว เมื่อคลิกที่ link แก้ไขข้อมูลจะต้องมีการเเสดงข้อมูลเดิมที่ได้ทำการ INSERT หลังจากนั้น เมื่อมีการแก้ไขข้อมูลใหม่ลงไปก็จะให้มีการ UPDATE ข้อมูลลงในฐานข้อมูล

 

ไฟล์ตัวอย่าง :: list.php
 <?php
require_once 'connect.php';

$sql ="SELECT * FROM phpbb_ranks";
$result = mysql_query($sql);
?>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<table border="1">
<tr>
<th>id</th>
<th>rank title</th>
<th>rank min</th>
<th>rank special</th>
<th>rank image</th>
<th>tools</th>

</tr>
<?php
while ($fetch = mysql_fetch_assoc($result)){
?>
<tr>
<td><?php echo $fetch['rank_id']?></td>
<td><?php echo $fetch['rank_title']?></td>
<td><?php echo $fetch['rank_min']?></td>
<td><?php echo $fetch['rank_special']?></td>
<td>
<?php
if(isset($fetch['rank_image'])){
if(!empty($fetch['rank_image'])){
echo '<img src="/image/'.$fetch['rank_image'].'" width="50"/><br>';
}
}
?>
</td>
<td>
<a href="/form.php?id=<?php echo $fetch['rank_id']; ?>">edit </a>
</td>
</tr>
<?php
}
?>
</table>
</body>
</html>

คำอธิบาย การสร้าง link ที่ไฟล์ list.php เพื่อส่งค่าไปยังไฟล์ save.php โดยให้ทำการ link ไปยังหน้า from.php

 

ไฟล์ตัวอย่าง :: form.php
<?php
require_once 'connect.php';

$data = array();
if(isset($_GET['id'])){
$sql ="SELECT * FROM phpbb_ranks WHERE rank_id = ".$_GET['id'];
$result = mysql_query($sql);
$data = mysql_fetch_assoc($result);
}
?>
<html>
<head>
<meta charset="UTF-8">
<title>form</title>
</head>
<body>
<h3>form ranks</h3>
<form neme="form" method="post" action="save.php" enctype="multipart/form-data">
<label>Title:</label><input type="text"name="rank_title" value="<?php echo (isset($data['rank_title']))? $data['rank_title']:''; ?>"/><br/>
<label>Min:</label><input type="text"name="rank_min"value="<?php echo (isset($data['rank_min']))? $data['rank_min']:''; ?>"/><br/>
<label>special:</label>
<?php
$chk = array();
$chk[1] = 'checked="checked';
$chk[0] = '';
if(isset($data['rank_special'])){
if($data['rank_special']==1){
$chk[1]='';
$chk[0]='checked="checked';

}
}
?>
<input type="radio"name="rank_special"id="rank_special1" value="1"checked="checked"/>
<label for="rank_special1">yes</label>
<input type="radio"name="rank_special"id="rank_special2"value="0"/>
<label for="rank_special2">no</label><br/>
<label>image:</label>
<?php
if(isset($data['rank_image'])){
if(!empty($data['rank_image'])){
echo '<img src="/image/'.$data['rank_image'].'"/><br>';
}
}

?>
<input type="file"name="rank_image"/><br/>
<input type="submit" neme="save" value="save" />
<input type="button" onClick="window.location='list.php';" value="cancel"/>
<?php
if(isset($_GET['id'])){
echo '<input type="hidden" name="rank_id" value=" '.$_GET['id'].' "/>';
}

?>

</body>
</html>

 

คำอธิบาย

    value="<?php echo (isset($data['rank_title']))? $data['rank_title']:''; ?>"

เป็นการตรวจสอบว่ามีการค่าของ rank_title ถ้ามีก็จะแสดงข้อมูล

       <?php
$chk = array();
$chk[1] = 'checked="checked';
$chk[0] = '';
if(isset($data['rank_special'])){
if($data['rank_special']==1){
$chk[1]='';
$chk[0]='checked="checked';

}
}
?>
<input type="radio"name="rank_special"id="rank_special1" value="1"checked="checked"/>
<label for="rank_special1">yes</label>
<input type="radio"name="rank_special"id="rank_special2"value="0"/>
<label for="rank_special2">no</label><br/>

เป็นการตรวจสอบว่ามีการค่าของ rank_special ว่ามีค่า เท่ากับ 0 หรือ 1

       <label>image:</label>
<?php
if(isset($data['rank_image'])){
if(!empty($data['rank_image'])){
echo '<img src="/image/'.$data['rank_image'].'"/><br>';
}
}

?>

เป็นการตรวจสอบว่า ถ้า rank_image ถ้าไม่เป็นค่าว่างก็ให้แสดงภาพ

<?php
if(isset($_GET['id'])){
echo '<input type="hidden" name="ranks_id" value= "'.$_GET['id'].'"/>';
}

?>

เป็นการส่งค่า GET ID เพื่อทำการส่งไป UPDATE

<input type="button" onclick="window.location='list.php';" value="cancel"/>

สร้างปุ่ม cancel เพื่อย้อนกลับไปยังหน้า list.php

ไฟล์ตัวอย่าง :: save.php
<?php
require_once'connect.php';
if(isset($_POST['save']));{
if(!empty($_FILES['rank_image']['name'])){
$filename = md5($_FILES['rank_image']['name'].time());
$ext = explode('.',$_FILES['rank_image']['name']);
$dest = __DIR__.DIRECTORY_SEPARATOR.'image'.DIRECTORY_SEPARATOR.$filename.'.'.$ext[1];
if(!copy($_FILES['rank_image']['tmp_name'], $dest)) {
echo 'upload Error';
exit();
}
$rank_image = $filename.'.'.$ext[1];
}

$rank_title =$_POST['rank_title'];
$rank_min =$_POST['rank_min'];
$rank_special =$_POST['rank_special'];

if(isset($_POST['rank_id'])) {
$sql = "UPDATE phpbb_ranks SET "
. " rank_title = '$rank_title', "
. " rank_min =$rank_min, "
. " rank_special = $rank_special,"
. " rank_image = '$rank_image' "
. " WHERE rank_id = ". $_POST['rank_id'];

}else{

$sql = "INSERT INTO phpbb_ranks (rank_title,rank_min,rank_special,rank_image)"
. "VALUES('$rank_title',$rank_min,$rank_special,'$rank_image')";
}

mysql_query($sql) or die ('save error');
header('location:list.php');
}

คำอธิบาย

 if(isset($_POST['rank_id'])) {
$sql ="UPDATE phpbb_rank SET "
. "rank_title = '$rank_title',"
. "rank_min =$rank_min,"
. "rank_special = $rank_special,"
. "rank_image = '$rank_image'"
."WHERE rank_id = ".$_POST['rank_id'];

เนื่องจาก form.php ใช้ในการ INSERT ข้อมูลด้วย เพราะฉะนั้นเราจะมีการตรวจสอบโดยเงื่อนไข การรับค่า GET ID มาหรือไม่ ถ้ามีการรับค่ามา ก็จะแสดงข้อมูลเดิมจากตาราง ถ้าไม่มี ก็จะเป็นการ INSERT เมื่อทำการ  UPDATE เสร็จก็จะทำการ กลับไปแสดงที่ list.php

ไฟล์ตัวอย่าง :: list.php
<?php
require_once'connect.php';
if(isset($_POST['save']));{
if(!empty($_FILES['rank_image']['name'])){
$filename = md5($_FILES['rank_image']['name'].time());
$ext = explode('.',$_FILES['rank_image']['name']);
$dest = __DIR__.DIRECTORY_SEPARATOR.'image'.DIRECTORY_SEPARATOR.$filename.'.'.$ext[1];
if(!copy($_FILES['rank_image']['tmp_name'], $dest)) {
echo 'upload Error';
exit();
}
$rank_image = $filename.'.'.$ext[1];
}

$rank_title =$_POST['rank_title'];
$rank_min =$_POST['rank_min'];
$rank_special =$_POST['rank_special'];

if(isset($_POST['rank_id'])) {
$sql = "UPDATE phpbb_ranks SET "
. " rank_title = '$rank_title', "
. " rank_min =$rank_min, "
. " rank_special = $rank_special,"
. " rank_image = '$rank_image' "
. " WHERE rank_id = ". $_POST['rank_id'];

}else{

$sql = "INSERT INTO phpbb_ranks (rank_title,rank_min,rank_special,rank_image)"
. "VALUES('$rank_title',$rank_min,$rank_special,'$rank_image')";
}

mysql_query($sql) or die ('save error');
header('location:list.php');
}

หัวเรื่อง
PHP & MySql : การใช้คำสั่งในการ UPDATE ข้อมูล
หมวดหมู่
PHP Basics, PHP Basics
ฮิต
50676
ผู้สร้างเอกสาร
thatsawan
วันที่สร้างเอกสาร
2014-05-14 03:04:13

update.zip

ประเภทไฟล์ zip

ขนาดไฟล์ 2.61 KB

ผู้อัพโหลดไฟล์ thatsawan

วันที่อัพโหลด 2014-05-13 20:04:10


กระทู้ล่าสุดจากเว็บบอร์ด
หัวข้อกระทู้
ตอบ
เปิดดู
ล่าสุด
สอบถามเรื่องการ select database oracle
โดย Anonymous ศ 31 ต.ค. 2014 1:34 pm บอร์ด SQL - Database
0
4636
ศ 31 ต.ค. 2014 1:34 pm โดย บุคคลทั่วไป View Topic สอบถามเรื่องการ select database oracle
SimpleScreenRecorder โปรแกรมบันทึกวีดีโอบน ubuntu
โดย jay_limm ศ 31 ต.ค. 2014 10:27 am บอร์ด Microsoft Office Knowledge & line & Etc
0
774
ศ 31 ต.ค. 2014 10:27 am โดย jay_limm View Topic SimpleScreenRecorder โปรแกรมบันทึกวีดีโอบน ubuntu
Official phpBB Extension เริ่มทะยอยออกมาแล้ว
โดย mindphp ศ 31 ต.ค. 2014 7:02 am บอร์ด MindPHP News & Feedback
1
1051
จ 02 มี.ค. 2015 5:17 pm โดย poundsilly View Topic Official phpBB Extension เริ่มทะยอยออกมาแล้ว
กำลังศึกษา เรื่อง Unit test phpbb ค่ะ เเต่ ไม่เข้าใจเลยช่วยเเนะนำหน่อยจิ
โดย thatsawan พฤ 30 ต.ค. 2014 11:21 pm บอร์ด Programming - PHP
0
727
พฤ 30 ต.ค. 2014 11:21 pm โดย thatsawan View Topic กำลังศึกษา เรื่อง Unit test phpbb ค่ะ เเต่ ไม่เข้าใจเลยช่วยเเนะนำหน่อยจิ
แก้ปัญหา virtuemart joomla 2.5 ขยายตัวเพิ่มจำนวนยังไง
โดย Anonymous พฤ 30 ต.ค. 2014 6:52 pm บอร์ด สอบถามปัญหาการใช้ phpBB3, SMF, Joomla, Wordpress, CMS, CRM
3
4671
ศ 31 ต.ค. 2014 3:48 am โดย mindphp View Topic แก้ปัญหา virtuemart joomla 2.5 ขยายตัวเพิ่มจำนวนยังไง
ความสนุก ของการเกิดเป็น สาวตัวสั้น ฯลฯ ประการ
โดย mindphp พฤ 30 ต.ค. 2014 9:54 pm บอร์ด พูดคุยเรื่องทั่วไป จับฉ่าย
2
1050
ศ 31 ต.ค. 2014 12:39 am โดย rinrada View Topic ความสนุก ของการเกิดเป็น สาวตัวสั้น ฯลฯ ประการ
เจอปัญหา XML Parsing Error: XML or text declaration not at start of entity
โดย mindphp พฤ 30 ต.ค. 2014 9:33 pm บอร์ด HTML CSS
0
1220
พฤ 30 ต.ค. 2014 9:33 pm โดย mindphp View Topic เจอปัญหา XML Parsing Error: XML or text declaration not at start of entity
จะเพิ่ม เงื่อนไขเข้าไปในตัวที่ส่งค่าให้เทมเพลต ตัวเดียวกัน T^T
โดย thatsawan พฤ 30 ต.ค. 2014 5:10 pm บอร์ด Programming - PHP
2
802
พ 05 พ.ย. 2014 6:53 pm โดย thatsawan View Topic จะเพิ่ม เงื่อนไขเข้าไปในตัวที่ส่งค่าให้เทมเพลต ตัวเดียวกัน T^T