Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > PHP > PHP Forum > ขอถามหน่อยครับ เราจะเก็บค่าใน textbox ลง DB ยังไง ถ้าเป็น ตารางที่เพิ่มแถวและลบแถวได้ ในลักษณะ นี้



 

ขอถามหน่อยครับ เราจะเก็บค่าใน textbox ลง DB ยังไง ถ้าเป็น ตารางที่เพิ่มแถวและลบแถวได้ ในลักษณะ นี้

 



Topic : 100683



โพสกระทู้ ( 184 )
บทความ ( 0 )



สถานะออฟไลน์




จะเก็บ ค่ายังไงคับ ไม่ให้ แต่ละ แถว ซ้ำ กัน แล้วในหน้าที่เราจะ insert ข้อมูล ควร เอาค่า ตัวไหน insert ขอบคุณครับ

Code (PHP)
      <script type="text/javascript">
            $(function(){
	$("#addRow").click(function(){
		var NR ="";   NR="<tr>";  
		var chk=0;
		NR+="<td width=\"60\">"; NR+="<input type=\"text\" name=\"Scholarships_year\" id=\"Scholarships_year\" size=\"10\" />"; NR+="</td>";
		NR+="<td width=\"120\">"; NR+="<input type=\"text\" name=\"Scholarships_name\" id=\"Scholarships_name\" size=\"20\" />"; NR+="</td>";
		NR+="<td width=\"120\">"; NR+="<input type=\"text\" name=\"Scholarships_kind\" id=\"Scholarships_kind\" size=\"20\" />"; NR+="</td>";
		NR+="<td width=\"120\">"; NR+="<input type=\"text\" name=\"Scholarships_money\" id=\"Scholarships_money\" size=\"20\" />"; NR+="</td>" ;

		NR+="</tr>";chk+=1
		//$("#myTbl").append($("#firstTr").clone());
		$("#myTbl").append($(NR));     });
	    
	$("#removeRow").click(function(){
		if($("#myTbl tr").size()>2){
			$("#myTbl tr:last").remove() ;chk-=1;
		
		}else{
			alert("ต้องมีรายการข้อมูลอย่างน้อย 1 รายการ");
		}
	});
    $('input[name="toon"]').click(function(){
        var Ck = $(this).val();
        if(Ck=="no"){
          $('#DivTable').hide();
        }else{
          $('#DivTable').show();
        }
    });
});
 </script>          
            
            <label><input type="radio" name="toon" value="yes" checked/>
เคยได้รับทุการศึกษา </label>
<label><input type="radio" name="toon" value="no"/>




ไม่เคยได้รับทุการศึกษา </label>
<br />


<div id="DivTable">
<table id="myTbl" width="70" border="1"  style="display:">
  <tr>
    <td align="center">ปีการศึกษา</td>
      <td align="center">ชื่อทุน</td>
        <td align="center">ประเภท</td>
        <td align="center">จำนวนเงิน</td>
  </tr>
  <tr id="firstTr">
    <td width="10"><input name="Scholarships_year" type="text" id="Scholarships_year" size="10" /></td>
      <td width="20"><input name="Scholarships_name" type="text" id="Scholarships_name" size="20" /></td>
       <td width="20"><input name="Scholarships_kind" type="text" id="Scholarships_kind" size="20" /></td>
       <td width="20"><input name="Scholarships_money" type="text" id="Scholarships_money" size="20" /></td>
  </tr>
 
</table>
<p>
<button id="addRow" type="button">+</button>
<button id="removeRow" type="button">-</button>
</p>




Tag : PHP, MySQL, JavaScript, Ajax, jQuery







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2013-09-19 09:48:26 By : komza View : 802 Reply : 6
 

 

No. 1



โพสกระทู้ ( 2,311 )
บทความ ( 1 )



สถานะออฟไลน์


กำหนดให้ค่าที่ส่งจากฟอร์มอยู่ในรูปของ Array ครับ

