 |
[PHP]
insert ค่าเข้ามาในดาต้าเบตแล้วไม่เป็นภาษาไทยค่ะ สอบถามรายละเอียดเล็กๆน้อยๆ |
|
 |
|
|
 |
 |
|
ตามหัวข้อเลยค่ะ
ตั้งค่าใน database แบบนี้

หลังจาก insert

อันนี้หน้าที่ไว้ใช้ insert 2 ตารางใน 1 ฟอร์ม
Code (PHP)
01. <?php
02. require 'connectdb.php' ;
03.
04. $login_username = $_POST [ 'username' ];
05. $login_password = $_POST [ 'password' ];
06. $login_email = $_POST [ 'email' ];
07. $firstname = $_POST [ 'firstname' ];
08. $last_name = $_POST [ 'lastname' ];
09. $nickname = $_POST [ 'nickname' ];
10. $address = $_POST [ 'address' ];
11. $phone = $_POST [ 'phone' ];
12.
13.
14. $salt = 'tikde78uj4ujuhlaoikiksakeidke' ;
15. $hash_login_password = hash_hmac( 'sha256' , $login_password , $salt );
16.
17.
18. mysqli_query( "SET character_set_results=utf8" );
19. mysqli_query( "SET character_set_client=utf8" );
20. mysqli_query( "SET character_set_connection=utf8" );
21.
22.
23. $query = "INSERT INTO tb_login (login_username,login_password,login_email) VALUES ('$login_username','$hash_login_password','$login_email')" ;
24. $result = mysqli_query( $dbcon , $query );
25. $query2 = "INSERT INTO student (firstname_s,lastname_s,nickname_s,address_s,phone_s,email_s) VALUES ('$firstname','$last_name','$nickname','$address','$phone','$login_email')" ;
26. $result2 = mysqli_query ( $dbcon , $query2 );
27. if ( $result && $result2 ) {
28. header( "Location: index.php" );
29. } else {
30. echo "เกิดข้อผิดพลาด " . mysqli_error( $dbcon );
31. }
32.
33. mysqli_close( $dbcon );
หน้าที่ไว้เชื่อมใน database
Code (PHP)
01. <?php
02. $dbcon = new mysqli( 'localhost' , 'root' , '' , 'enggoal' );
03. if ( $dbcon ->connect_error){
04. die ( 'Connect Error: ' . $dbcon ->connect_error);
05. }
06.
07. mysqli_query( "SET character_set_results=utf8" );
08. mysqli_query( "SET character_set_client=utf8" );
09. mysqli_query( "SET character_set_connection=utf8" );
10.
11. ?>
Tag : PHP, MySQL
|
ประวัติการแก้ไข 2017-12-20 21:22:01 2017-12-20 21:23:36 2017-12-20 21:39:12 2017-12-21 02:16:48
|
 |
 |
 |
 |
Date :
2017-12-20 21:21:05 |
By :
ny2003 |
View :
1156 |
Reply :
13 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
01. <?php
02. require 'connectdb.php' ;
03.
04. $login_username = $_POST [ 'username' ];
05. $login_password = $_POST [ 'password' ];
06. $login_email = $_POST [ 'email' ];
07. $firstname = $_POST [ 'firstname' ];
08. $last_name = $_POST [ 'lastname' ];
09. $nickname = $_POST [ 'nickname' ];
10. $address = $_POST [ 'address' ];
11. $phone = $_POST [ 'phone' ];
12.
13.
14. $salt = 'tikde78uj4ujuhlaoikiksakeidke' ;
15. $hash_login_password = hash_hmac( 'sha256' , $login_password , $salt );
16.
17.
18.
19.
20.
21.
22.
23. $query = "INSERT INTO tb_login (login_username,login_password,login_email) VALUES ('$login_username','$hash_login_password','$login_email')" ;
24. $result = mysqli_query( $dbcon , $query );
25. $query2 = "INSERT INTO student (firstname_s,lastname_s,nickname_s,address_s,phone_s,email_s) VALUES ('$firstname','$last_name','$nickname','$address','$phone','$email')" ;
26. $result2 = mysqli_query ( $dbcon , $query2 );
27. if ( $result && $result2 ) {
28. header( "Location: index.php" );
29. } else {
30. echo "เกิดข้อผิดพลาด " . mysqli_error( $dbcon );
31. }
32.
33. mysqli_close( $dbcon );
ลองคอมเม้นปิดไว้ครับ แล้วลองเช็คอีกรอบ หรือ เข้าไป ในส่วนของ collection ใน phpmyadmin วา่ เป็น utf8 ไหม
|
 |
 |
 |
 |
