ตัวอย่างการเขียนโปรแกรม บันทึก ลบ แก้ไข แสดงข้อมูล ค้นหา ข้อมูล จาก MySQL PHP

หมวดสำหรับแบ่งบันความ รู้ต่างๆ จะมีหมวดย่อยๆ ในหมวดนี้ เช่น php, SQL, XML, CSS

Moderator: mindphp, ผู้ดูแลกระดาน

ภาพประจำตัวสมาชิก
modtanoy_pen
PHP Hero Member
PHP Hero Member
โพสต์: 116
ลงทะเบียนเมื่อ: 19/10/2012 4:27 pm

ตัวอย่างการเขียนโปรแกรม บันทึก ลบ แก้ไข แสดงข้อมูล ค้นหา ข้อมูล จาก MySQL PHP

โพสต์ที่ยังไม่ได้อ่าน โดย modtanoy_pen »

ตัวอย่างการเขียนโปรแกรม บันทึก ลบ แก้ไข แสดงข้อมูล ค้นหา ข้อมูล จากฐานข้อมูล MySQL ด้วย PHP
สำหรับคนที่ยังไม่เคยเขียนโปรแกรมมาก่อน แนะนำให้ศึกษาจากบทเรียน เหล่านี้ก่อน สอน PHP พื้นฐาน โดยใช้เวลาเรียนรู้ ประมาณ 1 อาทิตย์
หรือ ถามชอบแบบดู VDO แนะนำที่นี่ เลือกศึกษาได้ตามถนัดและความชอบ ได้เลย


การ INSERT ข้อมูลลงฐานข้อมูลโดยรับค่าจากฟอร์ม
ขั้นตอนแรกสร้างฟอร์มรับค่าขึ้นมา 1 ฟอร์ม (from.php)

โค้ด: เลือกทั้งหมด

