ช่วยดู code คำสั่ง insert 2 table ให้หน่อยครับ ผิดตรงไหนทำไม insert เข้าแค่ 1 ตาราง

ถาม-ตอบ แนะนำไอเดียว โค้ดตัวอย่าง แนวทาง วิธีแก้ปัญหา ช่วยดู code คำสั่ง insert 2 table ให้หน่อยครับ ผิดตรงไหนทำไม insert เข้าแค่ 1 ตาราง

ช่วยดู code คำสั่ง insert 2 table ให้หน่อยครับ ผิดตรงไหนทำไม insert เข้าแค่ 1 ตาราง
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
if ($conn->query($sql) === TRUE) {
     $id = $conn->insert_id;
    $conn->query("INSERT INTO invoice
                   (id, money_id)
              VALUES($id, $money_id)");
 
     $invoice_id = $conn->insert_id;
     $conn->query("UPDATE cutmoney 
                    SET invoice_id = '$invoice_id'
                 WHERE money_id = $money_id");
 
 
    $msg = "<div class='alert alert-success'>
      <span class='glyphicon glyphicon-info-sign'></span> &nbsp; s] จองรถตู้สำเร็จ โปรดรอการตอบรับทาง Email !
     </div>";
}
 
 
 
else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}




ความหมายที่ผมต้องการคือ หลังจากทำการ insert ข้อมูลเข้าที่ตาราง cutmoney แล้ว จะให้มัน insert ฟิว money_id , id ไปที่ table invoice หลังinsert ใน invoice แล้วให้กลับมาอัพเดทที่ตาราง cutmoney โดยอัพเดท invoice_id  เข้าไป ที่ id money ที่เราทำรายการครั้งล่าสุด  

 

แต่ผมทำมัน insert เข้าแค่ cutmoney ครับ และมันไม่ insert ตารางอื่นและไม่วนกลับมาอัพเดท เลยครับ 



Sun Yuttakarn 13-01-2019 13:57:27

คำแนะนำ และการใช้งาน

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


  • ถาม-ตอบ กรุณา ล็อกอินเข้าระบบ
  • เปลี่ยน


    ( หรือ เข้าใช้งานผ่าน Social Login )

 ความคิดเห็นที่ 1
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
<?php
include "db.php";
//table invoice
$id = $_POST['id'];
 
$invoice_id = $_POST['invoice_id'];
$cut_detail = $_POST['cut_detail'];
$cut_money = $_POST['cut_money'];
$money_id = $_POST['money_id'];
 
   
echo $sql = "INSERT INTO cutmoney (money_id,cut_money,cut_detail,id,cut_date)
VALUES ('$money_id','$cut_money', '$cut_detail',' $id ', Now())";
 
 
//คำสั่ง insert  2 ตาราง
if ($conn->query($sql) === TRUE) {
     $id = $conn->insert_id;
    $conn->query("INSERT INTO invoice
                   (id, money_id)
              VALUES($id, $money_id)");
 
     $invoice_id = $conn->insert_id;
     $conn->query("UPDATE cutmoney 
                    SET invoice_id = '$invoice_id'
                 WHERE money_id = $money_id");
 
 
    $msg = "<div class='alert alert-success'>
      <span class='glyphicon glyphicon-info-sign'></span> &nbsp; s] จองรถตู้สำเร็จ โปรดรอการตอบรับทาง Email !
     </div>";
}
 
 
 
else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
 
 
 
$conn->close();
 
 
 
?>


Sun Yuttakarn 13-01-2019 13:58
 ความคิดเห็นที่ 2
ตัวแปรใช้ $id แต่ตอนเรียกใข้กลับกำหนดเป็น $money.id


ninenik 13-01-2019
 ความคิดเห็นที่ 3
ขอบคุณครับไล่หาเองจนเจอแล้ว   ผิดเองเต็มๆครับ  แฮะๆๆ


Sun Yuttakarn 14-01-2019 12:52
 ความคิดเห็นที่ 4

สอบถามเพิ่มเติมครับ การ ทำ ajax Alert ผลลัพธ์
ส่วนของ ajax ส่งค่าไปยัง mysqli
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
$(document).ready(function() {
    $(document).on('click','#save',function(){
       var cut_detail = $("#cut_detail").val();
       var cut_money = $("#cut_money").val();
       var user_id = $("#user_id").val();
       var money_id = $("#money_id").val();
       var invoice_id = $("#invoice_id").val();
        if (cut_detail == "" ) {
            $("#cut_detail").html(" Q");           
        }else if(cut_money == "") {
            $("#cut_money").html("q");
        } else{
            $.ajax ({
                url:"save_data.php",
                type: "post",
                data:{cut_detail:cut_detail,cut_money:cut_money,user_id:user_id, money_id:money_id , invoice_id:invoice_id},
                success:function(data){
                    alert("Update");
                    console.log(data); //Shows the correct piece of information
                    location.reload();
                     
                }
            });
        }
    });
 
});


ตรง บรรทัด 
1
2
3
4
success:function(data){
                    alert("Update");
                    console.log(data); //Shows the correct piece of information
                    location.reload();

ผมอยากให่้มันเรียก Javascript จากด่่านล่างมาใช้ต้องกำหนดยังไงครับ

1
2
3
4
5
6
7
// Success
       $('#noty_success').on('click', function() {
           new Noty({
               text: 'You successfully read this important alert message.',
               type: 'success'
           }).show();
       });


ขอบคุณครับ


Sun Yuttakarn 14-01-2019 13:00
1






เว็บไซต์ของเราให้บริการเนื้อหาบทความสำหรับนักพัฒนา โดยพึ่งพารายได้เล็กน้อยจากการแสดงโฆษณา โปรดสนับสนุนเว็บไซต์ของเราด้วยการปิดการใช้งานตัวปิดกั้นโฆษณา (Disable Ads Blocker) ขอบคุณครับ