Code (PHP)
<tr id="firstTr">
<td width="10"><input name="Scholarships_year[]" type="text" id="Scholarships_year" size="10" /></td>
<td width="20"><input name="Scholarships_name[]" type="text" id="Scholarships_name" size="20" /></td>
<td width="20"><input name="Scholarships_kind[]" type="text" id="Scholarships_kind" size="20" /></td>
<td width="20"><input name="Scholarships_money[]" type="text" id="Scholarships_money" size="20" /></td>
</tr>


เวลาบันทึกก็ใช้หลักการประมาณนี้
Code (PHP)
<?PHP 
$num_values = count($_POST['Scholarships_year'])-1;
for($i=0;$i<=$num_values;$i++){

    #ค่าที่นำไปใช้จะอยู่ในรูปแบบนี้ครับ
    $Scholarships_year = $_POST['Scholarships_year'][$i];
    $Scholarships_name= $_POST['Scholarships_name'][$i];
    $Scholarships_kind= $_POST['Scholarships_kind'][$i];
    $Scholarships_money= $_POST['Scholarships_money'][$i];

    #คำสั่ง INSERT.....

}
?>



ดัดแปลงเอานะครับ








ประวัติการแก้ไข
2013-09-19 10:10:33
2013-09-19 10:11:46
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-09-19 10:07:47 By : arm8957
 


 

No. 2



โพสกระทู้ ( 184 )
บทความ ( 0 )



สถานะออฟไลน์


แบบนี้ ปะ ลอง แล้ว ก็ไม่ได้อะ

Code (PHP)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874" />
<title>Multirows Insert</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>


</head>

<body>
<script type="text/javascript">
            $(function(){	 var chk=0;
	$("#addRow").click(function(){
		var NR ="";   NR="<tr>";  
	
		NR+="<td width=\"60\">"; NR+="<input type=\"text\" name=\"Scholarships_year[]\" id=\"Scholarships_year\" size=\"10\" />"; NR+="</td>";
		NR+="<td width=\"120\">"; NR+="<input type=\"text\" name=\"Scholarships_name[]\" id=\"Scholarships_name\" size=\"20\" />"; NR+="</td>";
		NR+="<td width=\"120\">"; NR+="<input type=\"text\" name=\"Scholarships_kind[]\" id=\"Scholarships_kind\" size=\"20\" />"; NR+="</td>";
		NR+="<td width=\"120\">"; NR+="<input type=\"text\" name=\"Scholarships_money[]\" id=\"Scholarships_money\" size=\"20\" />"; NR+="</td>" ;

		NR+="</tr>";chk+=1
		//$("#myTbl").append($("#firstTr").clone());
		$("#myTbl").append($(NR));     });
	    
	$("#removeRow").click(function(){
		if($("#myTbl tr").size()>2){
			$("#myTbl tr:last").remove() ;chk-=1;
		
		}else{
			alert("ต้องมีรายการข้อมูลอย่างน้อย 1 รายการ");
		}
	});
    $('input[name="toon"]').click(function(){
        var Ck = $(this).val();
        if(Ck=="no"){
          $('#DivTable').hide();
        }else{
          $('#DivTable').show();
        }
    });
});
 </script>          
   <form action="test_add.php"   >     
            <label><input type="radio" name="toon" value="yes" checked/>
เคยได้รับทุการศึกษา </label>
<label><input type="radio" name="toon" value="no"/>
ไม่เคยได้รับทุการศึกษา </label>
<br />


<div id="DivTable">
<table id="myTbl" width="70" border="1"  style="display:">
  <tr>
    <td align="center">ปีการศึกษา</td>
      <td align="center">ชื่อทุน</td>
        <td align="center">ประเภท</td>
        <td align="center">จำนวนเงิน</td>
  </tr>
  <tr id="firstTr">
    <td width="10"><input name="Scholarships_year[]" type="text" id="Scholarships_year" size="10" /></td>
      <td width="20"><input name="Scholarships_name[]" type="text" id="Scholarships_name" size="20" /></td>
       <td width="20"><input name="Scholarships_kind[]" type="text" id="Scholarships_kind" size="20" /></td>
       <td width="20"><input name="Scholarships_money[]" type="text" id="Scholarships_money" size="20" /></td>
  </tr>
 
