|
|
|
สอบถามการ insert data 3 ตาราง ทั้ง insert ปกติ และ insert array |
|
|
|
|
|
|
|
Code Insert ครับ
Code (PHP)
<?php
$objConnect = mysql_connect("localhost","root","**") or die("Error Connect to Database");
$objDB = mysql_select_db("**");
mysql_query("SET NAMES UTF8");
$strSQL1 = "INSERT INTO Personal ";
$strSQL1 .="(st_date,emp_id,emp_date,division,department,sc,name_th,lname_th,blood,name_en,
lname_en,per_date,id_card,exp_card,id_fund,date_fund,rate,acc_id,name_bank,
m_bank,add_h,district_h,amphoe_h,province_h,add_n,district_n,amphoe_n,province_n,
tel_home,tel,images,education,e_major,institution,name_e,tel_e,) ";
$strSQL1 .="VALUES ";
$strSQL1 .="('".$_POST["st_date"]."','".$_POST["emp_id"]."','".$_POST["emp_date"]."' ";
$strSQL1 .=",'".$_POST["division"]."','".$_POST["department"]."','".$_POST["sc"]."' ";
$strSQL1 .=",'".$_POST["name_th"]."','".$_POST["lname_th"]."','".$_POST["blood"]."' ";
$strSQL1 .=",'".$_POST["name_en"]."','".$_POST["lname_en"]."','".$_POST["per_date"]."' ";
$strSQL1 .=",'".$_POST["id_card"]."','".$_POST["exp_card"]."','".$_POST["id_fund"]."' ";
$strSQL1 .=",'".$_POST["date_fund"]."','".$_POST["rate"]."','".$_POST["acc_id"]."' ";
$strSQL1 .=",'".$_POST["name_bank"]."','".$_POST["m_bank"]."','".$_POST["add_h"]."' ";
$strSQL1 .=",'".$_POST["district_h"]."','".$_POST["amphoe_h"]."','".$_POST["province_h"]."' ";
$strSQL1 .=",'".$_POST["add_n"]."','".$_POST["district_n"]."','".$_POST["amphoe_n"]."' ";
$strSQL1 .=",'".$_POST["province_n"]."','".$_POST["tel_home"]."','".$_POST["tel"]."' ";
$strSQL1 .=",'".$_POST["images"]."','".$_POST["education"]."','".$_POST["e_major"]."' ";
$strSQL1 .=",'".$_POST["institution"]."','".$_POST["name_e"]."','".$_POST["tel_e"]."' ";
$objQuery = mysql_query($strSQL1);
$strSQL2 = "INSERT INTO status ";
$strSQL2 .="(name_m,lname_m,date_m,id_card_m,date_r,location,tel_m,status,mary,) ";
$strSQL2 .="VALUES ";
$strSQL2 .="('".$_POST["name_m"]."','".$_POST["lname_m"]."','".$_POST["date_m"]."' ";
$strSQL2 .=",'".$_POST["id_card_m"]."','".$_POST["date_r"]."','".$_POST["location"]."' ";
$strSQL2 .=",'".$_POST["tel_m"]."','".$_POST["status"]."','".$_POST["mary"]."' ";
$objQuery = mysql_query($strSQL2);
$strSQL3 = "INSERT INTO tax ";
$strSQL3 .="(f_name,f_id,f_date,claim_f,mf_name,mf_id,mf_date,claim_mf,mm_name,
mm_id,mm_date,claim_mm,insurance,num_in,insurance_l,num_inl,unit,num_un,interest,
num_int,support,num_su,donate,num_do,buy,num_b,other,num_o) ";
$strSQL3 .="VALUES ";
$strSQL3 .="('".$_POST["f_name"]."','".$_POST["f_id"]."','".$_POST["f_date"]."' ";
$strSQL3 .=",'".$_POST["claim_f"]."','".$_POST["mf_name"]."','".$_POST["mf_id"]."' ";
$strSQL3 .=",'".$_POST["mf_date"]."','".$_POST["claim_mf"]."','".$_POST["mm_name"]."' ";
$strSQL3 .=",'".$_POST["mm_id"]."','".$_POST["mm_date"]."','".$_POST["claim_mm"]."' ";
$strSQL3 .=",'".$_POST["insurance"]."','".$_POST["num_in"]."','".$_POST["insurance_l"]."' ";
$strSQL3 .=",'".$_POST["num_inl"]."','".$_POST["unit"]."','".$_POST["num_un"]."' ";
$strSQL3 .=",'".$_POST["interest"]."','".$_POST["num_int"]."','".$_POST["support"]."' ";
$strSQL3 .=",'".$_POST["num_su"]."','".$_POST["donate"]."','".$_POST["num_do"]."' ";
$strSQL3 .=",'".$_POST["buy"]."','".$_POST["num_b"]."','".$_POST["other"]."' ";
$strSQL3 .=",'".$_POST["num_o"]."' ";
$objQuery = mysql_query($strSQL3)
for ($i = 1; $i<= (int)$_POST["hdnCount"]; $i++)
{
if($_POST["name_baby$i"] != "")
{
$strSQL = "INSERT INTO status ";
$strSQL .="(name_baby,date_baby,status_baby,class,age,id_card_baby,stay) ";
$strSQL .="VALUES ";
$strSQL .="('".$_POST["name_baby$i"]."','".$_POST["date_baby$i"]."','".$_POST["status_baby$i"]."', ";
$strSQL .="'".$_POST["class$i"]."' ";
$strSQL .=",'".$_POST["age$i"]."','".$_POST["id_card_baby$i"]."', ";
$strSQL .="'".$_POST["stay$i"]."') ";
echo $objQuery = mysql_query($strSQL);
}
}
?>
Tag : PHP, MySQL
|
|
|
|
|
|
Date :
2017-03-02 11:54:30 |
By :
TOn |
View :
716 |
Reply :
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ถามว่าอะไรครับ
|
|
|
|
|
Date :
2017-03-02 12:37:51 |
By :
thesin18598 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ไม่มีวงเล็บปิดตรง VALUES
|
ประวัติการแก้ไข 2017-03-02 17:24:24
|
|
|
|
Date :
2017-03-02 17:23:45 |
By :
Luz |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ตอนนี้ผมใช้เป็น mysqli_multi_query แทน ตอนนี้ทุกค่าเข้า db แล้ว
แต่อยากสอบถามเพิ่มเติมครับ ว่า เราสามารถแทรก การ insert array เข้าไปได้ไหม ?
ขอบคุณทุกท่านครับ
Code (PHP)
$conn = mysqli_connect($servername, $username, $password, $dbname);
mysqli_set_charset($conn, "utf8");
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO personal (st_date,emp_id,emp_date,division,department,sc,name_th,lname_th,blood,name_en,
lname_en,per_date,id_card,exp_card,id_fund,date_fund,rate,acc_id,name_bank,
m_bank,add_h,district_h,amphoe_h,province_h,add_n,district_n,amphoe_n,province_n,
tel_home,tel,images,education,e_major,institution,name_e,tel_e)
VALUES
('".$_POST["st_date"]."','".$_POST["emp_id"]."','".$_POST["emp_date"]."','".$_POST["division"]."','".$_POST["department"]."','".$_POST["sc"]."','".$_POST["name_th"]."','".$_POST["lname_th"]."','".$_POST["blood"]."','".$_POST["name_en"]."'
,'".$_POST["lname_en"]."','".$_POST["per_date"]."','".$_POST["id_card"]."','".$_POST["exp_card"]."','".$_POST["id_fund"]."','".$_POST["date_fund"]."','".$_POST["rate"]."','".$_POST["acc_id"]."','".$_POST["name_bank"]."'
,'".$_POST["m_bank"]."','".$_POST["add_h"]."','".$_POST["district_h"]."','".$_POST["amphoe_h"]."','".$_POST["province_h"]."','".$_POST["add_n"]."','".$_POST["district_n"]."','".$_POST["amphoe_n"]."','".$_POST["province_n"]."'
,'".$_POST["tel_home"]."','".$_POST["tel"]."','".$_POST["images"]."','".$_POST["education"]."','".$_POST["e_major"]."','".$_POST["institution"]."','".$_POST["name_e"]."','".$_POST["tel_e"]."');";
$sql .= "INSERT INTO status (name_m,lname_m,date_m,id_card_m,date_r,location,tel_m,status,mary,num_baby)
VALUES
('".$_POST["name_m"]."','".$_POST["lname_m"]."','".$_POST["date_m"]."','".$_POST["id_card_m"]."','".$_POST["date_r"]."','".$_POST["location"]."'
,'".$_POST["tel_m"]."','".$_POST["status"]."','".$_POST["mary"]."','".$_POST["num_baby"]."');";
$sql .= "INSERT INTO tax (f_name,f_id,f_date,claim_f,mf_name,mf_id,mf_date,claim_mf,mm_name,
mm_id,mm_date,claim_mm,insurance,num_in,insurance_l,num_inl,unit,num_un,interest,
num_int,support,num_su,donate,num_do,buy,num_b,other,num_o)
VALUES
('".$_POST["f_name"]."','".$_POST["f_id"]."','".$_POST["f_date"]."','".$_POST["claim_f"]."','".$_POST["mf_name"]."','".$_POST["mf_id"]."'
,'".$_POST["mf_date"]."','".$_POST["claim_mf"]."','".$_POST["mm_name"]."','".$_POST["mm_id"]."','".$_POST["mm_date"]."','".$_POST["claim_mm"]."'
,'".$_POST["insurance"]."','".$_POST["num_in"]."','".$_POST["insurance_l"]."','".$_POST["num_inl"]."','".$_POST["unit"]."','".$_POST["num_un"]."'
,'".$_POST["interest"]."','".$_POST["num_int"]."','".$_POST["support"]."','".$_POST["num_su"]."','".$_POST["donate"]."','".$_POST["num_do"]."'
,'".$_POST["buy"]."','".$_POST["num_b"]."','".$_POST["other"]."','".$_POST["num_o"]."');";
if (mysqli_multi_query($conn, $sql)) {
echo "New records created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|