รบกวนสอบถามเรื่อง insert , update

ถาม-ตอบ แนะนำไอเดียว โค้ดตัวอย่าง แนวทาง วิธีแก้ปัญหา รบกวนสอบถามเรื่อง insert , update

รบกวนสอบถามเรื่อง insert , update
<?php include("Connections/connect.php"); ?>
<?
 $crews =$_GET["id"];
 
 if(!empty($crews))
 {
 $sql = "select*from crew_educations where ce_fk_cp_id = '".$crews."' and ce_education = 'ประถมศึกษา'";
 $result = mysql_query($sql);
 $row = mysql_fetch_array($result);
 
 $sql1 = "select*from crew_educations where ce_fk_cp_id = '".$crews."' and ce_education = 'มัธยมศึกษา'";
 $result1 = mysql_query($sql1);
 $row1 = mysql_fetch_array($result1);
 
 $sql2 = "select*from crew_educations where ce_fk_cp_id = '".$crews."' and ce_education = 'อาชีวะศึกษา'";
 $result2 = mysql_query($sql2);
 $row2 = mysql_fetch_array($result2);
 
 $sql3 = "select*from crew_educations where ce_fk_cp_id = '".$crews."' and ce_education = 'อนุปริญญา'";
 $result3 = mysql_query($sql3);
 $row3 = mysql_fetch_array($result3);
 
 $sql4 = "select*from crew_educations where ce_fk_cp_id = '".$crews."' and ce_education = 'ปริญญาตรี'";
 $result4 = mysql_query($sql4);
 $row4 = mysql_fetch_array($result4);
 
 $sql5 = "select*from crew_educations where ce_fk_cp_id = '".$crews."' and ce_education = 'อื่นๆ'";
 $result5 = mysql_query($sql5);
 $row5 = mysql_fetch_array($result5);
 }
 if(!empty($_POST["submit2"])&& $_POST["submit2"]=="Save")
 {
  $sql = "insert into crew_educations(ce_education,ce_institute_name,ce_institute_province,ce_institute_country,ce_year_start,ce_year_finish,ce_degree,ce_fk_cp_id,ce_status)
    values('ประถมศึกษา','".$_POST["ce_institute_name"]."','".$_POST["ce_institute_province"]."','".$_POST["ce_institute_country"]."','".$_POST["ce_year_start"]."',
      '".$_POST["ce_year_finish"]."','".$_POST["ce_degree"]."','$crews','1')";
  
  mysql_query($sql);
  
  $sql1 = "insert into crew_educations(ce_education,ce_institute_name,ce_institute_province,ce_institute_country,ce_year_start,ce_year_finish,ce_degree,ce_fk_cp_id,ce_status)
    values('มัธยมศึกษา','".$_POST["ce_institute_name1"]."','".$_POST["ce_institute_province1"]."','".$_POST["ce_institute_country1"]."','".$_POST["ce_year_start1"]."',
      '".$_POST["ce_year_finish1"]."','".$_POST["ce_degree1"]."','$crews','1')";
  
  mysql_query($sql1);
 }
 else{
  $sql = "update crew_educations set ce_education='".$_POST["ce_education"]."',ce_institute_name='".$_POST["ce_institute_name"]."',ce_institute_province='".$_POST["ce_institute_province"]."',
    ce_institute_country='".$_POST["ce_institute_country"]."',ce_year_start='".$_POST["ce_year_start"]."',ce_year_finish='".$_POST["ce_year_finish"]."',ce_degree='".$_POST["ce_degree"]."'
    where ce_fk_cp_id='".$crews."'";
  mysql_query($sql);
  
  $sql1 = "update crew_educations set ce_education='".$_POST["ce_education1"]."',ce_institute_name='".$_POST["ce_institute_name1"]."',ce_institute_province='".$_POST["ce_institute_province1"]."',
    ce_institute_country='".$_POST["ce_institute_country1"]."',ce_year_start='".$_POST["ce_year_start1"]."',ce_year_finish='".$_POST["ce_year_finish1"]."',ce_degree='".$_POST["ce_degree1"]."'
    where ce_fk_cp_id='".$crews."'";
  mysql_query($sql1);
   
  /*$sql2 = "insert into crew_educations(ce_education,ce_institute_name,ce_institute_province,ce_institute_country,ce_year_start,ce_year_finish,ce_degree,ce_fk_cp_id,ce_status)
    values('อาชีวะศึกษา','".$_POST["ce_institute_name2"]."','".$_POST["ce_institute_province2"]."','".$_POST["ce_institute_country2"]."','".$_POST["ce_year_start2"]."',
      '".$_POST["ce_year_finish2"]."','".$_POST["ce_degree2"]."','$crews','1')";
  
  mysql_query($sql2); 
  
  $sql3 = "insert into crew_educations(ce_education,ce_institute_name,ce_institute_province,ce_institute_country,ce_year_start,ce_year_finish,ce_degree,ce_fk_cp_id,ce_status)
    values('อนุปริญญา','".$_POST["ce_institute_name3"]."','".$_POST["ce_institute_province3"]."','".$_POST["ce_institute_country3"]."','".$_POST["ce_year_start3"]."',
      '".$_POST["ce_year_finish3"]."','".$_POST["ce_degree3"]."','$crews','1')";
  
  mysql_query($sql3);
  
  $sql4 = "insert into crew_educations(ce_education,ce_institute_name,ce_institute_province,ce_institute_country,ce_year_start,ce_year_finish,ce_degree,ce_fk_cp_id,ce_status)
    values('ปริญญาตรี','".$_POST["ce_institute_name4"]."','".$_POST["ce_institute_province4"]."','".$_POST["ce_institute_country4"]."','".$_POST["ce_year_start4"]."',
      '".$_POST["ce_year_finish4"]."','".$_POST["ce_degree4"]."','$crews','1')";
  
  mysql_query($sql4);
  
  $sql5 = "insert into crew_educations(ce_education,ce_institute_name,ce_institute_province,ce_institute_country,ce_year_start,ce_year_finish,ce_degree,ce_fk_cp_id,ce_status)
    values('".$_POST["ce_education5"]."','".$_POST["ce_institute_name5"]."','".$_POST["ce_institute_province5"]."','".$_POST["ce_institute_country5"]."','".$_POST["ce_year_start5"]."',
      '".$_POST["ce_year_finish5"]."','".$_POST["ce_degree5"]."','$crews','1')";
  
  mysql_query($sql5);*/   
 }
