2,293   6 แบบนี้ต้องเขียนคำสั่งSQLอย่างไรหรอครับ



phanuwat3291


พลังงานการช่วยเหลือ
( Level 1 )


คะแนนขอบคุณจากสมาชิก
( 0 )


ตอบกระทู้ ( 5 )
เขียนบทความ ( 0 )



Code (SQL)
$sql_r="SELECT * FROM education_level_types ORDER BY education_level_id ASC ";
$query_r=mysql_query($sql_r);

$strSQL = "SELECT * FROM qualification_types ORDER BY qualification_id ASC";
$objQuery = mysql_query($strSQL);





Code (JS)
function CheckValidate()
{

for(i=1;i<=document.frmMain.hdnMaxLine.value;i++)
{

//*** Column 1 ***/
if(eval("document.frmMain.Column1_"+i+".value")=="")
{
alert('กรุณากรอกข้อมูลระดับการศึกษาด้วยค่ะ แถวที่' + i);
eval("document.frmMain.Column1_"+i+".focus();")
return false;
}

//*** Column 2 ***/
if(eval("document.frmMain.Column2_"+i+".value")=="")
{
alert('กรุณากรอกข้อมูลชื่อย่อ ปริญญาด้วยค่ะ.. แถวที่' + i);
eval("document.frmMain.Column2_"+i+".focus();")
return false;
}

//*** Column 3 ***/
if(eval("document.frmMain.Column3_"+i+".value")=="")
{
alert('กรุณากรอกข้อมูลสาขาวิชาเอกด้วยค่ะ..แถวที่ ' + i);
eval("document.frmMain.Column3_"+i+".focus();")
return false;
}

//*** Column 4 ***/
if(eval("document.frmMain.Column4_"+i+".value")=="")
{
alert('กรุณากรอกข้อมูล จบจากสถานศึกษาด้วยค่ะ..แถวที่ ' + i);
eval("document.frmMain.Column4_"+i+".focus();")
return false;
}

//*** Column 5 ***/
if(eval("document.frmMain.Column5_"+i+".value")=="")
{
alert('กรุณากรอกข้อมูล ปี พ.ศ.ที่จบด้วยค่ะ..แถวที่ ' + i);
eval("document.frmMain.Column5_"+i+".focus();")
return false;
}

}

}




function CreateSelectOption(ele)
{
var objSelect = document.getElementById(ele);
var Item = new Option("", "");
objSelect.options[objSelect.length] = Item;
<?php
while($objResult = mysql_fetch_array($objQuery))
{
?>
var Item = new Option("<?php echo $objResult["Initial_th"];?>", "<?php echo $objResult["Qualification_id"];?>");
objSelect.options[objSelect.length] = Item;
<?php
}
?>
}

function CreateSelectOption1(ele)
{
var objSelect = document.getElementById(ele);
var Item = new Option("", "");
objSelect.options[objSelect.length] = Item;
<?php
while($objResult = mysql_fetch_array($query_r))
{
?>
var Item = new Option("<?php echo $objResult["education_level_name"];?>", "<?php echo $objResult["education_level_id"];?>");
objSelect.options[objSelect.length] = Item;
<?php
}
?>
}

function CreateNewRow()
{
var intLine = parseInt(document.frmMain.hdnMaxLine.value);
intLine++;

var theTable = document.getElementById("tbExp");
var newRow = theTable.insertRow(theTable.rows.length)
newRow.id = newRow.uniqueID

var newCell

//*** Column 1 ***//
newCell = newRow.insertCell(0);
newCell.id = newCell.uniqueID;
newCell.setAttribute("class", "form-group");
newCell.innerHTML = "<SELECT NAME=\"Column1_"+intLine+"\" ID=\"Column1_"+intLine+"\" class=\"form-control\"></SELECT>";
//*** Column 2 ***//
newCell = newRow.insertCell(1);
newCell.id = newCell.uniqueID;
newCell.setAttribute("class", "form-group");
newCell.innerHTML = "<SELECT NAME=\"Column2_"+intLine+"\" ID=\"Column2_"+intLine+"\" class=\"form-control\"></SELECT>";

//*** Column 3 ***//
newCell = newRow.insertCell(2);
newCell.id = newCell.uniqueID;
newCell.setAttribute("class", "form-group");
newCell.innerHTML = "<INPUT TYPE=\"TEXT\" NAME=\"Column3_"+intLine+"\" ID=\"Column3_"+intLine+"\" VALUE=\"\" autocomplete=\"off\"class=\"form-control\" >";

//*** Column 4 ***//
newCell = newRow.insertCell(3);
newCell.id = newCell.uniqueID;
newCell.setAttribute("class", "form-group");
newCell.innerHTML = "<INPUT TYPE=\"TEXT\" NAME=\"Column4_"+intLine+"\" ID=\"Column4_"+intLine+"\" VALUE=\"\" autocomplete=\"off\" class=\"form-control\" > ";

//*** Column 5 ***//
newCell = newRow.insertCell(4);
newCell.id = newCell.uniqueID;
newCell.setAttribute("class", "form-group");
newCell.innerHTML = "<INPUT TYPE=\"TEXT\" NAME=\"Column5_"+intLine+"\" ID=\"Column5_"+intLine+"\" VALUE=\"\" autocomplete=\"off\" class=\"form-control\">";

//*** Column 5 ***//
newCell = newRow.insertCell(5);
newCell.id = newCell.uniqueID;
newCell.setAttribute("class", "form-group");
newCell.innerHTML = "<INPUT TYPE=\"hidden\" NAME=\"Column6_"+intLine+"\" ID=\"Column6_"+intLine+"\" VALUE=\"55555\" >";


//*** Create Option ***//
CreateSelectOption1("Column1_"+intLine)
CreateSelectOption("Column2_"+intLine)

document.frmMain.hdnMaxLine.value = intLine;

}