</table>
<p>
<button id="addRow" type="button">+</button>
<button id="removeRow" type="button">-</button>
<button id="submit" type="submit" name="submit"> ตกลง</button>  

</p>
</div>
</form>
  <br />          
          

</body>
</html>




ไฟล์ test_add.php

Code (PHP)
  <?PHP 
$num_values = count($_POST['Scholarships_year'])-1;
for($i=0;$i<=$num_values;$i++){

    
    $Scholarships_year = $_POST['Scholarships_year'][$i];
    $Scholarships_name= $_POST['Scholarships_name'][$i];
    $Scholarships_kind= $_POST['Scholarships_kind'][$i];
    $Scholarships_money= $_POST['Scholarships_money'][$i];

    

}
?>
<?php 
echo "INSERT INTO nisit (Scholarships_year ,Scholarships_name ,Scholarships_kind ,Scholarships_money ) values ('$Scholarships_year' ,'$nisit_name' ,'$Scholarships_name' ,'$Scholarships_kind' ,'$Scholarships_money' )";  ?>

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-09-24 14:42:36 By : komza
 

 

No. 3



โพสกระทู้ ( 4,169 )
บทความ ( 7 )

Hall of Fame 2012

สถานะออฟไลน์


ต้องนำ insert เข้าไปใน loop for ครับ เพื่อที่จะทำการแตก array
example.php
Code (PHP)
$Product=$_POST['Product'];
for($i=0;$i<count($Product);$i++){
     $insert=mysql_query("insert into tb_example (Product) values ('".$Product[$i]."')") or die (mysql_error());
}



ประวัติการแก้ไข
2013-09-24 14:47:48
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-09-24 14:47:19 By : Ex-[S]i[L]e[N]t
 


 

No. 4



โพสกระทู้ ( 228 )
บทความ ( 0 )



สถานะออฟไลน์


Code (PHP)
echo Scholarships_year;
echo $Scholarships_name;
echo $Scholarships_kind;
echo $Scholarships_money;

ในลูป for ออกไหม
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-09-24 14:49:19 By : anotherdie
 


 

No. 5



โพสกระทู้ ( 184 )
บทความ ( 0 )



สถานะออฟไลน์


แบบนี้ ก็ไม่ออก คับ ไคร รู้ ช่วย แก้ หน่อย ยัง เก็บ ค่า ตัวนี้ ไม่ ได้ มา 2 อาทิต ละ


Code (PHP)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874" />
<title>Multirows Insert</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>


</head>

<body>
<script type="text/javascript">
            $(function(){	 var chk=0;
	$("#addRow").click(function(){
		var NR ="";   NR="<tr>";  
	
		NR+="<td width=\"60\">"; NR+="<input type=\"text\" name=\"Scholarships_year[chk]\" id=\"Scholarships_year\" size=\"10\" />"; NR+="</td>";
		NR+="<td width=\"120\">"; NR+="<input type=\"text\" name=\"Scholarships_name[chk]\" id=\"Scholarships_name\" size=\"20\" />"; NR+="</td>";
		NR+="<td width=\"120\">"; NR+="<input type=\"text\" name=\"Scholarships_kind[chk]\" id=\"Scholarships_kind\" size=\"20\" />"; NR+="</td>";
		NR+="<td width=\"120\">"; NR+="<input type=\"text\" name=\"Scholarships_money[chk]\" id=\"Scholarships_money\" size=\"20\" />"; NR+="</td>" ;

		NR+="</tr>";chk+=1
		//$("#myTbl").append($("#firstTr").clone());
		$("#myTbl").append($(NR));     });
	    
	$("#removeRow").click(function(){
		if($("#myTbl tr").size()>2){
			$("#myTbl tr:last").remove() ;chk-=1;
		
		}else{
			alert("ต้องมีรายการข้อมูลอย่างน้อย 1 รายการ");
		}
	});
    $('input[name="toon"]').click(function(){
        var Ck = $(this).val();
        if(Ck=="no"){
          $('#DivTable').hide();
        }else{
          $('#DivTable').show();
        }
    });
});
 </script>          
   <form action="test_add.php"   >     
            <label><input type="radio" name="toon" value="yes" checked/>
