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 > php mysql ขอแนวคิดหรือ ตัวอย่างที่คล้ายๆในการ insert ข้อมูล



 

php mysql ขอแนวคิดหรือ ตัวอย่างที่คล้ายๆในการ insert ข้อมูล

 



Topic : 091468



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



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




ช่วยหน่อยน้า ติดอยู่ที่เดียว จะเรียน จบแล้ว ถ้าแก้ตรงนี้ไม่ได้เรียนไม่จบจริงๆ

รับค่าจากฟร์อม มา 3 ค่า ปี
1. วันเริ่มสัญญา
2. วันหมดสัญญา
3. เงินแต่ละเดือน

ที่อยากจะทำคือ

เมื่อเลือกวันที่ เริ่มสัญญา กับ วันหมดสัญญา แล้ว และกรอก ข้อมูล ในช่องเงินเดือนแต่ละเดือนแล้ว

เมื่อกด submit ก็จะทำการบัททึกลงฐานข้อมูล อย่างนี้ เช่น

ตัวแปรที่1 คือ วันเริ่มสัญญา = 02/08/2556
ตัวแปรที่2 คือ วันหมดสัญญา = 20/02/2557 //ในที่นี้ต้องบอกก่อนว่าไม่ต้องสนใจวัน สนใจแต่เดือนกับ ปี
ตัวแปรที่3 คือ เงินแต่ละเดือน = 1,000 บาท

แล้วอยากให้เงินเดือน 1,000 บาท เข้าไปอยู๋ในทุกเดือน ของวันเริ่ม และวันหมด ของสัญญา จะได้ตารางดังนี้

12399

ตัวแปรที่1 คือ วันเริ่มสัญญา = 02/08/2556
ตัวแปรที่2 คือ วันหมดสัญญา = 20/02/2557 // สองตัวนี้อยู่อีกตารางนึง

ขอรบกวนด้วยนะคร้า จะจบแบ้ว



Tag : PHP







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2013-02-26 14:36:52 By : mameaw4569 View : 766 Reply : 5
 

 

No. 1



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

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

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

ใช้ DateDiff น่ะครับ จะได้จำนวนเดือนครับ แล้ว Loop เพื่อ Insert ได้เลยครับ

Go to : PHP DateDiff / TimeDiff






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-02-27 10:26:16 By : mr.win
 


 

No. 2



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

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

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

หรือใช้วิธีคิดแบบง่ายๆ เลยครับ

ถ้าไม่สนใจวัน ดังนั้นสิ่งที่สนใจคือเดือนกับปี

ดังนั้นเอาแค่เลขเดือนและเลขปี "ไปทำเป็น timestamp ของวันที่ 1 ของเดือนนั้นๆ" โดยใช้ mktime()
และวนลูปโดยตรวจสอบว่าจะจบเมื่อไหร่จากค่าดังกล่าวที่ได้

ตัวอย่าง
<?php
$start_date = '02/08/2556';
$end_date = '20/02/2557';

// แยกเลข เดือน ปี ออกมาด้วย explode() โดยใช้ / เป็นตัวแยก
// จะสังเกตได้ว่าไม่ใช้เลขวัน จึงเว้นไว้
list(/*ไม่ใช้*/, $month, $year) = explode('/', $start_date);
$month = (int)$month;
list(/*ไม่ใช้*/, $end_month, $end_year) = explode('/', $end_date);

// แปลง พ.ศ.กลับเป็น ค.ศ. ด้วยการลบกับ 543
$year -= 543;
$end_year -= 543;

// สร้าง timestamp ของวันที่ 1 ในเดือนสุดท้าย ด้วย mktime()
// รูปแบบการใช้คือ mktime(ชั่วโมง, นาที, วินาที, เดือน, วัน, ปี)
$end = mktime(0, 0, 0, $end_month, 1, $end_year);

// วนลูปไปเรื่อยๆ โดยเริ่มจากเดือนแรก
do {
	// สร้าง timestamp ของวันที่ 1 ในเดือนปัจจุบัน ด้วย mktime()
    $current = mktime(0, 0, 0, $month, 1, $year);
	
	// ตรงนี้จะทำการ INSERT เงินเดือนหรืออะไรก็ตามแต่
	// ซึ่งเราจะใช้ค่า $month และ $year เป็นค่าที่จะ INSERT เข้าไป
	// แต่ในที่นี้ แค่แสดงผลทดสอบว่าค่า "$month และ $year ถูกต้องหรือไม่
	echo "$month/$year<br />\n";
	
	// เพิ่มจำนวนเดือน
    $month++;
	
	// หากเดือนมากกว่า 12
    if ($month > 12) {
		// ให้เดือนกลับมาเริ่มที่ 1
        $month = 1;
		
		// และเพิ่มปี
        $year++;
    }
	
// ตรวจสอบว่า timestamp ปัจจุบัน "ไม่เท่ากับ" timestamp วันสุดท้ายหรือไม่
// ถ้าเป็นจริง ก็ขึ้นไปทำข้างบนต่อไป (หลัง do)
} while (($current !== $end));
?>