function RemoveRow()
{
intLine = parseInt(document.frmMain.hdnMaxLine.value);
if(parseInt(intLine) > 0)
{
theTable = document.getElementById("tbExp");
theTableBody = theTable.tBodies[0];
theTableBody.deleteRow(intLine);
intLine--;
document.frmMain.hdnMaxLine.value = intLine;
}
}







Code (CODE)
<section class="content-header">
<!-- Nav tabs -->
<div class="list-group">

<a href="#" class="list-group-item active"><i class="glyphicon glyphicon-education
"></i>&nbsp;&nbsp;บันทึกประวัติการศึกษา</a>
<a href="#"onclick="alert('กรุณาบันทึกข้อมูลประวัติการศึกษาก่อนค่ะ');" class="list-group-item "><i class="glyphicon glyphicon-th-list
"></i>&nbsp;&nbsp;รายละเอียดประวัติการศึกษา</a>
<a href="../form_add_per/index.php" class="list-group-item ">
<i class="glyphicon glyphicon-user
"></i>&nbsp;&nbsp;บันทึกประวัติส่วนตัว
</a>
</div>


</section>

<div class="container-fulid" >
<div class="panel panel-default">
<div class="panel-heading">
<h3><i class="glyphicon glyphicon-education"></i>&nbsp; บันทึกประวัติการศึกษา</h3>
</div>
<div class="panel-body">
<div class="container-fulid">
<form name="frmMain" action="detail.php" method="get" OnSubmit="return CheckValidate();">

<input type="hidden" name="hdnMaxLine" value="0">
<button name="btnAdd" type="button" id="btnAdd" onClick="CreateNewRow();" class="btn btn-primary"><i class="glyphicon glyphicon-plus"></i>&nbsp;เพิ่ม</button>
<button name="btnDel" type="button" id="btnDel" onClick="RemoveRow();" class="btn btn-danger"><i class="glyphicon glyphicon-minus"></i>&nbsp;ลบ</button>
<div class="table-responsive">
<table class="table" id="tbExp">
<thead>
<tr >
<th>ระดับการศึกษา</th>
<th>ชื่อย่อ ปริญญา</th>
<th>สาขาวิชาเอก</th>
<th>จบจากสถานศึกษา</th>
<th>ปี พ.ศ.ที่จบ</th>
</tr>
</thead>
<tr>

</tr>

</table>
</div>





<div class="container-fuid">
<div class="row">
<div class="col-md-3 col-md-push-3">
<div class="row"></div>
<button type="submit" class="btn btn-block btn-success" ><i class="glyphicon glyphicon-floppy-disk"></i> บันทึก</button></div>
<div class="col-md-3 col-md-push-3">
<button type="reset" class="btn btn-block btn-danger" ><i class="glyphicon glyphicon-floppy-remove"></i> ล้างข้อมูล</button></div>
</div>

</div>

</div>

</div>

</form>

</div>

</div>

<!-- panel default -->
</div>
<!-- container fulid -->

</form>

</div>
</div>
</div>

</div>

</div>

</div>
ความคิดเห็นที่ 1
อ้างอิงความเห็น


bamossza


พลังงานการช่วยเหลือ
( Level 3 )


คะแนนขอบคุณจากสมาชิก
( 16 )


