มีปัญหาการอัปเดทข้อมูลไม่ได้ครับ php mysql
ถาม-ตอบ แนะนำไอเดียว โค้ดตัวอย่าง แนวทาง วิธีแก้ปัญหา มีปัญหาการอัปเดทข้อมูลไม่ได้ครับ php mysql
มีปัญหาการอัปเดทข้อมูลไม่ได้ครับ php mysql
สวัดดีครับ
มีปัญหาการอัปเดทข้อมูลไม่ได้ครับ หลังจากที่ใส่รหัสนักเรียน คนที่ 5-6 เนื่องจากอยู่ต่างชั้นกัน กับนักเรียน คนที่ 1-4
ก่อนเพิ่มรหัสนักเรียน คนที่ 5-6 ใช้งานได้ครับ รบกวนด้วยครับ
โครงสร้างฐานข้อมูล


ลิ้งจะส่งไปยัง form.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 10px;
}
th{
background-color:lightgreen;
}
td1 {
padding: 0.3em 0.3em;
text-align: rigth;
}
</style>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="author" content="Codrops" />
<link rel="shortcut icon" href="../favicon.ico">
<link rel="stylesheet" type="text/css" href="css/normalize.css" />
<link rel="stylesheet" type="text/css" href="css/demo.css" />
<link rel="stylesheet" type="text/css" href="css/component.css" />
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body >
<?
include 'database.php';
$sql = "SELECT classID,c_name
FROM class
WHERE classID = '".$_GET["classID"]."' ";
$result = mysqli_query($link,$sql);
$objResult = mysqli_fetch_array($result);
?>
<h3 align=center>ข้อมูลนักเรียนชั้น <?=$objResult["c_name"];?</h3>
<hr>
<?php
$sql = "Select student.StudentID,student.Name,student.Surname,student.classID,check.ma,check.kad,check.la,check.kid
From student INNER JOIN check ON student.StudentID = check.StudentID
WHERE classID = '".$_GET["classID"]."' ORDER BY StudentID ASC ";
$result = mysqli_query($link,$sql);
?>
<Form Action="result.php" Method="Post">
<?php
echo "<table border=1px align=center>";
echo "<th>StudentID</th><th>Name&Surname</th><th>ma</th><th>kad</th><th>la</th><th>kid</th>";
while ($data = mysqli_fetch_array($result))
{
$choice4 = $data['kid'];
$choice3 = $data['la'];
$choice2 = $data['kad'];
$choice1 = $data['ma'];
$surname = $data['Surname'];
$name = $data['Name'];
$no = $data['StudentID'];
echo "<tr>";
echo "<td align=center width=15%px><Input Type=Radio Name=q[$no] Value=4></td>";
echo "<td align=center width=15%px><Input Type=Radio Name=q[$no] Value=3></td>";
echo "<td align=center width=15%px><Input Type=Radio Name=q[$no] Value=2></td>";
echo "<td align=center width=15%px><Input Type=Radio Name=q[$no] Value=1 checked></td>";
echo "<td align=rigth >$name $surname</td>";
echo "<td bgcolor=lightblue align=center>$no</td></tr>";
}
echo "</table>
<center><button type='submit' value='Submit'>Save hudur</button>
<button type='reset' value='Reset'>Reset</button></center>
</Form>";
?>
<?php
mysqli_close($link);
?>
</body>
</html>
Form จะส่งค่าไปยัง result.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 10px;
}
th{
background-color:orange;
}
</style>
</head>
<body>
<h2 align=center>ข้อมูลนักเรียน</h2>
<hr>
<?php
include 'database.php';
$sql = "Select student.StudentID,student.Name,student.Surname,student.classID,check.ma,check.kad,check.la,check.kid
From student INNER JOIN check ON student.StudentID = check.StudentID
WHERE classID = '".$_GET["classID"]."' ORDER BY StudentID ASC ";
$result = mysqli_query($link,$sql);
echo "<table align=center>";
echo "<th>StudentID</th><th>Name&Surname</th><th>ma</th><th>kad</th><th>la</th><th>kid</th>";
$fld=array('Name','ma', 'kad','la','kid');
$result = $link->query('Select student.StudentID,student.Name,student.Surname,student.classID,check.ma,check.kad,check.la,check.kid
From student INNER JOIN check ON student.StudentID = check.StudentID
WHERE classID = '".$_GET["classID"]."' ORDER BY StudentID ASC');
while($data = $result->fetch_assoc() ){
$no = $data['StudentID'];
$name = $data['Name'];
$surname = $data['Surname'];
$key=$fld[$_POST['q'][$no]];
$data[$key]++;
$sql = 'UPDATE check SET '.$key.' = '.$data[$key]." WHERE StudentID=$no";
$link->query($sql) or die( $sql . "<br>" . $link->error);
echo "<tr>
<td align=center width=20%px>$data[kid]</td>
<td align=center width=20%px>$data[la]</td>
<td align=center width=20%px>$data[kad]</td>
<td align=center width=20%px>$data[ma]</td>
<td align=right width=20%px>$name $surname</td>
<td bgcolor=lightgreen align=center>$no</td>
</tr>";
}
echo "</table><br>";
echo "<form action='form.php'>
<center><input type='submit' value='กลับสู่หน้าหลัก'></center>
</form>";
mysqli_close($link);
?>
</body>
</html>
มีปัญหาการอัปเดทข้อมูลไม่ได้ครับ หลังจากที่ใส่รหัสนักเรียน คนที่ 5-6 เนื่องจากอยู่ต่างชั้นกัน กับนักเรียน คนที่ 1-4
ก่อนเพิ่มรหัสนักเรียน คนที่ 5-6 ใช้งานได้ครับ รบกวนด้วยครับ
โครงสร้างฐานข้อมูล
ลิ้งจะส่งไปยัง form.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 10px;
}
th{
background-color:lightgreen;
}
td1 {
padding: 0.3em 0.3em;
text-align: rigth;
}
</style>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="author" content="Codrops" />
<link rel="shortcut icon" href="../favicon.ico">
<link rel="stylesheet" type="text/css" href="css/normalize.css" />
<link rel="stylesheet" type="text/css" href="css/demo.css" />
<link rel="stylesheet" type="text/css" href="css/component.css" />
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body >
<?
include 'database.php';
$sql = "SELECT classID,c_name
FROM class
WHERE classID = '".$_GET["classID"]."' ";
$result = mysqli_query($link,$sql);
$objResult = mysqli_fetch_array($result);
?>
<h3 align=center>ข้อมูลนักเรียนชั้น <?=$objResult["c_name"];?</h3>
<hr>
<?php
$sql = "Select student.StudentID,student.Name,student.Surname,student.classID,check.ma,check.kad,check.la,check.kid
From student INNER JOIN check ON student.StudentID = check.StudentID
WHERE classID = '".$_GET["classID"]."' ORDER BY StudentID ASC ";
$result = mysqli_query($link,$sql);
?>
<Form Action="result.php" Method="Post">
<?php
echo "<table border=1px align=center>";
echo "<th>StudentID</th><th>Name&Surname</th><th>ma</th><th>kad</th><th>la</th><th>kid</th>";
while ($data = mysqli_fetch_array($result))
{
$choice4 = $data['kid'];
$choice3 = $data['la'];
$choice2 = $data['kad'];
$choice1 = $data['ma'];
$surname = $data['Surname'];
$name = $data['Name'];
$no = $data['StudentID'];
echo "<tr>";
echo "<td align=center width=15%px><Input Type=Radio Name=q[$no] Value=4></td>";
echo "<td align=center width=15%px><Input Type=Radio Name=q[$no] Value=3></td>";
echo "<td align=center width=15%px><Input Type=Radio Name=q[$no] Value=2></td>";
echo "<td align=center width=15%px><Input Type=Radio Name=q[$no] Value=1 checked></td>";
echo "<td align=rigth >$name $surname</td>";
echo "<td bgcolor=lightblue align=center>$no</td></tr>";
}
echo "</table>
<center><button type='submit' value='Submit'>Save hudur</button>
<button type='reset' value='Reset'>Reset</button></center>
</Form>";
?>
<?php
mysqli_close($link);
?>
</body>
</html>
Form จะส่งค่าไปยัง result.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 10px;
}
th{
background-color:orange;
}
</style>
</head>
<body>
<h2 align=center>ข้อมูลนักเรียน</h2>
<hr>
<?php
include 'database.php';
$sql = "Select student.StudentID,student.Name,student.Surname,student.classID,check.ma,check.kad,check.la,check.kid
From student INNER JOIN check ON student.StudentID = check.StudentID
WHERE classID = '".$_GET["classID"]."' ORDER BY StudentID ASC ";
$result = mysqli_query($link,$sql);
echo "<table align=center>";
echo "<th>StudentID</th><th>Name&Surname</th><th>ma</th><th>kad</th><th>la</th><th>kid</th>";
$fld=array('Name','ma', 'kad','la','kid');
$result = $link->query('Select student.StudentID,student.Name,student.Surname,student.classID,check.ma,check.kad,check.la,check.kid
From student INNER JOIN check ON student.StudentID = check.StudentID
WHERE classID = '".$_GET["classID"]."' ORDER BY StudentID ASC');
while($data = $result->fetch_assoc() ){
$no = $data['StudentID'];
$name = $data['Name'];
$surname = $data['Surname'];
$key=$fld[$_POST['q'][$no]];
$data[$key]++;
$sql = 'UPDATE check SET '.$key.' = '.$data[$key]." WHERE StudentID=$no";
$link->query($sql) or die( $sql . "<br>" . $link->error);
echo "<tr>
<td align=center width=20%px>$data[kid]</td>
<td align=center width=20%px>$data[la]</td>
<td align=center width=20%px>$data[kad]</td>
<td align=center width=20%px>$data[ma]</td>
<td align=right width=20%px>$name $surname</td>
<td bgcolor=lightgreen align=center>$no</td>
</tr>";
}
echo "</table><br>";
echo "<form action='form.php'>
<center><input type='submit' value='กลับสู่หน้าหลัก'></center>
</form>";
mysqli_close($link);
?>
</body>
</html>

คำแนะนำ และการใช้งาน
สมาชิก กรุณา ล็อกอินเข้าระบบ เพื่อตั้งคำถามใหม่ หรือ ตอบคำถาม สมาชิกใหม่ สมัครสมาชิกได้ที่ สมัครสมาชิก
- ถาม-ตอบ กรุณา ล็อกอินเข้าระบบ
ความคิดเห็นที่
1

การใช้งาน SQL INNER JOIN
https://www.ninenik.com/content.php?arti_id=553 via @ninenik

ความคิดเห็นที่
2
ระบบเช็คชื่อนักเรียน มา ขาด ลา กิจ
อ้างอิง
http://www.thaicreate.com/php/forum/119396.html
อ้างอิง
http://www.thaicreate.com/php/forum/119396.html

ความคิดเห็นที่
3
คืออยากให้มันบันทึกได้ตามชั้นเรียน ต้องเเก้ไขโค้ดส่วนไหนครับ
ถ้าเเก้ข้อมูลนักเรียนทั้งหมดใน table student คอลั้มน์ classID ให้อยู่ชั้นเดียวกัน สามารถบันทึกได้ครับ
เเต่ความเป็นจริง นักเรียนทั้งหมดไม่ได้อยู่ชั้นเดียวกัน จึงอยากบันทึกเเยกตามชั้นเรียน WHERE classID = '".$_GET["classID"]."' รบกวนด้วยครับ
ถ้าเเก้ข้อมูลนักเรียนทั้งหมดใน table student คอลั้มน์ classID ให้อยู่ชั้นเดียวกัน สามารถบันทึกได้ครับ
เเต่ความเป็นจริง นักเรียนทั้งหมดไม่ได้อยู่ชั้นเดียวกัน จึงอยากบันทึกเเยกตามชั้นเรียน WHERE classID = '".$_GET["classID"]."' รบกวนด้วยครับ

ความคิดเห็นที่
4
ผมลองตัดโค้ดไฟล result.php ให้เหลือคำสั่งอัปเดทอย่างเดียว
<?php
$fld=array('Name','ma', 'kad','la','kid');
$result = $link->query('Select student.StudentID,student.Name,student.Surname,student.classID,check.ma,check.kad,check.la,check.kid
From student INNER JOIN check ON student.StudentID = check.StudentID
WHERE classID = '".$_GET["classID"]."' ORDER BY StudentID ASC');
while($data = $result->fetch_assoc() ){
$no = $data['StudentID'];
$name = $data['Name'];
$surname = $data['Surname'];
$key=$fld[$_POST['q'][$no]];
$data[$key]++;
$sql = 'UPDATE check SET '.$key.' = '.$data[$key]." WHERE StudentID=$no";
$link->query($sql) or die( $sql . "<br>" . $link->error);
}
?>
มันวิ่งไปหน้า result.php พร้อมเเสดง error ออกมาครับ
UPDATE check SET = 1 WHERE StudentID=1111
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= 1 WHERE StudentID=1111' at line 1
มันฟ้องอะไร เเละต้องเเก้อย่างไรครับ
<?php
$fld=array('Name','ma', 'kad','la','kid');
$result = $link->query('Select student.StudentID,student.Name,student.Surname,student.classID,check.ma,check.kad,check.la,check.kid
From student INNER JOIN check ON student.StudentID = check.StudentID
WHERE classID = '".$_GET["classID"]."' ORDER BY StudentID ASC');
while($data = $result->fetch_assoc() ){
$no = $data['StudentID'];
$name = $data['Name'];
$surname = $data['Surname'];
$key=$fld[$_POST['q'][$no]];
$data[$key]++;
$sql = 'UPDATE check SET '.$key.' = '.$data[$key]." WHERE StudentID=$no";
$link->query($sql) or die( $sql . "<br>" . $link->error);
}
?>
มันวิ่งไปหน้า result.php พร้อมเเสดง error ออกมาครับ
UPDATE check SET = 1 WHERE StudentID=1111
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= 1 WHERE StudentID=1111' at line 1
มันฟ้องอะไร เเละต้องเเก้อย่างไรครับ

ความคิดเห็นที่
5

อีกอย่างฟิลด์ที่น่าจะมีคือวันที่ การอัพเดทข้อมูลแต่ละครั้ง น่าจะอ้างอิงวันที่ด้วย เช่น เลือกห้องเรียน
แสดงรายชื่อนักเรียน ถ้าจัดการการ ขาด ลา มา สาย ก็น่าจะให้เลือกวันที่ก่อนแล้ว บันทึกสถานะ ตัวอย่าง ตาราง check
stdid date ma kad la kid
11111 2017-04-23 1 0 0 0
หรือ ถ้าไม่่มีวันที่ ก็ใช้เป็นแค่การอัพเดท ค่าปกติ เอาโค้ดแนวทางนี้ไปดู ที่เหลือคงต้องพยายามเอง เพราะขึ้นกับความเข้าใจ
แนวทางโค้ดใช้รูปแบบ mysqli ดูจากเนื้อหานี้
https://www.ninenik.com/forum_view_2398_1.html#comment_5642
ใช้รูปแบบการคิวรี่ด้วย LEFT JOIN
การใช้งาน SQL LEFT JOIN
https://www.ninenik.com/content.php?arti_id=554 via @ninenik
ลิ้สรายการห้องเรียน
1 2 3 4 5 6 7 8 9 10 11 | <?php $sql = "SELECT * FROM class" ; $result = $mysqli ->query( $sql ); if ( $result ){ while ( $row = $result ->fetch_assoc()){ ?> <a href= "liststudent.php?classID=<?=$row['classID']?>" ><?= $row [ 'c_name' ]?></a> <?php } } ?> |
หน้ารับค่าแสดงราชื่อนักเรียน พร้อมฟอร์มส่งค่าการ มา ลา ขาด
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | <?php if (isset( $_GET [ 'classID' ]) && $_GET [ 'classID' ]!= "" ){ ?> <form name= "form1" method= "post" action= "result.php?classID=<?=$_GET['classID']?>" > <table width= "100%" border= "0" cellspacing= "0" cellpadding= "0" > <tr> <td>name</td> <td>ma</td> <td>kad</td> <td>la</td> <td>kid</td> </tr> <?php $sql =" SELECT * FROM student WHERE classID= '".$_GET[' classID ']."' "; $result = $mysqli ->query( $sql ); if ( $result ){ while ( $row = $result ->fetch_assoc()){ ?> <tr> <td></td> <td><input type= "radio" name= "check_status[<?=$row['StudentID']?>]" id= "radio" value= "1" ></td> <td><input type= "radio" name= "check_status[<?=$row['StudentID']?>]" id= "radio" value= "2" ></td> <td><input type= "radio" name= "check_status[<?=$row['StudentID']?>]" id= "radio" value= "3" ></td> <td><input type= "radio" name= "check_status[<?=$row['StudentID']?>]" id= "radio" value= "4" ></td> </tr> <?php } } ?> </table> <input type= "submit" name= "button" id= "button" value= "Submit" > </form> <?php } ?> |
หน้ารับค่าจากฟอร์มพร้อมแสดงข้อมูลการ มา ลา ขาด ....
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | <?php // UPDATE หรือ INSERT ค่า if (isset( $_POST [ 'buttun' ])){ $arr_filed = array ( "1" => "ma" , "2" => "kad" , "3" => "la" , "4" => "kid" ); if (isset( $_POST [ 'check_status' ]) && count ( $_POST [ 'check_status' ])>0){ foreach ( $_POST [ 'check_status' ] as $k_stuID => $v_checkStats ){ $sql = " SELECT * FROM check WHERE StudentID= '".$k_stuID."' "; $result = $mysqli ->query( $sql ); if ( $result && $result ->num_rows>0){ // คิวรี่ข้อมูลสำเร็จหรือไม่ และมีรายการข้อมูลหรือไม่ $row = $result ->fetch_array(); $sql_update =" UPDATE check SET $arr_filed [ $v_checkStats ]= $arr_filed [ $v_checkStats ]+1 WHERE StudentID= '".$row[' StudentID ']."' "; if ( $mysqli ->query( $sql_update )){ // มีการคิวรี่อัพเพทข้อมูล if ( $mysqli ->affected_rows>0){ // การคิวรี่มีการเปลี่ยนแปลงค่า // ทำคำสั่งใดๆ ถ้ามี } else { // การคิวรี่ไม่มีการเปลี่ยนแปลงค่า เช่น กรณีเราอัพเดทค่าเดิม // ทำคำสั่งใดๆ ถ้ามี } } } else { $sql_insert =" INSERT INTO check SET StudentID= '".$row[' StudentID ']."' , $arr_filed [ $v_checkStats ]=1 "; $result = $mysqli ->query( $sql_insert ); if ( $result && $mysqli ->affected_rows>0){ // มีการคิวรี่และมีการเพิ่มรายการ $insert_userID = $mysqli ->insert_id; // primary key ของรายการที่เพิ่มเข้ามาใหม่ // ทำคำสั่งใดๆ ถ้ามี } } } } } ?> <table width= "100%" border= "0" cellspacing= "0" cellpadding= "0" > <tr> <td>name</td> <td>ma</td> <td>kad</td> <td>la</td> <td>kid</td> </tr> <?php if (isset( $_GET [ 'classID' ]) && $_GET [ 'classID' ]!= "" ){ $sql =" SELECT a.*,b.* FROM student a LEFT JOIN check b ON a.StudentID=b.StudentID WHERE a.classID= '".$_GET[' classID ']."' "; $result = $mysqli ->query( $sql ); if ( $result ){ while ( $row = $result ->fetch_assoc()){ ?> <tr> <td></td> <td><?= $row [ 'ma' ]?></td> <td><?= $row [ 'kad' ]?></td> <td><?= $row [ 'la' ]?></td> <td><?= $row [ 'kid' ]?></td> </tr> <?php } } } ?> </table> |
ตัวอย่างโค้ดเป็นแนวทางโครงสร้าง เวลาไปประยุกต์ใช้ ให้ตราวจความถูกต้องของข้อมูล ประกอบ

ความคิดเห็นที่
6
ผมลองติดตั้งดูเเล้วครับ ไม่เเสดง Error ใดๆ
เเต่ค่าที่ส่งไป ไม่ได้บันทึกที่ฐานข้อมูล เเละไม่ได้เเสดงออกมาที่่ result.php
ความหมายจากโค้ด result.php
ถ้าหากใน table ckeck ยังไม่มีข้อมูลนักเรียน ตาม table student ให้ insert ค่า studentID เข้าไป
พร้อมค่าการเช็คการมาเรียน ขาด ลา กิจ โดยค่าการเช็ค = 1
เเละ ถ้าหากใน table ckeck มีข้อมูลนักเรียน(studentID)เเล้ว ให้อัปเดท ค่าการเช็คการมาเรียน ขาด ลา กิจ โดยเพิ่มจากค่าเดิมอีกหนึ่ง
ผมเข้าใจถูกต้องหรือปล่าว
เเล้ว ตรงคอมเม้นต //ทำคำสั่งใดๆ ถ้ามี มีสามที่ ให้เพิ่มคำสั่งอะไรครับผมไม่เข้าใจ




เเต่ค่าที่ส่งไป ไม่ได้บันทึกที่ฐานข้อมูล เเละไม่ได้เเสดงออกมาที่่ result.php
ความหมายจากโค้ด result.php
ถ้าหากใน table ckeck ยังไม่มีข้อมูลนักเรียน ตาม table student ให้ insert ค่า studentID เข้าไป
พร้อมค่าการเช็คการมาเรียน ขาด ลา กิจ โดยค่าการเช็ค = 1
เเละ ถ้าหากใน table ckeck มีข้อมูลนักเรียน(studentID)เเล้ว ให้อัปเดท ค่าการเช็คการมาเรียน ขาด ลา กิจ โดยเพิ่มจากค่าเดิมอีกหนึ่ง
ผมเข้าใจถูกต้องหรือปล่าว
เเล้ว ตรงคอมเม้นต //ทำคำสั่งใดๆ ถ้ามี มีสามที่ ให้เพิ่มคำสั่งอะไรครับผมไม่เข้าใจ






ขอบคุณทุกการสนับสนุน
![]()