 |
[PHP]
php บันทึกข้อมูลลงดาต้าเบส แล้วข้อมูลมันลงซ้ำกัน 2rows |
|
 |
|
|
 |
 |
|
php บันทึกข้อมูลลงดาต้าเบส แล้วข้อมูลมันลงซ้ำกัน 2rows
จริงๆต้องขึ้นแต่ จุดที่ 1 แค่แถวเดียว
ในดาต้าเบสก็ขึ้น 2rows ต้องแก้ไขยังไงอ่าคะ หรือต้องใส่เงื่อนไขแบบไหนดี


Code (PHP)
01. <table class = "table" >
02. <thead>
03.
04. <tr>
05. <td width= "480" ><div class = "form-group font_head" align= "center" >
06. <label>หัวข้อการบันทึกคะแนน <span class = "style3" >*</span> :</label>
07. </div></td>
08. <td width= "550" ><label><textarea style= "font-size:12px;" class = "form-control" name= "score_name" cols= "35" rows= "4" id= "score_name" ></textarea></label></td>
09. </tr>
10.
11. <tr>
12. <td width= "480" ><div class = "form-group font_head" align= "center" >
13. <label>คะแนนเต็ม <span class = "style3" >*</span> :</label>
14. </div></td>
15. <td width= "550" ><label><input style= "font-size:12px;" class = "form-control" name= "score_full" type= "text" id= "score_full" value= "" /></label></td>
16. </tr>
17.
18. <tr><td colspan= "2" ><div align= "center" >
19.
20. <strong><div class = 'font_head' ><button type= "button" class = "btn btn-primary font_head" > กลับ </button>
21. <button type= "submit" name= "Submit" id= "Submit" class = "btn btn-primary font_head" > บันทึก </button>
22. </div></strong>
23.
24. </div></td></tr>
25. </thead>
26. </table>
Code (PHP)
01. <?php
02. session_start();
03. ?>
04.
05. <?php
06.
07.
08.
09. include ( "../../config.php" );
10.
11. $strSQL = "INSERT INTO score_head_Secondary (score_head_id, course_class_id, Class_ID, classroom_id, course_id, year_id, term_id, UserID_teacher, score_name, score_full, score_main_id ) VALUES ('null', '" .trim( $_POST ['course_class_id '])."' , '".trim($_POST[' Class_ID '])."' , '".trim($_POST[' classroom_id '])."' , '".trim($_POST[' course_id '])."' , '".trim($_POST[' year_id '])."' , '".trim($_POST[' term_id '])."' , '".trim($_POST[' UserID_teacher '])."' , '".trim($_POST[' score_name '])."' , '".trim($_POST[' score_full '])."' , '".trim($_POST[' score_main_id '])."' );";
12. $objQuery = mysql_query( $strSQL );
13.
14.
15. mysql_close();
16.
17. if ( $objQuery ){
18. echo "<script type='text/javascript'>" ;
19. echo "alert('ADD Data Succesfuly');" ;
20. echo "</script>" ;
21. }
22. else {
23. echo "<script type='text/javascript'>" ;
24. echo "alert('Error back to Update again');" ;
25. echo "</script>" ;
26. }
27. ?>
Tag : PHP, Ms SQL Server 2012, Ms SQL Server 2014, HTML, JavaScript
|
ประวัติการแก้ไข 2020-06-03 10:11:10 2020-06-03 10:21:01
|
 |
 |
 |
 |
Date :
2020-06-02 10:51:05 |
By :
aumebum-km |
View :
823 |
Reply :
12 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คุณเอารูปมา เค้าจะช่วย Dev ยังไงละครับ??
|
 |
 |
 |
 |
Date :
2020-06-02 12:10:15 |
By :
Genesis™ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอดูโค้ด PHP การ Insert Database ครับ
อันนั้นเรียกซ้ำ Field หรือซ้ำ Column ไม่ใช่เหรอ 
|
 |
 |
 |
 |