เคยได้รับทุการศึกษา </label>
<label><input type="radio" name="toon" value="no"/>
ไม่เคยได้รับทุการศึกษา </label>
<br />


<div id="DivTable">
<table id="myTbl" width="70" border="1"  style="display:">
  <tr>
    <td align="center">ปีการศึกษา</td>
      <td align="center">ชื่อทุน</td>
        <td align="center">ประเภท</td>
        <td align="center">จำนวนเงิน</td>
  </tr>
  <tr id="firstTr">
    <td width="10"><input name="Scholarships_year[]" type="text" id="Scholarships_year" size="10" /></td>
      <td width="20"><input name="Scholarships_name[]" type="text" id="Scholarships_name" size="20" /></td>
       <td width="20"><input name="Scholarships_kind[]" type="text" id="Scholarships_kind" size="20" /></td>
       <td width="20"><input name="Scholarships_money[]" type="text" id="Scholarships_money" size="20" /></td>
  </tr>
 
</table>
<p>
<button id="addRow" type="button">+</button>
<button id="removeRow" type="button">-</button>
<button id="submit" type="submit" name="submit"> ตกลง</button>  

</p>
</div>
</form>
  <br />          
          

</body>
</html>





ไฟล์ที่เอาไว้ แอดค่าลงตาราง test_add.php


Code (PHP)
  <?PHP 


    
    $Scholarships_year = $_POST['Scholarships_year[$i]'];
    $Scholarships_name= $_POST['Scholarships_name[$i]'];
    $Scholarships_kind= $_POST['Scholarships_kind[$i]'];
    $Scholarships_money= $_POST['Scholarships_money[$i]'];

    
$num_values = count($_POST['Scholarships_year'])-1;
for($i=0;$i<=$num_values;$i++){
	
	 mysql_query("insert into  scholarships_history (Scholarships_year ,Scholarships_name ,Scholarships_kind ,Scholarships_money) values ('".$Scholarships_year[$i]."','".$Scholarships_name[$i]."','".$Scholarships_kind[$i]."','".$Scholarships_money[$i]."')") or die (mysql_error());
}

?>

<?php 
$num_values = count($_POST['Scholarships_year'])-1;
for($i=0;$i<=$num_values;$i++)
{
echo "INSERT INTO nisit (Scholarships_year ,Scholarships_name ,Scholarships_kind ,Scholarships_money ) values ('$Scholarships_year[$i]','$Scholarships_name[$i]','$Scholarships_kind[$i]','$Scholarships_money[$i])";  } ?> 

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-09-25 19:46:12 By : komza
 


 

No. 6



โพสกระทู้ ( 11,835 )
บทความ ( 10 )

สมาชิกที่ใส่เสื้อไทยครีเอท Hall of Fame 2012

สถานะออฟไลน์


ง่ายๆ นะครับ ถ้าเข้าใจเรื่อง array ลอง print_r($_POST); ออกมาดู ถ้าไม่เข้าใจ array เลย ลองไปอ่านบทเรียนก่อนนะครับ อ่านแล้วยังทำไม่ได้ ลองเขียนแล้วมาถามอีกรอบครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-09-25 21:41:51 By : PlaKriM
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : ขอถามหน่อยครับ เราจะเก็บค่าใน textbox ลง DB ยังไง ถ้าเป็น ตารางที่เพิ่มแถวและลบแถวได้ ในลักษณะ นี้
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 04
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่