insert into หลายๆ ตาราย ทำยังไงค่ะ insertสูงสุดได้แค่ 2ตารางค่ะ

<?php

$host ="localhost";

$dbuser = "root";

$dbpass = "1234";

$dbname = "patients_db";

$tbluser = "user";

$tb="`patientshistory_tb`, `factfinding_tb`,`informant` ";



mysql_connect("$host","$dbuser","$dbpass")or die("Connect server Error...");

mysql_select_db("$dbname")or die("Can not select Database...");

$sql="select * from $tb";

$dbquery=mysql_db_query($dbname,$sql);

$result= mysql_fetch_array($dbquery);

mysql_query("SET NAMES UTF8");

date_default_timezone_set("Asia/Bangkok");



mysql_query("INSERT INTO factfinding_tb (patients_hn,receive_name,receive_branch,receive_admit_date,receive_an,without,society,target_id,target_other,disabled_id,cost_care,can_pay,assist) values($code,'".$_POST[recieve_name]."','".$_POST[textfield]."','".$_POST[textfield2]."','".$_POST[textfield3]."','".$_POST[textfield4]."','".$_POST[textfield33]."','".$_POST[target]."','".$_POST[textfield6]."','".$_POST[targetsub]."','".$_POST[textfield7]."','".$_POST[textfield8]."','".$_POST[textfield9]."')");



mysql_query("INSERT INTO patientshistory_tb (service_id,case_date,patients_hn,sn_number,article_id,patients_name,patients_id,patients_birthday,patients_age,patients_nation,patients_origin,religion_id,status_id,patients_education,sid_id,patients_sid_other,patients_career,patients_salary,patients_salarysource,patients_source_other,patients_debt,patients_debt_count,patients_debt_source,patients_tel,patients_mobile,patients_address,patients_moo,patients_soi,patients_road,patients_province,patients_amphoe,patients_district,country,patients_post,patients_photo,patients_map) values('".$_POST[serviceid]."','".$_POST[casedate]."',$code,'".$_POST[sn]."','".$_POST[article_id]."','".$_POST[pname]."','".$_POST[pid]."','".$_POST[pbd]."','".$_POST[page]."','".$_POST[pnation]."','".$_POST[porigin]."','".$_POST[religion_id]."','".$_POST[status_id]."','".$_POST[peducation]."','".$_POST[sid_id]."','".$_POST[textfield31]."','".$_POST[pcareer]."','".$_POST[psalary]."','".$_POST[salarysource]."','".$_POST[psalarysource]."','".$_POST[debt]."','".$_POST[pdebtcount]."','".$_POST[debtsource]."','".$_POST[ptel]."','".$_POST[pmobile]."','".$_POST[paddress]."','".$_POST[pmoo]."','".$_POST[psoi]."','".$_POST[proad]."','".$_POST[province]."','".$_POST[amphur]."','".$_POST[district]."','".$_POST[pcountry]."','".$_POST[ppost]."','".$_POST[file1]."','".$_POST[file2]."')");

mysql_query("INSERT INTO informant (patients_hn,informant_type,article_id,informant_name,informant_relate,informant_address,informant_moo,informant_soi,informant_road,informant_districtinformant_amphoe,informant_provice,informant_country,informant_post) values($code,'".$_POST[informant]."','".$_POST[article_1]."','".$_POST[textfield10]."','".$_POST[textfield11]."','".$_POST[textfield12]."','".$_POST[textfield13]."','".$_POST[textfield14]."','".$_POST[textfield15]."','".$_POST[district_1]."','".$_POST[amphur_1]."','".$_POST[province_1]."','".$_POST[textfield19]."','".$_POST[textfield20]."')")or die (mysql_error());









echo "<script>alert('บันทึกข้อมูลเรียบร้อยแล้ว'); window.location.href ='patients_detail.php';</script>";





?>

16 ก.ย. 2557 1 1,230

ที่เขียนอยู่ก็น่าจะถูกนะครับ



เหตที่ insert ไม่เข้า อาจมาจากการ error ของ query เช่น ค่าที่ส่งมามีอักขระที่ไม่พึงประสงค์ หรือ ค่าที่ส่งมาเป็นค่าว่าง ในขณะที่ field นั้น ถูกกำหนดให้ไม่สามารถว่างได้ หรือ ข้อมูลผิดระเภท เช่น ฟิลด์ตัวเลข แต่กรอกตัวอักษรมา



ลองทดสอบด้วยการ echo query ออกมาดูครับ แล้วนำผลที่ได้ไปลองรันบน phpmyadmin ว่ามันสามารถ insert ได้หรือไม่



คำแนะนำ ไม่ควรรับค่าตัวแปร แล้ว ส่งไปยัง query โดยตรง ควรมีการตรวจสอบก่อนว่า ค่าที่ได้รับมา ถูกต้องตามข้อกำหนดหรือไม่ เช่น ฟิลด์ตัวเลข ค่าที่ส่งมาเป็นตัวเลขหรือไม่ และ ฟิลด์ที่เป็น string มีการ add slashes มาหรือไม่

#1
ความคิดเห็น
ไฟล์อัปโหลด ชนิด jpg, jpeg ขนาดไฟล์ไม่เกิน 1024
^