Date :
2020-06-02 18:14:25 |
By :
PhrayaDev |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 3 เขียนโดย : mr.v เมื่อวันที่ 2020-06-02 18:29:37
รายละเอียดของการตอบ ::
Code (PHP)
01. <table class = "table" >
02. <thead>
03. <tr>
04. <td width= "480" ><div class = "form-group font_head" align= "center" >
05. <label>หัวข้อการบันทึกคะแนน <span class = "style3" >*</span> :</label>
06. </div></td>
07. <td width= "550" ><label><textarea style= "font-size:12px;" class = "form-control" name= "score_name" cols= "35" rows= "4" id= "score_name" ></textarea></label></td>
08. </tr>
09.
10. <tr>
11. <td width= "480" ><div class = "form-group font_head" align= "center" >
12. <label>คะแนนเต็ม <span class = "style3" >*</span> :</label>
13. </div></td>
14. <td width= "550" ><label><input style= "font-size:12px;" class = "form-control" name= "score_full" type= "text" id= "score_full" value= "" /></label></td>
15. </tr>
16.
17. <tr><td colspan= "2" ><div align= "center" >
18.
19. <strong><div class = 'font_head' ><a href= "scoring_system_list.php?course_class_id=<?php echo " ".$_GET['course_class_id']." "; ?>&year_id=<?php echo " ".$_GET['year_id']." "; ?>&term_id=<?php echo $_GET[" term_id "]; ?>" ><button type= "button" class = "btn btn-primary font_head" > กลับ </button></a>
20. <button type= "submit" name= "Submit" id= "Submit" class = "btn btn-primary font_head" > บันทึก </button>
21. </div></strong>
22.
23. </div></td></tr>
24. </thead>
25. </table>
Code (PHP)
01. <?php
02. session_start();
03. ?>
04.
05. <?php
06.
07.
08.
09. include ( "../../config.php" );
10.
11. $strSQL = "INSERT INTO score_head_Secondary (score_head_id, course_class_id, Class_ID, classroom_id, course_id, year_id, term_id, UserID_teacher, score_name, score_full, score_main_id ) VALUES ('null', '" .trim( $_POST ['course_class_id '])."' , '".trim($_POST[' Class_ID '])."' , '".trim($_POST[' classroom_id '])."' , '".trim($_POST[' course_id '])."' , '".trim($_POST[' year_id '])."' , '".trim($_POST[' term_id '])."' , '".trim($_POST[' UserID_teacher '])."' , '".trim($_POST[' score_name '])."' , '".trim($_POST[' score_full '])."' , '".trim($_POST[' score_main_id '])."' );";
12. $objQuery = mysql_query( $strSQL );
13.
14.
15. mysql_close();
16.
17. if ( $objQuery ){
18. echo "<script type='text/javascript'>" ;
19. echo "alert('ADD Data Succesfuly');" ;
20. echo "</script>" ;
21. }
22. else {
23. echo "<script type='text/javascript'>" ;
24. echo "alert('Error back to Update again');" ;
25. echo "</script>" ;
26. }
27. ?>
|
 |
 |
 |
 |
Date :
2020-06-03 10:04:38 |
By :
aumebum-km |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลงให้ถูกไฟล์ครับ อันนี้ไม่เกี่ยวข้องกับภาพแรก
หรือ ถ้ามั่นใจว่าถูก ใช้ตัวแปรชื่ออะไรในการ รับค่าจาก user หรือ insert to db (เกรงว่าจะไม่มี)
|
 |
 |
 |
 |
Date :
2020-06-03 10:33:15 |
By :
PhrayaDev |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