?>
<form action="" method="post">
 <div class="table-responsive">
     <table class="table table-striped table-bordered">
             <tr align="center">
              <td rowspan="2">
                  <h5>ระดับการศึกษา</h5>
                     <h6>Education</h6>
                 </td>
                 <td rowspan="2">
                  <h5>ชื่อสถานบัน</h5>
                     <h6>Name of Institute</h6>
                 </td>
                 <td rowspan="2">
                  <h5>จังหวัด</h5>
                     <h6>Province</h6>
                 </td>
                    <td rowspan="2">
                  <h5>ประเทศ</h5>
                     <h6>Country</h6>
                 </td>
                 <td colspan="2">
                  <h5>ปีการศึกษา</h5>
                     <h6>Year Attended</h6>
                 </td>
                 <td rowspan="2">
                  <h5>วิชาที่ศึกษา/วุฒิที่ได้รับ</h5>
                     <h6>Course Taken/Completed</h6>
                 </td>
             </tr>
             <tr align="center" style="background-color:#f9f9f9;">
                 <td>
                     <h5>จาก</h5>
                     <h6>From</h6>
                  </td>
                  <td>
                     <h5>ถึง</h5>
                     <h6>To</h6>
                  </td>
             </tr>
             <tr>
              <td>
                  <h5>ประถมศึกษา</h5>
                     <h6>Primary</h6>
                 </td>
                 <td>
                  <input type="text" name="ce_institute_name" value="<?=$row["ce_institute_name"]; ?>" class="form-control" placeholder="กรุณากรอกชื่อสถานบัน"/>
                 </td>
                 <td>
                  <input type="text" name="ce_institute_province" value="<?=$row["ce_institute_province"]; ?>" class="form-control" placeholder="กรุณากรอกจังหวัด"/>
                 </td>
                    <td>
                  <input type="text" name="ce_institute_country" value="<?=$row["ce_institute_country"]; ?>" class="form-control" placeholder="กรุณากรอกประเทศ"/>
                 </td>
                 <td>
                  <input type="text" name="ce_year_start" value="<?=$row["ce_year_start"]; ?>" class="form-control" placeholder="กรุณากรอกจากปี"/>
                 </td>
                 <td>
                  <input type="text" name="ce_year_finish" value="<?=$row["ce_year_finish"]; ?>" class="form-control" placeholder="กรุณากรอกถึงปี"/>
                 </td>
                 <td>
                  <input type="text" name="ce_degree" value="<?=$row["ce_degree"]; ?>" class="form-control" placeholder="กรุณากรอกวิชาที่ศึกษา/วุฒิที่ได้รับ"/>
                 </td>
             </tr>
             <tr>
              <td>
                  <h5>มัธยมศึกษา</h5>
                     <h6>Secondary</h6>
                 </td>
                 <td>
                  <input type="text" name="ce_institute_name1" value="<?=$row1["ce_institute_name"]; ?>" class="form-control" placeholder="กรุณากรอกชื่อสถานบัน"/>
                 </td>
                 <td>
                  <input type="text" name="ce_institute_province1" value="<?=$row1["ce_institute_province"]; ?>" class="form-control" placeholder="กรุณากรอกจังหวัด"/>
                 </td>
                    <td>
                  <input type="text" name="ce_institute_country1" value="<?=$row1["ce_institute_country"]; ?>" class="form-control" placeholder="กรุณากรอกประเทศ"/>
                 </td>
                 <td>
                  <input type="text" name="ce_year_start1" value="<?=$row1["ce_year_start"]; ?>" class="form-control" placeholder="กรุณากรอกจากปี"/>
                 </td>
                 <td>
                  <input type="text" name="ce_year_finish1" value="<?=$row1["ce_year_finish"]; ?>" class="form-control" placeholder="กรุณากรอกถึงปี"/>
                 </td>
                 <td>
                  <input type="text" name="ce_degree1" value="<?=$row1["ce_degree"]; ?>" class="form-control" placeholder="กรุณากรอกวิชาที่ศึกษา/วุฒิที่ได้รับ"/>
                 </td>
             </tr>
             <tr>
              <td>
                  <h5>อาชีวะศึกษา</h5>
                     <h6>Vocational</h6>
                 </td>
                 <td>
                  <input type="text" name="ce_institute_name2" value="<?=$row2["ce_institute_name"]; ?>" class="form-control" placeholder="กรุณากรอกชื่อสถานบัน"/>
                 </td>
                 <td>
                  <input type="text" name="ce_institute_province2" value="<?=$row2["ce_institute_province"]; ?>" class="form-control" placeholder="กรุณากรอกจังหวัด"/>
                 </td>
                    <td>
                  <input type="text" name="ce_institute_country2" value="<?=$row2["ce_institute_country"]; ?>" class="form-control" placeholder="กรุณากรอกประเทศ"/>
                 </td>
                 <td>
                  <input type="text" name="ce_year_start2" value="<?=$row2["ce_year_start"]; ?>" class="form-control" placeholder="กรุณากรอกจากปี"/>
                 </td>
                 <td>
                  <input type="text" name="ce_year_finish2" value="<?=$row2["ce_year