Date :
2017-12-20 22:59:53 |
By :
menphurk |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เป็นนะคะ หมายถึง utf8 ในคอลัมน์ใช่ไหมคะ?
|
 |
 |
 |
 |
Date :
2017-12-21 02:20:52 |
By :
ny2003 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตัวอย่างการใช้ mysqli::prepare เพื่อป้องกัน sql injection
Code (PHP)
1. <?php
2.
3. $dbcon = new mysqli( 'localhost' , 'root' , '' , 'enggoal' );
4. if ( $dbcon ->connect_error){
5. die ( 'Connect Error: ' . $dbcon ->connect_error);
6. }
7. $dbcon ->set_charset( "utf-8" );
Code (PHP)
01. <?php
02. require 'connectdb.php' ;
03. $ar = array ( $_POST [ 'username' ], $_POST [ 'password' ], $_POST [ 'email' ], $_POST [ 'firstname' ]
04. , $_POST [ 'lastname' , $_POST [ 'nickname' ], $_POST [ 'address' ], $_POST [ 'phone' ]
05. );
06.
07.
08. $salt = 'tikde78uj4ujuhlaoikiksakeidke' ;
09.
10. $hash_login_password = hash_hmac( 'sha256' , $_POST [ 'password' ], $salt );
11. if ( $stmt = $dbcon ->prepare( 'insert into tb_login set login_username=?, login_password=? , login_email=?' )) {
12. $stmt ->bind_param( "sss" , $_POST [ 'username' ], $hash_login_password , $_POST [ 'email' ]);
13. $rs1 = $stmt ->execute();
14. $stmt ->close();
15. }
16. if (! $rs1 ) die ( "เกิดข้อผิดพลาด <br>" . $dbcon ->error);
17.
18. $ins = 'INSERT INTO student set firstname_s=?, lastname_s=?, nickname_s=?, address_s=?, phone_s=?, email_s=?' ;
19. if ( $stmt = $dbcon ->prepare( $ins )) {
20. $stmt ->bind_param( "ssssss" ,
21. $_POST [ 'firstname' ], $_POST [ 'last_name' ], $_POST [ 'nickname' ]
22. , $_POST [ 'address' ], $_POST [ 'phone' ], $_POST [ 'email' ]
23. );
24. $rs2 = $stmt ->execute();
25. $stmt ->close();
26. }
27. if ( $rs2 ) {
28. header( "Location: index.php" );
29. } else {
30. die ( "เกิดข้อผิดพลาด<br>" . $dbcon ->error);
31. }
ส่วนที่เป็นปัญหาของเจ้าของกระทู้ น่าจะมาจากบันทัดที่ 7 ใน connectdb.php
ตัวคิวรี่ ไม่มี link
หรือจาก html ลองเช็คส่วนหัวของ html ดูว่ามีกำหนดภาษา คล้ายแบบนี้ไหม
Code (PHP)
01. <!doctype html>
02. <html>
03. <head>
04. <title>test ภาษาไทย</title>
05. <meta charset= "utf-8" >
06. </head>
07. <body>
08. <p>สวัสดีครับ รักทุกคนนะครับ</p>
09. </body>
10. </html>
|
ประวัติการแก้ไข 2017-12-21 08:24:59
 |
 |
 |
 |
Date :
2017-12-21 08:20:41 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองทำแล้ว ตอนนี้กลายเป็นว่าในตารางนักเรียนที่อยากให้ข้อมูล insert เข้าไปก็ไม่ insert ให้ แต่ในตาราง user ก็ใช้ได้ ตอนนี้เปลี่ยน collationตารางเป็นแบบ utf8_unicode_ci ในคอลัมน์ก็เปลี่ยนหมดแล้ว ใช้โค้ดเดิมควบกันไปก็ insert ในตารางนักเรียนได้ แต่ภาษาก็ยึกยือเหมือนเดิมเลยค่ะ
|
 |
 |
 |
 |
Date :
2017-12-21 18:36:54 |
By :
ny2003 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แก้ไขยังไงเอาโค๊ดมาดูหน่อยครับ
|
 |
 |
 |
 |
Date :
2017-12-21 18:52:27 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
register.php
Code (PHP)
01. <!doctype html>
02. <html>
03. <head>
04. <meta charset= "UTF-8" >
05. </head>
06. <body>
07. <?php
08. require 'connectdb.php' ;
09. $ar = array ( $_POST [ 'username' ], $_POST [ 'password' ], $_POST [ 'email' ], $_POST [ 'firstname' ]
10. , $_POST [ 'lastname' ], $_POST [ 'nickname' ], $_POST [ 'address' ], $_POST [ 'phone' ]
11. );
12.
13.
14. $salt = 'tikde78uj4ujuhlaoikiksakeidke' ;
15. $hash_login_password = hash_hmac( 'sha256' , $_POST [ 'password' ], $salt );
16. if ( $stmt = $dbcon ->prepare( 'insert into tb_login set login_username=?, login_password=? , login_email=?' )) {
17. $stmt ->bind_param( "sss" , $_POST [ 'username' ], $hash_login_password , $_POST [ 'email' ]);
18. $rs1 = $stmt ->execute();
19. $stmt ->close();
20. }
21. if (! $rs1 ) die ( "เกิดข้อผิดพลาด <br>" . $dbcon ->error);
22. $ins = 'INSERT INTO student set firstname_s=?, lastname_s=?, nickname_s=?, address_s=?, phone_s=?, email_s=?' ;
23. if ( $stmt = $dbcon ->prepare( $ins )) {
24. $stmt ->bind_param( "ssssss" ,
25. $_POST [ 'firstname' ], $_POST [ 'last_name' ], $_POST [ 'nickname' ]
26. , $_POST [ 'address' ], $_POST [ 'phone' ], $_POST [ 'email' ]
27. );
28. $rs2 = $stmt ->execute();
29. $stmt ->close();
30. }
31. if ( $rs2 ) {
32. header( "Location: index.php" );
33. } else {
34. die ( "เกิดข้อผิดพลาด<br>" . $dbcon ->error);
35. }
36. ?>
37. </body>
38. </html>
connectdb.php
Code (PHP)
01. <?php
02. $dbcon = new mysqli( 'localhost' , 'root' , '' , 'enggoal' );
03. if ( $dbcon ->connect_error){
04. die ( 'Connect Error: ' . $dbcon ->connect_error);
05. }
06.
07. $dbcon ->set_charset( "utf-8" );
08.
09. ?>
หรือตรงdocument ต้องใส่
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
แบบนี้คะ?
พอใส่ข้อมูลแล้วมันก็ขึ้นเกิดข้อผิดพลาดให้อ่ะค่ะ แต่ในตาราง tb_login มันขึ้นให้นะคะ แต่ตาราง student มันไม่ขึ้นข้อมูลให้เลย
|
 |
 |
 |
 |
Date :
2017-12-21 19:17:37 |
By :
ny2003 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
register.php ไม่ต้องใส่ <!doctype> ครับ เป็น ส่วนของ server ที่รับ request
ที่ต้องตรวจสอบคือ ไฟล์ ที่เอาข้อมูลเข้า และ ส่ง request มายัง register.php ครับ
ทดสอบข้อมูลใน register.php ว่ามีอะไรส่งมาบ้าง
Code (PHP)
01. <?php
02. require 'connectdb.php' ;
03.
04.
05.
06.
07.
08.
09. var_dump( $_POST ); exit ;
10.
11.
12. $salt = 'tikde78uj4ujuhlaoikiksakeidke' ;
13. $hash_login_password = hash_hmac( 'sha256' , $_POST [ 'password' ], $salt );
14. if ( $stmt = $dbcon ->prepare( 'insert into tb_login set login_username=?, login_password=? , login_email=?' )) {
15. $stmt ->bind_param( "sss" , $_POST [ 'username' ], $hash_login_password , $_POST [ 'email' ]);
16. $rs1 = $stmt ->execute();
17. $stmt ->close();
18. }
19. if (! $rs1 ) die ( "เกิดข้อผิดพลาด <br>" . $dbcon ->error);
20. $ins = 'INSERT INTO student set firstname_s=?, lastname_s=?, nickname_s=?, address_s=?, phone_s=?, email_s=?' ;
21. if ( $stmt = $dbcon ->prepare( $ins )) {
22. $stmt ->bind_param( "ssssss" ,
23. $_POST [ 'firstname' ], $_POST [ 'last_name' ], $_POST [ 'nickname' ]
24. , $_POST [ 'address' ], $_POST [ 'phone' ], $_POST [ 'email' ]
25. );
26. $rs2 = $stmt ->execute();
27. $stmt ->close();
28. }
29. if ( $rs2 ) {
30. header( "Location: index.php" );
31. } else {
32. die ( "เกิดข้อผิดพลาด<br>" . $dbcon ->error);
33. }
|
 |
 |
 |
 |
Date :
2017-12-21 19:49:08 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขึ้นแบบนี้ค่ะ
array(9) { ["firstname"]=> string(12) "ทิตา" ["lastname"]=> string(21) "สุขเสรี" ["nickname"]=> string(6) "ทิ" ["address"]=> string(22) "289 สา่วสา" ["phone"]=> string(11) "09878374629" ["username"]=> string(2) "ti" ["password"]=> string(2) "ti" ["email"]=> string(12) "ti@gmail.com" ["submit"]=> string(15) "สมัคร" }
รับค่ามาจาก frm_register.php ที่มีแค่กล่องที่ใช้ input เฉยๆอ่ะค่ะ ส่วนวิธีการข้างในก็เอามาทำใน register.php
|
 |
 |
 |
 |
Date :
2017-12-21 20:00:32 |
By :
ny2003 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
$_POST['last_name'] สกดผิด
ปล. อย่าลืมเอาบันทัด 9 var_dump ออกด้วยนะครับ 5555
|
ประวัติการแก้ไข 2017-12-21 21:08:46
 |
 |
 |
 |
Date :
2017-12-21 21:04:26 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พอกดสมัครแล้วขึ้นว่าเกิดข้อผิดพลาดอ่ะค่ะ
|
ประวัติการแก้ไข 2017-12-22 11:50:00
 |
 |
 |
 |
Date :
2017-12-22 11:49:37 |
By :
ny2003 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
และ เออเร่อร์อื่นๆ ล่ะครับ มันต้องขึ้น อย่างอื่นมาด้วย
อันนี้เอาไปทดลองทำ ก็ไม่เห็นจะมี error อะไรน่ะครับ
Code (PHP)
01. <?php
02.
03. $dbcon = new mysqli( 'localhost' , 'test' , 'test' , 'test' );
04. if ( $dbcon ->connect_error){
05. die ( 'Connect Error: ' . $dbcon ->connect_error);
06. }
07. $dbcon ->set_charset( "utf-8" );
08. $ar = $_POST ;
09.
10.
11. $salt = 'tikde78uj4ujuhlaoikiksakeidke' ;
12. $hash_login_password = hash_hmac( 'sha256' , $ar [ 'password' ], $salt );
13.
14. $stmt = $dbcon ->prepare( 'insert into tb_login set login_username=?, login_password=? , login_email=?' );
15. $stmt ->bind_param( "sss" , $ar [ 'username' ], $hash_login_password , $ar [ 'email' ]);
16.
17. if (! $stmt ->execute()) die ( "เกิดข้อผิดพลาด 1 <br>" . print_r( $stmt ->error_list,true));
18. $stmt ->close();
19.
20. $ins = 'INSERT INTO student set firstname_s=?, lastname_s=?, nickname_s=?, address_s=?, phone_s=?, email_s=?' ;
21. $stmt = $dbcon ->prepare( $ins );
22. $stmt ->bind_param( "ssssss" , $ar [ 'firstname' ], $ar [ 'lastname' ], $ar [ 'nickname' ], $ar [ 'address' ], $ar [ 'phone' ], $ar [ 'email' ]);
23.
24. if ( ! $stmt ->execute()) die ( "เกิดข้อผิดพลาด 2 <br>" . print_r( $stmt ->error_list,true));
25. $stmt ->close();
26.
27. echo 'complete' ;
28.
|
 |
 |
 |
 |
Date :
2017-12-22 15:15:21 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ได้แล้วค่ะๆ
|
 |
 |
 |
 |
Date :
2017-12-22 18:27:20 |
By :
ny2003 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|