ข้ามไปที่เนื้อหาหลัก

PHP MySQL ตอนที่ 1 : การบันทึกข้อมูลด้วย MySQLi Function

- ออกแบบฐานข้อมูลด้วย phpMyAdmin
- สร้างแบบฟอร์มด้วย Bootstap css framework
- บันทึกข้อมูลด้วย PHP mysqli




โครงสร้างฐานข้อมูล 

--
-- Database: `dev_database`
--

-- --------------------------------------------------------

--
-- Table structure for table `comments`
--

CREATE TABLE `comments` (
  `id` int(11) NOT NULL,
  `name` varchar(30) NOT NULL,
  `detail` text NOT NULL,
  `sex` int(1) NOT NULL,
  `date_record` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='ตารางเก็บความคิดเห็น';

--
-- Indexes for dumped tables
--

--
-- Indexes for table `comments`
--
ALTER TABLE `comments`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `comments`
--
ALTER TABLE `comments`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

การเชื่อมต่อฐานข้อมูล

<?php
$conn = mysqli_connect("localhost","tobedev","dev.1234");

if (!$conn->set_charset("utf8")) {
    printf("Error loading character set utf8: %s\n", $conn->error);
    exit();
}

$conn->select_db("test");
$conn->select_db("dev_database");

?>

แบบฟอร์ม

<?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>
     

      <div class="row">
     
      <form class="form-horizontal" method="POST" action="save.php">
  <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" 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"></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" value="1"> ชาย
        </label>
        <label class="radio-inline">
          <input type="radio" name="sex" id="inlineRadio2" 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>

ไฟล์บันทึกข้อมูล
 <?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');

$sql = "INSERT INTO `comments`
            (`id`, `name`, `detail`, `sex`, `date_record`)
            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);

?>

ดาวน์โหลดซอร์สโค้ดตัวอย่างทั้งหมดได้ที่
https://github.com/sunzandesign/example

PHP CI MANIA PHP Code Generator 
โปรแกรมช่วยสร้างโค้ด ลดเวลาการเขียนโปรแกรม

สนใจสั่งซื้อเพียง 4,500 บาท
http://fastcoding.phpcodemania.com/




สนับสนุนค่ากาแฟผู้เขียนได้ที่

PayPal

ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

แจกฟรี!! ระบบต่างๆที่พัฒนาด้วย PHP สำหรับนำไปใช้ในงานต่างๆ

       สำหรับหลายท่านที่ขอโค้ดเข้ามาทาง Inbox ของเฟซบุ๊กแฟนเพจ หรือถามถึงระบบต่างๆหลังไมค์มานั้น ส่วนใหญ่ก็มีแจกอยู่แล้วในเว็บบอร์ด ThaiCreate.Com นะครับ และด้านล่างนี้ก็เป็นระบบต่างๆที่แจกให้นำไปลองใช้ลองศึกษากันครับ

สอนเขียน PHP แสดงการจองห้องประชุมแบบไฮไลท์ตามช่วงเวลา (แบบเชื่อมต่อฐานข้อมูล MySQL)

ตัวอย่าง ผลลัพธ์ที่ได้จากการจองในฐานข้อมูล ตาราง tb_room สร้างตารางรายชื่อห้องประชุม สำหรับ id นั้นเป็น Primarykey จะกำหนดให้สร้างอัตโนมัติ ทุกครั้งที่เราเพิ่มชื่อห้องประชุมใหม่ -- -- Database: `tobedev_example` -- -- -------------------------------------------------------- -- -- Table structure for table `tb_room` -- CREATE TABLE IF NOT EXISTS `tb_room` (   `id` int(11) NOT NULL,   `name` varchar(30) NOT NULL ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8; -- -- Dumping data for table `tb_room` -- INSERT INTO `tb_room` (`id`, `name`) VALUES (1, 'ห้องประชุม 1'), (2, 'ห้องประชุม 2'), (3, 'ห้องประชุม 3'), (4, 'ห้องประชุม 4'), (5, 'ห้องประชุม 5'); -- -- Indexes for dumped tables -- -- -- Indexes for table `tb_room` -- ALTER TABLE `tb_room`   ADD PRIMARY KEY (`id`); -- -- AUTO_INCREMENT for dumped tables -- -- -- AUTO_INCREMENT for table `tb_room` -- ALTER TABLE `tb_room`   MODIFY `i...

การดึงข้อมูล MySQL มาแสดงในปฏิทิน Fullcalendar ด้วย PHP

หลักการทำงาน เรียกข้อมูลจากตาราง tb_event ด้วยฟังก์ชั่นของ MySQLi ข้อมูลจากฐานข้อมูลจะถูกวนลูปเก็บไว้ใน $data และจะถูกส่งไปที่ Fullcalendar ด้วยการแปลงข้อมูลจาก PHP ให้อยู่ในรูปแบบ JSON อาร์เรย์ด้วยฟังก์ชั่น json_encode() PHP Code <?php //Database $data = array(); $link = mysqli_connect("127.0.0.1", "tobedev", "1234", "tobedev_example"); mysqli_set_charset($link, 'utf8'); if (!$link) {     echo "Error: Unable to connect to MySQL." . PHP_EOL;     echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;     echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;     exit; } $query = "SELECT * FROM tb_event"; if ($result = $link->query($query)) {     /* fetch object array */     while ($obj = $result->fetch_object()) {        $data[] = array(                 ...