Thepaysz 183.89.152.xxx 17-09-2015 15:44:41

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

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


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


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

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


ninenik 183.88.77.xxx 17-09-2015
 ความคิดเห็นที่ 2
พอดีว่า ต้องการให้กรอก insert เวลาไม่มีข้อมูลลงไปในหน้า update ด้วย แล้ว สามารถแก้ไขในหน้านั้นได้ด้วย โดยที่ว่าถ้ามีข้อมูลอยู่แล้ว ไม่ต้อง insert ลงดาต้าเบส ค่ะ แต่ถ้าไม่มีข้อมูลให้ทำการ insert ข้อมูลนั้นๆ โดยที่ไม่ insert ข้อมูลที่มีอยู่แล้วค่ะ แล้วสมารถแก้ไขข้อมูลได้ด้วยค่ะ  ต้องทำยังไงค่ะ พอมีแนวทางไหมค่ะ รบกวนช่วยหน่อยนะค่ะ


thepaysz 183.89.152.xxx 17-09-2015 16:38
 ความคิดเห็นที่ 3
เป็นแนวทาง

if(@mysql_num_rows(@mysql_query("
select * from crew_educations where ce_fk_cp_id = '".$crews."' and ce_education = 'ประถมศึกษา'
"))>0){
    @mysql_query("
        // คำสั่่ง sql สำหรับ UPDATE
    ");
}else{
    @mysql_query("
        // คำสั่ง sql สำหรับ INSERT INTO
    ");    
}


ninenik 183.88.77.xxx 17-09-2015
 ความคิดเห็นที่ 4
ช่วยดูหน่อยค่ะ ต้อง get id จาก table นี้ใส่ช่วงไหนคะ
id auto จาก การ insert ข้อมูลเข้าไป

คือว่าโค้ด สามารถอัพเดตตามที่ต้องการได้แล้ว แต่ต้องพิม where field id auto มา  คือ 

if(@mysql_num_rows(@mysql_query("
  select * from crew_educations where ce_fk_cp_id = '".$crews."' and ce_education = 'มัธยมศึกษา'  
 
  
"))>0)
{  
    @mysql_query("
        update crew_educations set ce_education='มัธยมศึกษา',ce_institute_name='".$_POST["ce_institute_name1"]."',ce_institute_province='".$_POST["ce_institute_province1"]."',
  ce_institute_country='".$_POST["ce_institute_country1"]."',ce_year_start='".$_POST["ce_year_start1"]."',ce_year_finish='".$_POST["ce_year_finish1"]."',ce_degree='".$_POST["ce_degree1"]."',
  ce_fk_cp_id='17',ce_status='1' where ce_id = '26'
  
    ");
 @mysql_query("
        update crew_educations set ce_education='ประถมศึกษา',ce_institute_name='".$_POST["ce_institute_name"]."',ce_institute_province='".$_POST["ce_institute_province"]."',
  ce_institute_country='".$_POST["ce_institute_country"]."',ce_year_start='".$_POST["ce_year_start"]."',ce_year_finish='".$_POST["ce_year_finish"]."',ce_degree='".$_POST["ce_degree"]."',
  ce_fk_cp_id='17',ce_status='1' where ce_id = '487'
  
    ");
 
}else{
 
 @mysql_query("
        insert into crew_educations(ce_education,ce_institute_name,ce_institute_province,ce_institute_country,ce_year_start,ce_year_finish,ce_degree,ce_fk_cp_id,ce_status)
    values('ประถมศึกษา','".$_POST["ce_institute_name"]."','".$_POST["ce_institute_province"]."','".$_POST["ce_institute_country"]."','".$_POST["ce_year_start"]."',
      '".$_POST["ce_year_finish"]."','".$_POST["ce_degree"]."','17','1') ");
      
   @mysql_query("
        insert into crew_educations(ce_education,ce_institute_name,ce_institute_province,ce_institute_country,ce_year_start,ce_year_finish,ce_degree,ce_fk_cp_id,ce_status)
    values('มัธยมศึกษา','".$_POST["ce_institute_name1"]."','".$_POST["ce_institute_province1"]."','".$_POST["ce_institute_country1"]."','".$_POST["ce_year_start1"]."',
      '".$_POST["ce_year_finish1"]."','".$_POST["ce_degree1"]."','17','1') "); 
 
}


อยากถามว่าควรจะใส่ get id auto มายังไงคะ มันถึงจะอัพเดตข้อมูลได้ ใส่ไม่ถูกเลย 

และนี่คือโค้ด input 
<form action="" method="post">
 
 <div class="table-responsive">
     <table class="table table-striped table-bordered">
             <tr align="center">
              <td rowspan="+2">
                  <h5>ระดับการศึกษา</h5>
                     <h6>Education</h6>
                 </td>
                 <td rowspan="2">
                  <h5>ชื่อสถานบัน</h5>
                     <h6>Name of Institute</h6>
                 </td>
                 <td rowspan="2">
                  <h5>จังหวัด</h5>
                     <h6>Province</h6>
                 </td>
                    <td rowspan="2">
                  <h5>ประเทศ</h5>
                     <h6>Country</h6>
                 </td>
                 <td colspan="2">
                  <h5>ปีการศึกษา</h5>
                     <h6>Year Attended</h6>
                 </td>
                 <td rowspan="2">
                  <h5>วิชาที่ศึกษา/วุฒิที่ได้รับ</h5>
                     <h6>Course Taken/Completed</h6>
                 </td>
             </tr>
             <tr align="center" style="background-color:#f9f9f9;">
                 <td>
                     <h5>จาก</h5>
                     <h6>From</h6>
                  </td>
                  <td>
                     <h5>ถึง</h5>
                     <h6>To</h6>
                  </td>
             </tr>
             <tr>
              <td>
                  <h5>ประถมศึกษา</h5>
                     <h6>Primary</h6>
                 </td>
                 <td>
                  <input type="text" name="ce_institute_name" value="<?=$row["ce_institute_name"]; ?>" class="form-control" placeholder="กรุณากรอกชื่อสถานบัน"/>
                 </td>
                 <td>
                  <input type="text" name="ce_institute_province" value="<?=$row["ce_institute_province"]; ?>" class="form-control" placeholder="กรุณากรอกจังหวัด"/>
                 </td>
                    <td>
                  <input type="text" name="ce_institute_country" value="<?=$row["ce_institute_country"]; ?>" class="form-control" placeholder="กรุณากรอกประเทศ"/>
                 </td>
                 <td>
                  <input type="text" name="ce_year_start" value="<?=$row["ce_year_start"]; ?>" class="form-control" placeholder="กรุณากรอกจากปี"/>
                 </td>
                 <td>
                  <input type="text" name="ce_year_finish" value="<?=$row["ce_year_finish"]; ?>" class="form-control" placeholder="กรุณากรอกถึงปี"/>
                 </td>
                 <td>
                  <input type="text" name="ce_degree" value="<?=$row["ce_degree"]; ?>" class="form-control" placeholder="กรุณากรอกวิชาที่ศึกษา/วุฒิที่ได้รับ"/>
                 </td>
             </tr>
             <tr>
              <td>
                  <h5>มัธยมศึกษา</h5>
                     <h6>Secondary</h6>
                 </td>
                 <td>
                  <input type="text" name="ce_institute_name1" value="<?=$row1["ce_institute_name"]; ?>" class="form-control" placeholder="กรุณากรอกชื่อสถานบัน"/>
                 </td>
                 <td>
                  <input type="text" name="ce_institute_province1" value="<?=$row1["ce_institute_province"]; ?>" class="form-control" placeholder="กรุณากรอกจังหวัด"/>
                 </td>
                    <td>
                  <input type="text" name="ce_institute_country1" value="<?=$row1["ce_institute_country"]; ?>" class="form-control" placeholder="กรุณากรอกประเทศ"/>
                 </td>
                 <td>
                  <input type="text" name="ce_year_start1" value="<?=$row1["ce_year_start"]; ?>" class="form-control" placeholder="กรุณากรอกจากปี"/>
                 </td>
                 <td>
                  <input type="text" name="ce_year_finish1" value="<?=$row1["ce_year_finish"]; ?>" class="form-control" placeholder="กรุณากรอกถึงปี"/>
                 </td>
                 <td>
                  <input type="text" name="ce_degree1" value="<?=$row1["ce_degree"]; ?>" class="form-control" placeholder="กรุณากรอกวิชาที่ศึกษา/วุฒิที่ได้รับ"/>
                 </td>
             </tr>
             <tr>

 


thepaysz 183.89.152.xxx 18-09-2015 16:11
 ความคิดเห็นที่ 5
เป็นแนวทาง ขึ้นกับต้องไปประยุกต์เพิ่มเติม 

$data_ce_id = @mysql_result(@mysql_query("
select ce_id from crew_educations where ce_fk_cp_id = '".$crews."' and ce_education = 'มัธยมศึกษา'  
"),0,0);
if(isset($data_ce_id) && $data_ce_id>0){
    @mysql_query("
        update crew_educations set ce_education='มัธยมศึกษา',ce_institute_name='".$_POST["ce_institute_name1"]."',ce_institute_province='".$_POST["ce_institute_province1"]."',
  ce_institute_country='".$_POST["ce_institute_country1"]."',ce_year_start='".$_POST["ce_year_start1"]."',ce_year_finish='".$_POST["ce_year_finish1"]."',ce_degree='".$_POST["ce_degree1"]."',
  ce_fk_cp_id='17',ce_status='1' where ce_id = '".$data_ce_id."'
  
    ");    
}else{
    // INSERT
}

$data_ce_id = @mysql_result(@mysql_query("
select ce_id from crew_educations where ce_fk_cp_id = '".$crews."' and ce_education = 'ประถมศึกษา'  
"),0,0);
if(isset($data_ce_id) && $data_ce_id>0){
 @mysql_query("
        update crew_educations set ce_education='ประถมศึกษา',ce_institute_name='".$_POST["ce_institute_name"]."',ce_institute_province='".$_POST["ce_institute_province"]."',
  ce_institute_country='".$_POST["ce_institute_country"]."',ce_year_start='".$_POST["ce_year_start"]."',ce_year_finish='".$_POST["ce_year_finish"]."',ce_degree='".$_POST["ce_degree"]."',
  ce_fk_cp_id='17',ce_status='1' where ce_id = '".$data_ce_id."'
  
    "); 
}else{
    // INSERT
}


ninenik 183.88.77.xxx 18-09-2015
1






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