ตอบกระทู้ ( 112 )
เขียนบทความ ( 28 )

ลองใช้การตั้งชื่อแบบ Array ครับ
หลังจากกดปุ่ม submit form (PHP)




$count = count($_POST["Column1"]); //เช็คก่อนว่ามีกี่แถว

if($count > 0){ //เช็คว่ามีข้อมูลทั้งหมดกี่แถว

   for ($i=0; $i < $count; $i++) {

     $sql = "
         INSERT INTO table(Column1, Column2, Column3, Column4, Column5)
         VALUES(
         '{$_POST["Column1"][$i]}'
         ,  '{$_POST["Column2"][$i]}'
         , '{$_POST["Column3"][$i]}'
         , '{$_POST["Column4"][$i]}'
         , '{$_POST["Column5"][$i]}'
         )
      ";

     query($sql);
   
   }//end loop
}//end if




HTML FORM(CODE)
<input name="Column1[]" />
<input name="Column2[]" />
<input name="Column3[]" />
<input name="Column4[]" />
<input name="Column5[]" />


ลองไปประยุทธ์อีกทีครับ แบบนี้ง่ายด้วย
ความคิดเห็นที่ 2
อ้างอิงความเห็น


phanuwat3291


พลังงานการช่วยเหลือ
( Level 1 )


คะแนนขอบคุณจากสมาชิก
( 0 )


ตอบกระทู้ ( 5 )
เขียนบทความ ( 0 )

Code (PHP)
echo $count = count($_POST["Column1_"]); //เช็คก่อนว่ามีกี่แถว

if($count > 0){ //เช็คว่ามีข้อมูลทั้งหมดกี่แถว

for ($i=1; $i <= $count; $i++) {

echo $sql = "
INSERT INTO table(Column1, Column2, Column3, Column4, Column5)
VALUES(
'{$_POST["Column1_"][$i]}'
, '{$_POST["Column2_"][$i]}'
, '{$_POST["Column3_"][$i]}'
, '{$_POST["Column4_"][$i]}'
, '{$_POST["Column5_"][$i]}'
)
";


$query= mysql_query($sql);
if ( $query) {
echo "<script>alert('บันทึกสำเร็จ');</script>";
# code...
}
}//end loop
}//end if








ข้อมูลเข้าแล้วคับแต่ยังไม่บันทึกคับ 


ความคิดเห็นที่ 3
อ้างอิงความเห็น


bamossza


พลังงานการช่วยเหลือ
( Level 3 )


คะแนนขอบคุณจากสมาชิก
( 16 )


ตอบกระทู้ ( 112 )
เขียนบทความ ( 28 )

อ้างอิงจากความเห็นที่ 2


แล้วเปลี่ยนชื่อ table และColumn ให้ตรงกับ Database ละยังครับ
***ถ้าดูจากโค้ด มันทำงานได้แล้วนะ ที่เหลือ ชื่อ table และColumn ล้วนๆ 
ดัดแปลงนิ๊ดหน่อยก้ได้ละ
ความคิดเห็นที่ 4
อ้างอิงความเห็น


phanuwat3291


พลังงานการช่วยเหลือ
( Level 1 )


คะแนนขอบคุณจากสมาชิก
( 0 )


ตอบกระทู้ ( 5 )
เขียนบทความ ( 0 )

ขอบคุณครับพี่ ^^  
ความคิดเห็นที่ 5
อ้างอิงความเห็น


bamossza


พลังงานการช่วยเหลือ
( Level 3 )


คะแนนขอบคุณจากสมาชิก
( 16 )


ตอบกระทู้ ( 112 )
เขียนบทความ ( 28 )

อ้างอิงจากความเห็นที่ 4


จากที่ดูโค้ดไป echo ทำไมครับ ทำไมไม่ query เลย 

หรือว่าตอนนี้ทำงานได้แล้วครับ cool
ความคิดเห็นที่ 6
อ้างอิงความเห็น


phanuwat3291


พลังงานการช่วยเหลือ
( Level 1 )


คะแนนขอบคุณจากสมาชิก
( 0 )


ตอบกระทู้ ( 5 )
เขียนบทความ ( 0 )

ลองทดสอบเฉยๆครับพี่ laughing

แสดงความเห็น

อัพโหลดรูปภาพ..คลิก *** เพื่อความปลอดภัยในด้านสิทธิ ไม่อนุญาติให้นำลิ้งรูปภาพจากที่อื่นมาแปะ ถ้าพบเจอ ลบกระทู้ ทันที ***

Back to Top