Code (PHP)
01. <table class = "table" >
02. <thead>
03. <tr>
04. <td width= "480" ><div class = "form-group font_head" align= "center" >
05. <label>หัวข้อการบันทึกคะแนน <span class = "style3" >*</span> :</label>
06. </div></td>
07. <td width= "550" ><label><textarea style= "font-size:12px;" class = "form-control" name= "score_name" cols= "35" rows= "4" id= "score_name" ></textarea></label></td>
08. </tr>
09.
10. <tr>
11. <td width= "480" ><div class = "form-group font_head" align= "center" >
12. <label>คะแนนเต็ม <span class = "style3" >*</span> :</label>
13. </div></td>
14. <td width= "550" ><label><input style= "font-size:12px;" class = "form-control" name= "score_full" type= "text" id= "score_full" value= "" /></label></td>
15. </tr>
16.
17. <tr><td colspan= "2" ><div align= "center" >
18.
19. <strong><div class = 'font_head' ><a href= "scoring_system_list.php?course_class_id=<?php echo " ".$_GET['course_class_id']." "; ?>&year_id=<?php echo " ".$_GET['year_id']." "; ?>&term_id=<?php echo $_GET[" term_id "]; ?>" ><button type= "button" class = "btn btn-primary font_head" > กลับ </button></a>
20. <button type= "submit" name= "Submit" id= "Submit" class = "btn btn-primary font_head" > บันทึก </button>
21. </div></strong>
22.
23. </div></td></tr>
24. </thead>
25. </table>
Code (PHP)
01. <?php
02. session_start();
03. ?>
04.
05. <?php
06.
07.
08.
09. include ( "../../config.php" );
10.
11. $strSQL = "INSERT INTO score_head_Secondary (score_head_id, course_class_id, Class_ID, classroom_id, course_id, year_id, term_id, UserID_teacher, score_name, score_full, score_main_id ) VALUES ('null', '" .trim( $_POST ['course_class_id '])."' , '".trim($_POST[' Class_ID '])."' , '".trim($_POST[' classroom_id '])."' , '".trim($_POST[' course_id '])."' , '".trim($_POST[' year_id '])."' , '".trim($_POST[' term_id '])."' , '".trim($_POST[' UserID_teacher '])."' , '".trim($_POST[' score_name '])."' , '".trim($_POST[' score_full '])."' , '".trim($_POST[' score_main_id '])."' );";
12. $objQuery = mysql_query( $strSQL );
13.
14.
15. mysql_close();
16.
17. if ( $objQuery ){
18. echo "<script type='text/javascript'>" ;
19. echo "alert('ADD Data Succesfuly');" ;
20. echo "</script>" ;
21. }
22. else {
23. echo "<script type='text/javascript'>" ;
24. echo "alert('Error back to Update again');" ;
25. echo "</script>" ;
26. }
27. ?>
|
 |
 |
 |
 |
Date :
2020-06-03 13:13:34 |
By :
aumebum-km |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อย่างแรกเลย ควรเลิกใช้ฟังก์ชั่นที่เขาประกาศให้เลิกใช้มาเป็นสิบปีได้แล้ว
https://www.thaicreate.com/php/forum/095986.html
อย่างที่สอง แน่ใจหรือว่าไม่ได้เกิดจากการ submit form ซ้ำดับเบิ้ล?
วิธีแก้ให้ลองศึกษาการทำ nonce (number once) โดยสร้างค่าสุ่มใส่ form input hidden และใน session ให้ตรงกัน พอตอนรับค่าก็เช็คให้ตรงกันจึงค่อยทำงาน และในหน้ารับค่าฟอร์มก็ล้างค่า nonce ใน session ทุกครั้งเพื่อให้ไปเรียกเอาค่าใหม่เสมอ กันคลิก submit ซ้ำได้
Update:
ยิ่งมาดูโค้ด insert แล้วยิ่งอยากเอาสักสิบมือก่ายหน้าผาก 
คุณจขกท.ไม่ได้ป้องกัน mysql injection เลยสักนิดเดียว. ดังนั้นถ้านี่เป็นงานเขียนใหม่ ไม่ใช่แก้งานเก่าโบราณนานมา ก็ควรเขียนซะใหม่เลย ไปใช้ฟังก์ชั่นใหม่ๆที่ไม่ใช่ mysql_xxx ทั้งหลาย แล้วก็ศึกษาเรื่อง mysql injection ด้วยเพราะอันตรายของมันไม่ใช่ระดับง่อยๆ
|
ประวัติการแก้ไข 2020-06-04 12:29:23
 |
 |
 |
 |
Date :
2020-06-03 23:31:40 |
By :
mr.v |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมว่าเกิดจากการรีเฟสหน้าหรือป่าวทำให้ข้อมูลเข้าซ้ำ ลองบันทึกแล้วกด F5 ดูครับ
|
 |
 |
 |
 |
Date :
2020-06-08 10:03:21 |
By :
POOK |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|