<?php 
session_start ();
if (isset($_SESSION['post'])){
    $post=$_SESSION['post'];
}else{
    $post=array();
}
if (isset($_SESSION['error'])){
    $error=$_SESSION['error'];
}else{
    $error=array();
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>กรอกข้อมูลสำหรับการติดต่อ</title>
<style type="text/css">
.style1 {color: #FF0000}
.style10 {color: #FF6666}
.style11 {font-size: 24px}
.style12 {color: #FF6666; font-weight: bold; }
</style>
</head>

<body>
<form id="form1" name="form1" method="post" action="insert.php">
  <p><strong><span class="style10">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style11">กรอกข้อมูลสำหรับการติดต่อ</span></span></strong></p>
  <table width="100%" border="1" align="center" bgcolor="#FFCCCC">
    <tr>
      <td width="24%">
        <div align="right">
      ชื่อร้าน/สวน :</div></td>
      <td width="76%"><input type="text" name="nameth" id="nameth" value="<?php echo isset($post['nameth']) ? $post['nameth'] : '';?>" /></td>
    </tr>
    <tr>
      <td><div align="right">ชื่อร้าน/สวน (ภาษาอังกฤษ) :</div></td>
      <td><input type="text" name="nameeg" id="nameeg" value="<?php echo isset ($post['nameeg'])? $post['nameeg'] : '';?>"/>
        <span class="style1">*<?php echo isset ($error['nameeg'])? $error['nameeg'] : '';?></span></td>
    </tr>
    <tr>
      <td><div align="right">รายละเอียดเกี่ยวกับร้านหรือสวน :</div></td>
      <td><textarea name="detail" id="detail" cols="45" rows="5"><?php echo isset ($post['detail'])? $post['detail'] : '';?></textarea> 
        <span class="style1">*<?php echo isset ($error['detail'])? $error['detail'] : '';?></span></td>
    </tr>
    <tr>
      <td><div align="right">ที่อยู่ :</div></td>
      <td><textarea name="address" id="address" cols="45" rows="5"><?php echo isset ($post['address'])? $post['address'] : '';?></textarea>
        <span class="style1">*<?php echo isset ($error['address'])? $error['address'] : '';?></span></td>
    </tr>
    <tr>
      <td><div align="right">จังหวัด :</div></td>
      <td><label>
        <input type="text" name="province" id="province" value="<?php echo isset ($post['province'])? $post['province'] : '';?>"/>
        <span class="style1">*<?php echo isset ($error['province'])? $error['province'] : '';?></span></label></td>
    </tr>
    <tr>
      <td><div align="right">โทรศัพท์ :</div></td>
      <td><input type="text" name="tel1" id="tel1" value="<?php echo isset ($post['tel1'])? $post['tel1'] : '';?>" /> 
        <span class="style1">*<?php echo isset ($error['tel1'])? $error['tel1'] : '';?></span></td>
    </tr>
    <tr>
      <td><div align="right">Fax :</div></td>
      <td><input type="text" name="fax" id="fax" value="<?php echo isset ($post['fax'])? $post['fax'] : '';?>" /></td>
    </tr>
    <tr>
      <td><div align="right">Website http://</div></td>
      <td><input type="text" name="webs" id="webs" value="<?php echo isset ($post['webs'])? $post['webs'] : '';?>" />Ex. www.yourweb.com </td>
    </tr>
    <tr>
      <td class="style12">ข้อมูลติดต่อ</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td><div align="right">ชื่อ-สกุล :</div></td>
      <td><input type="text" name="name" id="name" value="<?php echo isset ($post['name'])? $post['name'] : '';?>" />
        <span class="style1">*<?php echo isset ($error['name'])? $error['name'] : '';?></span></td>
    </tr>
    <tr>
      <td><div align="right">เพศ :</div></td>
      <td><input name="gender" type="radio" id="radio" value="ชาย" checked="checked" <?php isset($post['gender'])? "checked" :'';
       ?> />
        ชาย 
        <input type="radio" name="gender" id="radio2" value="หญิง" <?php isset($post['gender'])? "checked" :'';
       ?> />
        หญิง</td>
    </tr>
    <tr>
      <td><div align="right">E-mail :</div></td>
      <td><input type="text" name="email" id="email" value="<?php echo isset ($post['email'])? $post['email'] : '';?>" /></td>
    </tr>
    <tr>
      <td><div align="right">เบอร์โทรศัพท์ (เบอร์ภายใน) :</div></td>
      <td><input type="text" name="tel2" id="tel2" value="<?php echo isset ($post['tel2'])? $post['tel2'] : '';?>" />
        <span class="style1">*<?php echo isset ($error['tel2'])? $error['tel2'] : '';?></span></td>
    </tr>
    <tr>
      <td><div align="right">เบอร์มือถือ :</div></td>
      <td><input type="text" name="mobile" id="mobile" value="<?php echo isset ($post['mobile'])? $post['mobile'] : '';?>" /></td>
    </tr>
    <tr>
      <td colspan="2"><div align="center">
        <input type="submit" name="button" id="button" value="ตกลง" />
        <input type="reset" name="button2" id="button2" value="ยกเลิก" />
      </div></td>
    </tr>
  </table>
</form>
</body>
</html>
ขั้นตอนที่สองสร้างไฟล์การเชื่อม DATABASE SERVER และฐานข้อมูล (connect.php)

โค้ด: เลือกทั้งหมด

<?php 
$host ="localhost";//ชื่อโฮส
$user ="root";//ชื่อผู้ใช้
$pass ="";//รหัสผ่าน
$db ="pen";//ชื่อฐานข้อมูลที่เราสร้างไว้
mysql_connect ($host,$user,$pass) or die ("ติดต่อเครื่อง Database Server ไม่สำเร็จ");//ติดต่อ Database Server
mysql_select_db($db) or die ("ติดต่อฐานข้อมูลไม่สำเร็จ");//ติอต่อฐานข้อมูล
mysql_query("SET NAMES utf8")//ตั้งค่าภาษา
?>
ขั้นตอนที่สามสร้างไฟล์เพื่อบันทึกข้อมูล (insert.php)

โค้ด: เลือกทั้งหมด

<?php 
session_start() ;
$_SESSION['post']=$_POST;
$_SESSION['error']="";
$nameth=$_POST['nameth'];//ตั้งค่าตัวแปล
$nameeg=$_POST['nameeg'];
$detail=$_POST['detail'];
$address=$_POST['address'];
$province=$_POST['province'];
$tel1=$_POST['tel1'];
$fax=$_POST['fax'];
$webs=$_POST['webs'];
$name=$_POST['name'];
$gender=$_POST['gender'];
$email=$_POST['email'];
$tel2=$_POST['tel2'];
$mobile=$_POST['mobile'];
require_once("connect.php");
if (trim ($nameeg==""))//ตรวจสอบฟิลด์ที่ไม่ได้กรอกกรอกข้อมูล
{
    $_SESSION['error']['nameeg']='กรุณากรอกชื่อภาษาอังกฤษให้ถูกต้องเพื่อความสะดวกในการตรวจสอบข้อมูล';
} if(trim ($detail=="")){
    $_SESSION['error']['detail']='กรุณากรอกรายละเอียดเกี่ยวกับร้าน/สวน';
} if(trim ($address=="")){
    $_SESSION['error']['address']='กรุณากรอกที่อยู่';
} if(trim ($province=="")){
    $_SESSION['error']['province']='กรุณากรอกจังหวัด';
} if(trim ($tel1=="")){
    $_SESSION['error']['tel1']='กรุณากรอกเบอร์โทร';
} if(trim ($name=="")){
    $_SESSION['error']['name']='กรุณากรอกชื่อ';
} if(trim ($tel2=="")){
    $_SESSION['error']['tel2']='กรุณากรอกเบอร์โทรศัพท์ภายใน';
    echo "<script> window.history.go(-1);</script>\n";
    exit();
}else{
    $sql="INSERT INTO test (Id,nameth,nameeg,detail,address,province,tel1,fax,webs,name,gender,email,tel2,mobile)VALUES ('','$nameth','$nameeg','$detail','$address','$province','$tel1','$fax','$webs','$name','$gender','$email','$tel2','$mobile')";//คำสั่งเพิ่มข้อมูล
    $sql_query=mysql_query($sql);
 if($sql_query) {
        echo "<script type='text/javascript'>alert('บันทึกข้อมูลเรียบร้อยแล้ว')</script>";
        echo "<meta http-equiv ='refresh'content='0;URL=show.php'>";
        $_SESSION['post']="";
    }else{
        echo "<script type='text/javascript'>alert('เกิดข้อผิดพลาดในการบันทึกข้อมูล');window.history.go(-1);</script>" ;
    }
}
mysql_close();

?>
แก้ไขล่าสุดโดย modtanoy_pen เมื่อ 23/11/2012 1:59 pm, แก้ไขไปแล้ว 3 ครั้ง.
ภาพประจำตัวสมาชิก
modtanoy_pen
PHP Hero Member
PHP Hero Member
โพสต์: 116
ลงทะเบียนเมื่อ: 19/10/2012 4:27 pm

Re: การบันทึก ลบ แก้ไข แสดงข้อมูล ในMy SQL ด้วย PHP

โพสต์ที่ยังไม่ได้อ่าน โดย modtanoy_pen »

การแสดงข้อมูลแบบแบ่งหน้า (show.php)

โค้ด: เลือกทั้งหมด

<?php require_once("connect.php");//เรียกใช้ไฟล์การเชื่อมต่อDATABASE SERVERและฐานข้อมูล
$limit=5;//กำหนดข้อมูลที่จะแสดงในหนึ่งหน้า
if (isset($_GET['page'])){
    $page = $_GET['page'];
} else {
    $page = 0;
}
$sql = "SELECT COUNT(*) AS num_rows FROM test";//เรียกข้อมูลจากฐานข้อมูลมาใช้งานและนับข้อมูลในฐานข้อมูล
$re = mysql_query($sql);
$num_rows = mysql_result($re ,0, 'num_rows');
$sum_page = ceil($num_rows/$limit);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>แสดงข้อมูล</title>
</head>

<body>
<p>
 <?php 
    if(!$page or $page==1){
        $page=0;
    } else{ 
        $page=($page *$limit)-$limit;
    }
    $link="";
    for($i=1;$i<=$sum_page;$i++) { 
        $link.="[<a href='?page=$i'>".$i."</a>]";
    }//การส่งค่าแบบ GET
    
    echo $link="หน้า : ".$link;
    $sql = "SELECT * FROM test limit $page,$limit";
    $re = mysql_query($sql);//การเรียกใช้ข้อมูลจากฐานข้อมูลมาใช้งานโดยกำหนดค่าการเรียกใช้

?>
</p>
<table width="100%" height="50" border="0.5" align="center" >
  <tr bgcolor="#FF6699">
    
    <td width="82"><div align="center">ชื่อร้าน/สวน</div></td>
    <td width="168"><div align="center">ชื่อร้าน/สวน (ภาษาอังกฤษ)</div></td>
    <td width="210"><div align="center">รายละเอียดเกี่ยวกับร้านหรือสวน</div></td>
    <td width="42"><div align="center">ที่อย</div></td>
    <td width="45"><div align="center">จังหวัด</div></td>
    <td width="60"><div align="center">โทรศัพท์</div></td>
    <td width="25"><div align="center">Fax</div></td>
    <td width="54"><div align="center">Website</div></td>
    <td width="51"><div align="center">ชื่อ-สกุล</div></td>
    <td width="26"><div align="center">เพศ</div></td>
    <td width="47"><div align="center">E-mail</div></td>
    <td width="95"><div align="center">เบอร์โทรศัพท์</div></td>
    <td width="76"><div align="center">เบอร์มือถือ</div></td>
    <td width="91"><div align="center">แก้ไขข้อมูล</div></td>
    <td width="91"><div align="center">ลบข้อมูล</div></td>
  </tr>
 
<?php
    while($row= mysql_fetch_assoc($re))// คำสั่งให้แสดงข้อมูล
    {
        echo"<tr bgcolor='#FFCCCC'>";
        echo "<td>$row[nameth]</td>";
        echo "<td>$row[nameeg]</td>";
        echo "<td>$row[detail]</td>";
        echo "<td>$row[address]</td>";
        echo "<td>$row[province]</td>";
        echo "<td>$row[tel1]</td>";
        echo "<td>$row[fax]</td>";
        echo "<td>$row[webs]</td>";
        echo "<td>$row[name]</td>";
        echo "<td>$row[gender]</td>";
        echo "<td>$row[email]</td>";
        echo "<td>$row[tel2]</td>";
        echo "<td>$row[mobile]</td>";
        echo"<td><center><a href ='edit.php?show_id=$row[Id]'>แก้ไข </a></center></td>";//ลิงค์และส่งค่าเพื่อไปแก้ไขข้อมูล
        echo"<td><center><a href = 'delete.php?delete_id=$row[Id]' onclick=\"return confirm('คุณต้องการลบข้อมูล!!!!')\"> delete</a></center></td>";//ลิงค์และส่งค่าข้อูมูลเพื่อทำการลบข้อมูล
        echo"</tr>";
    }
mysql_close();
?>
</table>
<p>&nbsp;</p>
</body>
</html>
แก้ไขล่าสุดโดย modtanoy_pen เมื่อ 23/11/2012 2:01 pm, แก้ไขไปแล้ว 2 ครั้ง.
ภาพประจำตัวสมาชิก
modtanoy_pen
PHP Hero Member
PHP Hero Member
โพสต์: 116
ลงทะเบียนเมื่อ: 19/10/2012 4:27 pm

Re: การบันทึก ลบ แก้ไข แสดงข้อมูล ในMy SQL ด้วย PHP

โพสต์ที่ยังไม่ได้อ่าน โดย modtanoy_pen »

การแก้ไขข้อมูล (edit.php)
ขั้นตอนแรกสร้างฟอร์มเพื่อรับข้อมูลที่ส่งมาแก้ไข

โค้ด: เลือกทั้งหมด

<?php
$show_id=$_GET['show_id'];//รับค่าGETที่ส่งมาจากไฟล์โชว์
require_once("connect.php") ;//เรียกใช้ไฟล์การเชื่อมต่อDATABASE SERVERและฐานข้อมูล
$sql="SELECT*FROM test WHERE Id=$show_id";//เรียกข้อมูลจากฟอร์มโดยกำหนดเงื่อนไข
$sql_query=mysql_query($sql); 
$row=mysql_fetch_assoc($sql_query);
mysql_close(); 
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>แก้ไขข้อมูล</title>
<style type="text/css">
.style10 {color: #FF6666}
.style11 {font-size: 24px}
.style12 {color: #FF0000}
</style>
</head>

<body>
<form id="form1" name="form1" method="post" action="saveedit.php">
  <p><strong><span class="style10">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style11">แก้ไขข้อมูลสำหรับการติดต่อ</span></span></strong>
    <input type="hidden" name="Id" value="<?php echo"$row[Id]"; ?>"/>
  </p>
  <table width="80%" border="1" align="center" bgcolor="#FFCCCC">
    <tr>
      <td width="24%">
        <div align="right">
          <BODY DIR="LTR">
      ชื่อร้าน/สวน :</div></td>
      <td width="76%"><input type="text" name="nameth" id="nameth" value="<?php echo $row['nameth']; //แสดงข้อมูลที่จะแก้ไข?>" /></td>
    </tr>
    <tr>
      <td><div align="right">ชื่อร้าน/สวน (ภาษาอังกฤษ) :</div></td>
      <td><input type="text" name="nameeg" id="nameeg" value="<?php echo"$row[nameeg]"; ?>" />
        <span class="style1 style12">*</span>กรุณากรอกชื่อภาษาอังกฤษให้ถูกต้องเพื่อความสะดวกในการตรวจสอบข้อมูล</td>
    </tr>
    <tr>
      <td><div align="right">รายละเอียดเกี่ยวกับร้านหรือสวน :</div></td>
      <td><textarea name="detail" id="detail" cols="45" rows="5"><?php echo"$row[detail]"; ?></textarea> 
        <span class="style1 style12">*</span></td>
    </tr>
    <tr>
      <td><div align="right">ที่อยู่ :</div></td>
      <td><textarea name="address" id="address" cols="45" rows="5"><?php echo"$row[address]"; ?></textarea>
        <span class="style1 style12">*</span></td>
    </tr>
    <tr>
      <td><div align="right">จังหวัด :</div></td>
      <td><label>
        <input type="text" name="province" id="province" value="<?php echo"$row[province]"; ?>" />
      <span class="style1 style12">*</span></label></td>
    </tr>
    <tr>
      <td><div align="right">โทรศัพท์ :</div></td>
      <td><input type="text" name="tel1" id="tel1" value="<?php echo"$row[tel1]"; ?>" /> 
        <span class="style1 style12">*</span></td>
    </tr>
    <tr>
      <td><div align="right">Fax :</div></td>
      <td><input type="text" name="fax" id="fax" value="<?php echo"$row[fax]"; ?>" /></td>
    </tr>
    <tr>
      <td><div align="right">Website http://</div></td>
      <td><input type="text" name="webs" id="webs" value="<?php echo"$row[webs]"; ?>" />Ex. www.yourweb.com </td>
    </tr>
    <tr>
      <td class="style12">ข้อมูลติดต่อ</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td><div align="right">ชื่อ-สกุล :</div></td>
      <td><input type="text" name="name" id="name" value="<?php echo"$row[name]"; ?>" />
        <span class="style1 style12">*</span></td>
    </tr>
    <tr>
      <td><div align="right">เพศ :</div></td>
      <td><?php $rdo="";
      if (isset ($row['gender'])){$rdo=$row['gender'];}?><input type="radio" name="gender" id="radio" value="ชาย"<?php if($rdo=="ชาย")echo "checked"; ?> />
        ชาย 
        <input type="radio" name="gender" id="radio2" value="หญิง"<?php if($rdo=="หญิง")echo "checked"; ?> />
        หญิง</td>
    </tr>
    <tr>
      <td><div align="right">E-mail :</div></td>
      <td><input type="text" name="email" id="email" value="<?php echo"$row[email]"; ?>" /></td>
    </tr>
    <tr>
      <td><div align="right">เบอร์โทรศัพท์ :</div></td>
      <td><input type="text" name="tel2" id="tel2" value="<?php echo"$row[tel2]"; ?>" />
        <span class="style1 style12">*</span> เบอร์ภายใน</td>
    </tr>
    <tr>
      <td><div align="right">เบอร์มือถือ :</div></td>
      <td><input type="text" name="mobile" id="mobile" value="<?php echo"$row[mobile]"; ?>" /></td>
    </tr>
    <tr>
      <td colspan="2"><div align="center">
        <input type="submit" name="button" id="button" value="แก้ไข"  />
        <input type="reset" name="button2" id="button2" value="ยกเลิก" />
      </div></td>
    </tr>
  </table>
  </p>
</form>
</body>
</html>
ขั้นตอนที่สองเขียน code เพื่อบันทึกข้อมูลที่แก้ไข (saveedit.php)

โค้ด: เลือกทั้งหมด

<?php 
$Id=$_POST['Id'];
$nameth=$_POST['nameth'];////ตั้งค่าตัวแปลเพื่อรับค่าจากฟอร์ม
$nameeg=$_POST['nameeg'];
$detail=$_POST['detail'];
$address=$_POST['address'];
$province=$_POST['province'];
$tel1=$_POST['tel1'];
$fax=$_POST['fax'];
$webs=$_POST['webs'];
$name=$_POST['name'];
$gender=$_POST['gender'];
$email=$_POST['email'];
$tel2=$_POST['tel2'];
$mobile=$_POST['mobile'];
include ("connect.php");
    $sql="UPDATE test SET Id='$Id',nameth='$nameth',nameeg='$nameeg',detail='$detail',address='$address',province='$province',tel1='$tel1',fax='$fax',webs='$webs',name='$name',gender='$gender',email='$email',tel2='$tel2',mobile='$mobile'WHERE Id='$Id'";//คำสั่งเแก้ไขข้อมูล
    $sql_query=mysql_query($sql);
    if($sql_query) {
        echo "<script type='text/javascript'>alert('แก้ไขข้อมูลเรียบร้อยแล้ว')</script>";
        echo "<meta http-equiv ='refresh'content='0;URL=show.php'>";//เมื่อแก้ไขข้อมูลเรียบร้อยแล้วสั่งให้ลิงค์ไปตำแหน่งที่เราต้องการ
    }else{
        echo "<script type='text/javascript'>alert('ไม่สามารถแก้ไขข้อมูลได้');window.history.go(-1);</script>" ;//ถ้าไม่สามารถบันทึกข้อมูลได้ให้กลับไปที่หน้าเดิม
    }
mysql_close();
?>
แก้ไขล่าสุดโดย modtanoy_pen เมื่อ 23/11/2012 2:46 pm, แก้ไขไปแล้ว 2 ครั้ง.
ภาพประจำตัวสมาชิก
modtanoy_pen
PHP Hero Member
PHP Hero Member
โพสต์: 116
ลงทะเบียนเมื่อ: 19/10/2012 4:27 pm

Re: การบันทึก ลบ แก้ไข แสดงข้อมูล ในMy SQL ด้วย PHP

โพสต์ที่ยังไม่ได้อ่าน โดย modtanoy_pen »

การลบข้อมูล (delete.php)

โค้ด: เลือกทั้งหมด

<?php
$delete_id=$_GET['delete_id'];//รับค่าGET ที่ส่งมาจากไฟล์ show
require_once("connect.php");//เรียกใช้ไฟล์การเชื่อมต่อDATABASE SERVERและฐานข้อมูล
$sql="DELETE FROM test WHERE Id ='$delete_id'";//คำสั่งลบข้อมูล
$result = mysql_query($sql);
if($result){
    echo"<script type='text/javascript'>alert('ลบข้อมูลสำเร็จ')</script>";//javascript แจ้ง alert ข้อความ
    echo "<meta http-equiv ='refresh'content='0;URL=show.php'>";// คำสั่งให้ refreshหน้าไปหน้าที่เราต้องการ
} else {
    echo"<script type='text/javascript'>alert('ลบข้อมูลไม่สำเร็จ');window.history.go(-1);</script>";//javascript แจ้ง alert ข้อความ และคำสั่งให้ refresh หน้าเดิมถ้าลบข้อความไม่สำเร็จ
}    

?>
แก้ไขล่าสุดโดย mindphp เมื่อ 21/11/2012 4:30 pm, แก้ไขไปแล้ว 1 ครั้ง.
เหตุผล: ครอบโค้ด php [code=php]...โค้ดของท่าน [/code]
บุคคลทั่วไป

Re: ตัวอย่างการเขียนโปรแกรม บันทึก ลบ แก้ไข แสดงข้อมูล ค้นหา ข้อมูล จาก MySQL PHP

โพสต์ที่ยังไม่ได้อ่าน โดย บุคคลทั่วไป »

modtanoy_pen เขียน: 21/11/2012 2:18 pm ตัวอย่างการเขียนโปรแกรม บันทึก ลบ แก้ไข แสดงข้อมูล ค้นหา ข้อมูล จากฐานข้อมูล MySQL ด้วย PHP

การ INSERT ข้อมูลลงฐานข้อมูลโดยรับค่าจากฟอร์ม
ขั้นตอนแรกสร้างฟอร์มรับค่าขึ้นมา 1 ฟอร์ม (from.php)

โค้ด: เลือกทั้งหมด

<?php 
session_start ();
if (isset($_SESSION['post'])){
    $post=$_SESSION['post'];
}else{
    $post=array();
}
if (isset($_SESSION['error'])){
    $error=$_SESSION['error'];
}else{
    $error=array();
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>กรอกข้อมูลสำหรับการติดต่อ</title>
<style type="text/css">
.style1 {color: #FF0000}
.style10 {color: #FF6666}
.style11 {font-size: 24px}
.style12 {color: #FF6666; font-weight: bold; }
</style>
</head>

<body>
<form id="form1" name="form1" method="post" action="insert.php">
  <p><strong><span class="style10">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style11">กรอกข้อมูลสำหรับการติดต่อ</span></span></strong></p>
  <table width="100%" border="1" align="center" bgcolor="#FFCCCC">
    <tr>
      <td width="24%">
        <div align="right">
      ชื่อร้าน/สวน :</div></td>
      <td width="76%"><input type="text" name="nameth" id="nameth" value="<?php echo isset($post['nameth']) ? $post['nameth'] : '';?>" /></td>
    </tr>
    <tr>
      <td><div align="right">ชื่อร้าน/สวน (ภาษาอังกฤษ) :</div></td>
      <td><input type="text" name="nameeg" id="nameeg" value="<?php echo isset ($post['nameeg'])? $post['nameeg'] : '';?>"/>
        <span class="style1">*<?php echo isset ($error['nameeg'])? $error['nameeg'] : '';?></span></td>
    </tr>
    <tr>
      <td><div align="right">รายละเอียดเกี่ยวกับร้านหรือสวน :</div></td>
      <td><textarea name="detail" id="detail" cols="45" rows="5"><?php echo isset ($post['detail'])? $post['detail'] : '';?></textarea> 
        <span class="style1">*<?php echo isset ($error['detail'])? $error['detail'] : '';?></span></td>
    </tr>
    <tr>
      <td><div align="right">ที่อยู่ :</div></td>
      <td><textarea name="address" id="address" cols="45" rows="5"><?php echo isset ($post['address'])? $post['address'] : '';?></textarea>
        <span class="style1">*<?php echo isset ($error['address'])? $error['address'] : '';?></span></td>
    </tr>
    <tr>
      <td><div align="right">จังหวัด :</div></td>
      <td><label>
        <input type="text" name="province" id="province" value="<?php echo isset ($post['province'])? $post['province'] : '';?>"/>
        <span class="style1">*<?php echo isset ($error['province'])? $error['province'] : '';?></span></label></td>
    </tr>
    <tr>
      <td><div align="right">โทรศัพท์ :</div></td>
      <td><input type="text" name="tel1" id="tel1" value="<?php echo isset ($post['tel1'])? $post['tel1'] : '';?>" /> 
        <span class="style1">*<?php echo isset ($error['tel1'])? $error['tel1'] : '';?></span></td>
    </tr>
    <tr>
      <td><div align="right">Fax :</div></td>
      <td><input type="text" name="fax" id="fax" value="<?php echo isset ($post['fax'])? $post['fax'] : '';?>" /></td>
    </tr>
    <tr>
      <td><div align="right">Website http://</div></td>
      <td><input type="text" name="webs" id="webs" value="<?php echo isset ($post['webs'])? $post['webs'] : '';?>" />Ex. www.yourweb.com </td>
    </tr>
    <tr>
      <td class="style12">ข้อมูลติดต่อ</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td><div align="right">ชื่อ-สกุล :</div></td>
      <td><input type="text" name="name" id="name" value="<?php echo isset ($post['name'])? $post['name'] : '';?>" />
        <span class="style1">*<?php echo isset ($error['name'])? $error['name'] : '';?></span></td>
    </tr>
    <tr>
      <td><div align="right">เพศ :</div></td>
      <td><input name="gender" type="radio" id="radio" value="ชาย" checked="checked" <?php isset($post['gender'])? "checked" :'';
       ?> />
        ชาย 
        <input type="radio" name="gender" id="radio2" value="หญิง" <?php isset($post['gender'])? "checked" :'';
       ?> />
        หญิง</td>
    </tr>
    <tr>
      <td><div align="right">E-mail :</div></td>
      <td><input type="text" name="email" id="email" value="<?php echo isset ($post['email'])? $post['email'] : '';?>" /></td>
    </tr>
    <tr>
      <td><div align="right">เบอร์โทรศัพท์ (เบอร์ภายใน) :</div></td>
      <td><input type="text" name="tel2" id="tel2" value="<?php echo isset ($post['tel2'])? $post['tel2'] : '';?>" />
        <span class="style1">*<?php echo isset ($error['tel2'])? $error['tel2'] : '';?></span></td>
    </tr>
    <tr>
      <td><div align="right">เบอร์มือถือ :</div></td>
      <td><input type="text" name="mobile" id="mobile" value="<?php echo isset ($post['mobile'])? $post['mobile'] : '';?>" /></td>
    </tr>
    <tr>
      <td colspan="2"><div align="center">
        <input type="submit" name="button" id="button" value="ตกลง" />
        <input type="reset" name="button2" id="button2" value="ยกเลิก" />
      </div></td>
    </tr>
  </table>
</form>
</body>
</html>
ขั้นตอนที่สองสร้างไฟล์การเชื่อม DATABASE SERVER และฐานข้อมูล (connect.php)

โค้ด: เลือกทั้งหมด

<?php 
$host ="localhost";//ชื่อโฮส
$user ="root";//ชื่อผู้ใช้
$pass ="";//รหัสผ่าน
$db ="pen";//ชื่อฐานข้อมูลที่เราสร้างไว้
mysql_connect ($host,$user,$pass) or die ("ติดต่อเครื่อง Database Server ไม่สำเร็จ");//ติดต่อ Database Server
mysql_select_db($db) or die ("ติดต่อฐานข้อมูลไม่สำเร็จ");//ติอต่อฐานข้อมูล
mysql_query("SET NAMES utf8")//ตั้งค่าภาษา
?>
ขั้นตอนที่สามสร้างไฟล์เพื่อบันทึกข้อมูล (insert.php)

โค้ด: เลือกทั้งหมด

<?php 
session_start() ;
$_SESSION['post']=$_POST;
$_SESSION['error']="";
$nameth=$_POST['nameth'];//ตั้งค่าตัวแปล
$nameeg=$_POST['nameeg'];
$detail=$_POST['detail'];
$address=$_POST['address'];
$province=$_POST['province'];
$tel1=$_POST['tel1'];
$fax=$_POST['fax'];
$webs=$_POST['webs'];
$name=$_POST['name'];
$gender=$_POST['gender'];
$email=$_POST['email'];
$tel2=$_POST['tel2'];
$mobile=$_POST['mobile'];
require_once("connect.php");
if (trim ($nameeg==""))//ตรวจสอบฟิลด์ที่ไม่ได้กรอกกรอกข้อมูล
{
    $_SESSION['error']['nameeg']='กรุณากรอกชื่อภาษาอังกฤษให้ถูกต้องเพื่อความสะดวกในการตรวจสอบข้อมูล';
} if(trim ($detail=="")){
    $_SESSION['error']['detail']='กรุณากรอกรายละเอียดเกี่ยวกับร้าน/สวน';
} if(trim ($address=="")){
    $_SESSION['error']['address']='กรุณากรอกที่อยู่';
} if(trim ($province=="")){
    $_SESSION['error']['province']='กรุณากรอกจังหวัด';
} if(trim ($tel1=="")){
    $_SESSION['error']['tel1']='กรุณากรอกเบอร์โทร';
} if(trim ($name=="")){
    $_SESSION['error']['name']='กรุณากรอกชื่อ';
} if(trim ($tel2=="")){
    $_SESSION['error']['tel2']='กรุณากรอกเบอร์โทรศัพท์ภายใน';
    echo "<script> window.history.go(-1);</script>\n";
    exit();
}else{
    $sql="INSERT INTO test (Id,nameth,nameeg,detail,address,province,tel1,fax,webs,name,gender,email,tel2,mobile)VALUES ('','$nameth','$nameeg','$detail','$address','$province','$tel1','$fax','$webs','$name','$gender','$email','$tel2','$mobile')";//คำสั่งเพิ่มข้อมูล
    $sql_query=mysql_query($sql);
 if($sql_query) {
        echo "<script type='text/javascript'>alert('บันทึกข้อมูลเรียบร้อยแล้ว')</script>";
        echo "<meta http-equiv ='refresh'content='0;URL=show.php'>";
        $_SESSION['post']="";
    }else{
        echo "<script type='text/javascript'>alert('เกิดข้อผิดพลาดในการบันทึกข้อมูล');window.history.go(-1);</script>" ;
    }
}
mysql_close();

?>
meoktwi

Re: ตัวอย่างการเขียนโปรแกรม บันทึก ลบ แก้ไข แสดงข้อมูล ค้นหา ข้อมูล จาก MySQL PHP

โพสต์ที่ยังไม่ได้อ่าน โดย meoktwi »

Thanks for sharing very good information. Your blog has some really cool information. you read the post about Impressed. I'll bookmark your blog and come back for the next article.
SexyKitty
PHP Newbie
PHP Newbie
โพสต์: 5
ลงทะเบียนเมื่อ: 10/09/2023 7:06 pm

Re: ตัวอย่างการเขียนโปรแกรม บันทึก ลบ แก้ไข แสดงข้อมูล ค้นหา ข้อมูล จาก MySQL PHP

โพสต์ที่ยังไม่ได้อ่าน โดย SexyKitty »

Yes, I am entirely agreed with this article, and I just want say that this article is very helpful and enlightening. I also have some precious piece of concerned info !!!!!!Thanks.
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: Google [Bot] และบุคลทั่วไป 33