อ้างอิง

explode() - http://www.php.net/manual/en/function.explode.php
list() - http://www.php.net/manual/en/function.list.php
mktime() - http://php.net/manual/en/function.mktime.php
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-02-27 19:13:32 By : cookiephp
 

 

No. 3



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



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


ขอบคุณทุกท่านค่ะ เด๋วเอาไปลองดูน้า
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-02-28 19:57:46 By : mameaw4569
 


 

No. 4



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



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


aaaaaaa
ทำไม insert ไม่เข้าแว้ เฮ้ออออออออ ช่วยดูหน่อยน้า(PHP)
<?
include("../connect.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=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?
 
$idrent = $_POST["idrent"];
$mrent = $_POST["monthrent"];

$sql55 = "select * from rent where id_rent = $idrent";
$objResult= mysql_query($sql55);
$objrow = mysql_fetch_array($objResult);

$s_rent = $objrow['s_rent'];
$e_rent = $objrow['e_rent'];

list($Y,$M,$D) = explode("-" , $s_rent); 
 $day1 = "$D-$M-".($Y+543);
 $year = $Y;
 $month = $M;
list($Y,$M,$D) = explode("-" , $e_rent);
 $day2 = "$D-$M-".($Y+543);
 $end_year = $Y;
 $end_month = $M;
 
 


$end = mktime(0, 0, 0, $end_month, 1, $end_year);

// วนลูปไปเรื่อยๆ โดยเริ่มจากเดือนแรก
do {
	// สร้าง timestamp ของวันที่ 1 ในเดือนปัจจุบัน ด้วย mktime()
    $current = mktime(0, 0, 0, $month, 1, $year);
	
	// ตรงนี้จะทำการ INSERT เงินเดือนหรืออะไรก็ตามแต่
	// ซึ่งเราจะใช้ค่า $month และ $year เป็นค่าที่จะ INSERT เข้าไป
	$sqlck ="select * from revenuc where id_rent = $idrent and year = $year ";
	$objQuery = mysql_query($sqlck) or die (mysql_error());
    
	
	if($objQuery){
		
		      if($month == 1){
	          $sql1 = "update revenuc set  month1= '$mrent' where id_rent = $idrent and  year = $year";
			  $objQuery1 = mysql_query($sql1)or die (mysql_error());
	    }else if($month == 2){
		$sql2 = "update revenuc set  month2= '$mrent' where id_rent = $idrent and  year = $year";
		$objQuery2 = mysql_query($sql2)or die (mysql_error());
		}else if($month == 3){
		$sql3 = "update revenuc set  month3= '$mrent' where id_rent = $idrent and  year = $year";
		$objQuery3 = mysql_query($sql3)or die (mysql_error());
		}else if($month == 4){
		$sql4 = "update revenuc set  month4= '$mrent' where id_rent = $idrent and  year = $year";
		$objQuery4 = mysql_query($sql4)or die (mysql_error());
		}else if($month == 5){
		$sql5 = "update revenuc set  month5= '$mrent' where id_rent = $idrent and  year = $year";
		$objQuery5 = mysql_query($sql5)or die (mysql_error());
		}else if($month == 6){
		$sql6 = "update revenuc set  month6= '$mrent' where id_rent = $idrent and  year = $year";
		$objQuery6 = mysql_query($sql6)or die (mysql_error());
		}else if($month == 7){
		$sql7 = "update revenuc set  month7= '$mrent' where id_rent = $idrent and  year = $year";
		$objQuery7 = mysql_query($sql7)or die (mysql_error());
		}else if($month == 8){
		$sql8 = "update revenuc set  month8= '$mrent' where id_rent = $idrent and  year = $year";
		$objQuery8 = mysql_query($sql8)or die (mysql_error());
		}else if($month == 9){
		$sql9 = "update revenuc set  month9= '$mrent' where id_rent = $idrent and  year = $year";
		$objQuery9 = mysql_query($sql9)or die (mysql_error());
		}else if($month == 10){
		$sql10 = "update revenuc set  month10= '$mrent' where id_rent = $idrent and  year = $year";
		$objQuery10 = mysql_query($sql10)or die (mysql_error());
		}else if($month == 11){
		$sql11 = "update revenuc set  month11= '$mrent' where id_rent = $idrent and  year = $year";
		$objQuery11 = mysql_query($sql11)or die (mysql_error());
		}else{
		$sql12 = "update revenuc set  month12= '$mrent' where id_rent = $idrent and  year = $year";
		$objQuery12 = mysql_query($sql12)or die (mysql_error());
		}
		
		}else {
			if($month == 1){
			$sqlt1 = "INSERT INTO revenuc (id_rent,year,month1,month2,month3,month4,month5,month6,month7,month8,month9,month10,month11,month12) VALUES ('$idrent','$year','$mrent','0','0','0','0','0','0','0','0','0','0','0')";
            $result1 = mysql_query($sqlt1)or die (mysql_error());
				}else if($month == 2){
			$sqlt2 = "INSERT INTO revenuc (id_rent,year,month1,month2,month3,month4,month5,month6,month7,month8,month9,month10,month11,month12) VALUES ('$idrent','$year','0','$mrent','0','0','0','0','0','0','0','0','0','0')";
            $result2 = mysql_query($sqlt2)or die (mysql_error());
			}else if($month == 3){
			$sqlt3 = "INSERT INTO revenuc (id_rent,year,month1,month2,month3,month4,month5,month6,month7,month8,month9,month10,month11,month12) VALUES ('$idrent','$year','0','0','$mrent','0','0','0','0','0','0','0','0','0')";
            $result3 = mysql_query($sqlt3)or die (mysql_error());
			}else if($month == 4){
			$sqlt4 = "INSERT INTO revenuc (id_rent,year,month1,month2,month3,month4,month5,month6,month7,month8,month9,month10,month11,month12) VALUES ('$idrent','$year','0','0','0','$mrent','0','0','0','0','0','0','0','0')";
            $result4 = mysql_query($sqlt4)or die (mysql_error());
			}else if($month == 5){
			$sqlt5 = "INSERT INTO revenuc (id_rent,year,month1,month2,month3,month4,month5,month6,month7,month8,month9,month10,month11,month12) VALUES ('$idrent','$year','0','0','0','0','$mrent','0','0','0','0','0','0','0')";
            $result5 = mysql_query($sqlt5)or die (mysql_error());
			}else if($month == 6){
			$sqlt6 = "INSERT INTO revenuc (id_rent,year,month1,month2,month3,month4,month5,month6,month7,month8,month9,month10,month11,month12) VALUES ('$idrent','$year','0','0','0','0','0','$mrent','0','0','0','0','0','0')";
            $result6 = mysql_query($sqlt6)or die (mysql_error());
			}else if($month == 7){
			$sqlt7 = "INSERT INTO revenuc (id_rent,year,month1,month2,month3,month4,month5,month6,month7,month8,month9,month10,month11,month12) VALUES ('$idrent','$year','0','0','0','0','0','0','$mrent','0','0','0','0','0')";
            $result7 = mysql_query($sqlt7)or die (mysql_error());
			}else if($month == 8){
			$sqlt8 = "INSERT INTO revenuc (id_rent,year,month1,month2,month3,month4,month5,month6,month7,month8,month9,month10,month11,month12) VALUES ('$idrent','$year','0','0','0','0','0','0','0','$mrent','0','0','0','0')";
            $result8 = mysql_query($sqlt8)or die (mysql_error());
			}else if($month == 9){
			$sqlt9 = "INSERT INTO revenuc (id_rent,year,month1,month2,month3,month4,month5,month6,month7,month8,month9,month10,month11,month12) VALUES ('$idrent','$year','0','0','0','0','0','0','0','0','$mrent','0','0','0')";
            $result9 = mysql_query($sqlt9)or die (mysql_error());
			}else if($month == 10){
			$sqlt10 = "INSERT INTO revenuc (id_rent,year,month1,month2,month3,month4,month5,month6,month7,month8,month9,month10,month11,month12) VALUES ('$idrent','$year','0','0','0','0','0','0','0','0','0','$mrent','0','0')";
            $result10 = mysql_query($sqlt10)or die (mysql_error());
			}else if($month == 11){
			$sqlt11 = "INSERT INTO revenuc (id_rent,year,month1,month2,month3,month4,month5,month6,month7,month8,month9,month10,month11,month12) VALUES ('$idrent','$year','0','0','0','0','0','0','0','0','0','0','$mrent','0')";
            $result11 = mysql_query($sqlt11)or die (mysql_error());
			}else {
			$sqlt12 = "INSERT INTO revenuc (id_rent,year,month1,month2,month3,month4,month5,month6,month7,month8,month9,month10,month11,month12) VALUES ('$idrent','$year','0','0','0','0','0','0','0','0','0','0','0','$mrent')";
            $result12 = mysql_query($sqlt12)or die (mysql_error());
			}
		}
		
	
		
		
			
		
	
	
	// เพิ่มจำนวนเดือน
    $month++;
	
	// หากเดือนมากกว่า 12
    if ($month > 12) {
		// ให้เดือนกลับมาเริ่มที่ 1
        $month = 1;
		
		// และเพิ่มปี
        $year++;
    }
	
// ตรวจสอบว่า timestamp ปัจจุบัน "ไม่เท่ากับ" timestamp วันสุดท้ายหรือไม่
// ถ้าเป็นจริง ก็ขึ้นไปทำข้างบนต่อไป (หลัง do)
} while (($current !== $end));
?>

</body>
</html>

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-03-04 21:30:40 By : mameaw4569
 


 

No. 5



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



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


อ่อ ได้และโทดที
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-03-04 22:23:10 By : mameaw4569
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : php mysql ขอแนวคิดหรือ ตัวอย่างที่คล้ายๆในการ insert ข้อมูล
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ 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 01
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 อัตราราคา คลิกที่นี่