โค้ดหน้า insertfrom.php
โค้ด: เลือกทั้งหมด
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>บันทึกข้อมูลนักเรียน</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">
</head>
<body>
<div class="container my-5" >
<h2 class="text-center">แบบฟอร์มบันทึกข้อมูลนักเรียน</h2>
<from action="insertData.php" method= "POST">
<div class="from-group">
<label for="firstname">ชื่อ</label><br>
<input type="text" name="fname" id="" class="from-Control" >
</div>
<div class="from-group">
<label for="lastname">นามสกุล</label><br>
<input type="text" name="lname" id="" class="from-Control" >
</div><br>
<div class="from-group">
<label for="gender">เพศ</label>
<input type="radio" name="gender" id="" value="male" >ชาย
<input type="radio" name="gender" id="" value="female" >หญิง
<input type="radio" name="gender" id="" value="other" >อื่นๆ
</div>
<br>
<input type="submit" value="บันทึกข้อมูล" class= "btn btn-success">
<input type="reset" value="ล้างข้อมูล" class= "btn btn-danger">
<a href="index.php"class="btn btn-primary">กลับหน้าแรก</a>
</from><br>
</div>
</body>
</html>
โค้ดหน้า insertData.php
โค้ด: เลือกทั้งหมด
<?php
//เชื่อมต่อฐานข้อมูล
require('dbconnect.php');
//$con = mysqli_connect("localhost","root","","mydata");
//รับค่าที่ส่งมาจากฟอร์มลงในตัวแปร
$fname=$_POST["fname"];
$lname=$_POST["lname"];
//บันทึกข้อมูล
$sql = "INSERT INTO students(fname,lname) VALUES('$fname,$lname')";
$result=mysqli_query($connect,$sql);//สั่งรันคำสั่ง sql
if ($result){
echo "บันทึกข้อมูลเรียบร้อย";
}else{
echo mysql_error($connect);
}
?>