 |
พี่ๆครับรบก่วนช่วยผมด้วยครับ ผมทำหลายวันแล้วครับ ยังไม่ได้สักทีครับ (ท้อมากเลยครับ) |
|
 |
|
|
 |
 |
|
จะใส่ข้อมูลลงในฐานสองตารางจากฟอร์มเดียวกันครับ พี่ช่วยดูหน่อยครับว่าผิดจุดไหนครับ
หน้าฟอร์มครับ

ฐานข้อมูลครับ
payequip

payequip_detail

โค๊ดหน้าฟอร์มครับ
Code (PHP)
<?
session_start();
if($_SESSION['per_id'] == "")
{
$errorMessage = "Please Login !";
include ("page-fragment/login/LoginForm.php");
exit();
}
mysql_connect('localhost','root','12345');
mysql_select_db(medical_db);
$strSQL = "SELECT * FROM person p
JOIN department s ON p.depart_id=s.depart_id
WHERE per_id = '".$_SESSION['per_id']."' ";
$objQuery = mysql_query($strSQL);
$objResult = mysql_fetch_array($objQuery);
?>
<?
mysql_connect('localhost','root','12345');
mysql_select_db(medical_db);
$strSQL = "SELECT * FROM unit";
$objQuery = mysql_query($strSQL);
?>
<script language="javascript">
function OpenPopup(intLine)
{
window.open('page-fragment/order/OrderPopUpList.php?Line='+intLine,'myPopup','width=650,height=220,toolbar=0, menubar=0,location=0,status=1,scrollbars=1,resizable=1,left=0,top=0');
}
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 No ***//
newCell = newRow.insertCell(0);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center>"+intLine+"</center>";
//*** Column MedicalID ***//
newCell = newRow.insertCell(1);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center><INPUT TYPE=\"TEXT\" SIZE=\"5\" NAME=\"me_id"+intLine+"\" ID=\"me_id"+intLine+"\" VALUE=\"\"></center>";
//*** Column Name ***//
newCell = newRow.insertCell(2);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center><INPUT TYPE=\"TEXT\" SIZE=\"10\" NAME=\"me_name"+intLine+"\" ID=\"me_name"+intLine+"\" VALUE=\"\"></center>";
//*** Column Stock ***//
newCell = newRow.insertCell(3);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center><INPUT TYPE=\"TEXT\" SIZE=\"3\" NAME=\"stock"+intLine+"\" ID=\"stock"+intLine+"\" VALUE=\"\"></center>";
//*** Column Unit ***//
newCell = newRow.insertCell(4);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center><INPUT TYPE=\"TEXT\" SIZE=\"5\" NAME=\"unit_id"+intLine+"\" ID=\"unit_id"+intLine+"\" VALUE=\"\"></center>";
//*** Column จำนวนเบิก ***//
newCell = newRow.insertCell(5);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center><INPUT TYPE=\"TEXT\" SIZE=\"5\" NAME=\"amount"+intLine+"\" ID=\"amount"+intLine+"\" VALUE=\"\"></center>";
//*** Column 7 ***//
newCell = newRow.insertCell(6);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
//newCell.setAttribute("OnClick", "OpenPopup('"+intLine+"')");
newCell.innerHTML = "<center><INPUT TYPE=\"BUTTON\" NAME=\"btnPopup_"+intLine+"\" ID=\"btnPopup_"+intLine+"\" VALUE=\"...\" OnClick=\"OpenPopup('"+intLine+"')\"></center>";
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;
}
}
</script>
<body OnLoad="CreateNewRow();">
<form name="frmMain" method="post" action="ActionController.php?action=order/CreateOrderAction">
<!--เวลาเก็บ /CreateOrderAction -->
<table>
<tr>
<input type="hidden" name="per_id" id="per_id" value="<?=$objResult['per_id']?>" />
<td>
<p>แผนก : </p>
<input type="text" name="depart_name" id="depart_name" class="texPay" value="<?=$objResult["depart_name"];?>"/>
</td>
<td>
<p>วันที่เบิก : </p>
<input type="text" name="pay_date" id="pay_date" class="texPay" value="<?=date('Y-m-d')?>"/>
</td>
</tr>
</table><br>
<table width="600" border="1" id="tbExp">
<tr>
<td><div align="center">ลำดับ</div></td>
<td><div align="center">รหัสเวชภัณฑ์</div></td>
<td><div align="center">ชื่อเวชภัณฑ์</div></td>
<td><div align="center">คงเหลือ</div></td>
<td><div align="center">หน่วย</div></td>
<td><div align="center">จำนวนเบิก</div></td>
<td><div align="center">เลือก</div></td>
</tr>
</table>
<br/><br/><br/><br/><br/>
<input type="hidden" name="hdnMaxLine" value="0">
<input name="btnAdd" type="button" id="btnAdd" value="+" onClick="CreateNewRow();">
<input name="btnDel" type="button" id="btnDel" value="-" onClick="RemoveRow();">
<input type="submit" name="btnSubmit" value="Submit">
</form>
โค๊ด insert ข้อมูลครับ
Code (PHP)
<?
session_start();
?>
<?php
$con = mysql_pconnect('localhost','root','12345');
if (!$con)
{
echo "Can't connect to database";
}
mysql_select_db(medical_db, $con);
$sql="INSERT INTO payequip (per_id, pay_date) VALUES('$_POST[per_id]','$_POST[pay_date]')";
if (!mysql_query($sql,$con))
{
echo "Error Query";
}
else {
echo "<h4> Save 1 Successful.";
}
$sql1="INSERT INTO payequip_detail (pay_id, me_id, amount) VALUES('$_POST[pay_id]','$_POST[me_id]','$_POST[amount]')";
echo $sql1;
if (!mysql_query($sql1,$con))
{
echo "Error Query1";
}
else {
echo "<h4> Save 2 Successful.";
}
mysql_close($con)
?>
พี่ๆช่วยหน่อยครับว่ามันผิดตรงไหนครับ ตรงฐานข้อมูล หรือการ query ครับ
แล้วแก้ยังไงครับ ผมทำหลายรอบแล้วครับ มืดแปดด้านแล้วครับ
รบก่วนหน่อยครับ ขอบคุณครับ
Tag : PHP, MySQL
|
|
 |
 |
 |
 |
Date :
2012-02-09 17:28:16 |
By :
Mr.mee |
View :
2163 |
Reply :
51 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตัวอย่าง
Code (PHP)
$sql="INSERT INTO payequip (per_id, pay_date) VALUES(".$_POST[per_id].",'".$_POST[pay_date]."')";
$rs = mysql_query($sql,$con);
if (!$rs)
echo "Error Query";
}
else {
echo "<h4> Save 1 Successful.";
}
$sql1="INSERT INTO payequip_detail (pay_id, me_id, amount) VALUES(".$_POST[pay_id].",'".$_POST[me_id]."',".$_POST[amount].")";
$rs = mysql_query($sql1,$con);
if (!$rs)
{
echo "Error Query1";
}
else {
echo "<h4> Save 2 Successful.";
}
ลองดูครับ
|
 |
 |
 |
 |
Date :
2012-02-09 19:15:51 |
By :
Thep |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณสำหรับคำแนะนำครับ แต่ลองไปแล้วครับ
มันฟ้องมาว่า Error Query1 ครับ รู้สึกว่าตารางที่สองข้อมูลจะไม่เข้าครับ
|
 |
 |
 |
 |
Date :
2012-02-09 21:19:26 |
By :
Mr.mee |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
    
|
 |
 |
 |
 |
Date :
2012-02-10 17:27:11 |
By :
Mr.mee |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เริ่มจากฐานข้อมูลก่อนเลยนะครับ...
เทเบิลที่ 2 payequip_detail น่าจะใช้คีย์ 2 ตัวร่วมกันนะ ครับ คือ pay_ID กับ p_id
เวลา insert ก็ insert พร้อมกันได้เลยครับ
Code (PHP)
$sql="INSERT INTO payequip (per_id, pay_date) VALUES('$_POST[per_id]','$_POST[pay_date]')";
$query=mysql_query($conn,$sql);
$sql1="INSERT INTO payequip_detail (pay_id, me_id, amount) VALUES('$_POST[pay_id]','$_POST[me_id]','$_POST[amount]')";
$query=mysql_query($conn,$sql1);
echo "successful";
ลองดูนะครับ..
|
 |
 |
 |
 |
Date :
2012-02-10 17:42:00 |
By :
tomonaha |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
$sql1="INSERT INTO payequip_detail (pay_id, me_id, amount) VALUES( '{$_POST["pay_id"]}' ,'{$_POST["me_id"]}' , '{$_POST["amount"]}' )";
|
 |
 |
 |
 |
Date :
2012-02-10 23:20:42 |
By :
Unidentier |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เอาใหม่ครับ
Code (PHP)
$sql1="INSERT INTO payequip_detail (pay_id, me_id, amount) VALUES( '{$_POST["pay_id"]}' ,'{$_POST["me_id"]}' , '{$_POST["amount"]}' )";
mysql_query($sql1,$con) or die(mysql_error()."<br />".$sql);
echo "<h4> Save 2 Successful.</h4>";
|
 |
 |
 |
 |
Date :
2012-02-10 23:25:44 |
By :
Unidentier |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณพี่ๆสำหรับทุกคำแนะนำครับ
แต่ผมลองทำทุกวิธีทำพี่แนะนำแล้วครับ
มันติดที่ตารางที่สองครับ ยังไม่ยอมเข้าสักทีครับ
จนปัญญาครับ
โค๊ดที่แก้ใหม่ล่าสุดครับ
Code (PHP)
<?php
$con = mysql_pconnect(DB_SERVER,DB_USERNAME,DB_PASSWORD);
if (!$con)
{
echo "Can't connect to database";
}
mysql_select_db(DB_DATABASE_NAME, $con);
$sql="INSERT INTO payequip (per_id, pay_date) VALUES('$_POST[per_id]','$_POST[pay_date]')";
mysql_query($sql,$con) or die(mysql_error()."<br />".$sql);
echo "<h4> Save 2 Successful.</h4>";
$sql1="INSERT INTO payequip_detail (pay_id, me_id, amount) VALUES( '{$_POST["pay_id"]}' ,'{$_POST["me_id"]}' , '{$_POST["amount"]}' )";
mysql_query($sql1,$con) or die(mysql_error()."<br />".$sql1);
echo "<h4> Save 2 Successful.</h4>";
mysql_close($con)
?>
|
 |
 |
 |
 |
Date :
2012-02-11 00:20:19 |
By :
Mr.mee |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่มี error ออกมาเลยเหรอครับ
|
 |
 |
 |
 |
Date :
2012-02-11 02:26:29 |
By :
Unidentier |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
or die(mysql_error() แล้วก็น่าจะมี Error น่ะครับ 
|
 |
 |
 |
 |
Date :
2012-02-11 06:56:40 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมลองให้มัน echo บันทัดที่มัน insert ครับทั้งสองเลยครับ
แบบนี้ครับ
Code (PHP)
<?php
$con = mysql_pconnect(DB_SERVER,DB_USERNAME,DB_PASSWORD);
if (!$con)
{
echo "Can't connect to database";
}
mysql_select_db(DB_DATABASE_NAME, $con);
$sql="INSERT INTO payequip (per_id, pay_date) VALUES('$_POST[per_id]','$_POST[pay_date]')";
echo $sql;
mysql_query($sql,$con) or die(mysql_error()."<br />".$sql);
echo "<h4> Save 2 Successful.</h4>";
$sql1="INSERT INTO payequip_detail (pay_id, me_id, amount) VALUES( '{$_POST["pay_id"]}' ,'{$_POST["me_id"]}' , '{$_POST["amount"]}' )";
echo $sql1;
mysql_query($sql1,$con) or die(mysql_error()."<br />".$sql1);
echo "<h4> Save 2 Successful.</h4>";
mysql_close($con)
?>
ผลลัพธ์ที่ได้ครับ

ตอน insert ครั้งที่สองมันไม่อ่านข้อมูลครับ
|
 |
 |
 |
 |
Date :
2012-02-11 11:36:31 |
By :
Mr.mee |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมรู้แล้วล่ะ
per_id และ pay_date มันเป็นตัวแปรแบบ static ทีมีในฟอร์มตั้งแต่ต้น
แต่ pay_id ,me_id, amount มันเป็นตัวที่ถูกเจนเนเรตด้วยจาวาสคริปต์ครับ
ผมเห็นบรรทัดนี้ครับ
newCell.innerHTML = "<center><INPUT TYPE=\"TEXT\" SIZE=\"5\" NAME=\"me_id"+intLine+"\" ID=\"me_id"+intLine+"\" VALUE=\"\"></center>";
คิดว่าตัวแปรมันต้องออกมาในแนวนี้แน่ๆ
pay_id1 ,me_id1, amount1
pay_id2 ,me_id2, amount2
|
 |
 |
 |
 |
Date :
2012-02-11 12:15:39 |
By :
Unidentier |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แล้วต้องแก้ยังไงครับ ถึงจะให้มันเข้าในฐานข้อมูลครับ
ยังงงอยู่เลยครับ
|
 |
 |
 |
 |
Date :
2012-02-11 12:23:26 |
By :
Mr.mee |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
SIZE=\"5\" NAME=\"me_id"+intLine+"\"
SIZE=\"5\" NAME=\"me_id[]"
NAME=\"me_name"+intLine+"\"
NAME=\"me_name[]"
ทุกอันที่เป็น intLine เปลี่ยนให้เป็นตามตัวอย่างข้างบนให้หมด
แล้วเวลา insert ก็วนลูปซะด้วย for
Code (PHP)
for($j=0; $j<count($_POST["me_id"]); $j++){
$sql1="INSERT INTO payequip_detail (pay_id, me_id, amount) VALUES( '{$_POST["pay_id"][$j]}' ,'{$_POST["me_id"][$j]}' , '{$_POST["amount"][$j]}' )";
mysql_query($sql1,$con) or die(mysql_error()."<br />".$sql1);
echo "<h4> Save 2 Successful.</h4>";
}
|
 |
 |
 |
 |
Date :
2012-02-11 12:34:30 |
By :
Unidentier |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลอง
echo $_POST["pay_id"];
echo $_POST["me_id"];
echo $_POST["amount"];
มาดูครับว่ามันได้ค่าหรือป่าว ถ้าไม่มี ลองไป check ที่จาวาสคริปต์ดูครับ
ถ้าไม่ได้อีกลองสร้างตัวแปรมารับก่อนครับแล้วลอง echo มาดูอีกที
|
 |
 |
 |
 |
Date :
2012-02-11 12:38:03 |
By :
slurpee55555 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
สำคัญน่ะครับ การอ่านโค้ดให้เป็นเนี่ย ถ้าเราต้องมารอการดีบักอย่างเดียวผมว่า
มันจะใช้เวลามากโขอยู่ อยากให้น้องๆอ่านโค้ดให้เป็นครับ
|
 |
 |
 |
 |
Date :
2012-02-11 12:43:33 |
By :
Unidentier |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าอย่างนั้น แก้ไขใหม่อีกรอบครับ
Code (PHP)
for($j=0; $j<$_POST["hdnMaxLine"]; $j++){
$sql1="INSERT INTO payequip_detail (pay_id, me_id, amount) VALUES( '{$_POST["pay_id".$j]}' ,'{$_POST["me_id".$j]}' , '{$_POST["amount".$j]}' )";
mysql_query($sql1,$con) or die(mysql_error()."<br />".$sql1);
echo "<h4> Save 2 Successful.</h4>";
}
|
 |
 |
 |
 |
Date :
2012-02-11 13:58:41 |
By :
Unidentier |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พี่ครับ pay_id มันไม่ดึงมาครับ ตอนนี้ตาราง payequip_detail ผมกำหนด PK สองตัวครับ คือ pay_id และ p_id ครับ
p_id กำหนดรับแบบ auto ครับ มันเกี่ยวกันไหมครับ
|
 |
 |
 |
 |
Date :
2012-02-11 14:15:55 |
By :
Mr.mee |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อ้อผมแก้เมื่อกี้ช่อง NAME อย่างเดียวครับ ช่อง ID ไม่ได้แก้ที
เฮอๆๆ ประมาณจริงๆไม่ตรวจสอบให้ดีก่อน 
|
 |
 |
 |
 |
Date :
2012-02-11 14:22:18 |
By :
saimee |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เอาโค้ดชุดปัจจุบันมาวางอีกทีน่ะครับ
|
 |
 |
 |
 |
Date :
2012-02-11 14:23:58 |
By :
Unidentier |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
หน้าฟอร์มหลักครับ NewOrderForm.php
Code (PHP)
<?
mysql_connect('localhost','root','12345');
mysql_select_db(medical_db);
$strSQL = "SELECT * FROM person p
JOIN department s ON p.depart_id=s.depart_id
WHERE per_id = '".$_SESSION['per_id']."' ";
$objQuery = mysql_query($strSQL);
$objResult = mysql_fetch_array($objQuery);
?>
<?
mysql_connect('localhost','root','12345');
mysql_select_db(medical_db);
$strSQL = "SELECT * FROM unit";
$objQuery = mysql_query($strSQL);
?>
<script language="javascript">
function OpenPopup(intLine)
{
window.open('page-fragment/order/OrderPopUpList.php?Line='+intLine,'myPopup','width=650,height=220,toolbar=0, menubar=0,location=0,status=1,scrollbars=1,resizable=1,left=0,top=0');
}
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 No ***//
newCell = newRow.insertCell(0);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center>"+intLine+"</center>";
//*** Column MedicalID ***//
newCell = newRow.insertCell(1);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center><INPUT TYPE=\"TEXT\" SIZE=\"5\" NAME=\"me_id[]" ID=\"me_id"+intLine+"\" VALUE=\"\"></center>";
//*** Column Name ***//
newCell = newRow.insertCell(2);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center><INPUT TYPE=\"TEXT\" SIZE=\"10\" NAME=\"me_name[]" ID=\"me_name"+intLine+"\" VALUE=\"\"></center>";
//*** Column Stock ***//
newCell = newRow.insertCell(3);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center><INPUT TYPE=\"TEXT\" SIZE=\"3\" NAME=\"stock[]" ID=\"stock"+intLine+"\" VALUE=\"\"></center>";
//*** Column Unit ***//
newCell = newRow.insertCell(4);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center><INPUT TYPE=\"TEXT\" SIZE=\"5\" NAME=\"unit_id[]" ID=\"unit_id"+intLine+"\" VALUE=\"\"></center>";
//*** Column จำนวนเบิก ***//
newCell = newRow.insertCell(5);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center><INPUT TYPE=\"TEXT\" SIZE=\"5\" NAME=\"amount[]" ID=\"amount"+intLine+"\" VALUE=\"\"></center>";
//*** Column 7 ***//
newCell = newRow.insertCell(6);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
//newCell.setAttribute("OnClick", "OpenPopup('"+intLine+"')");
newCell.innerHTML = "<center><INPUT TYPE=\"BUTTON\" NAME=\"btnPopup_[]" ID=\"btnPopup_"+intLine+"\" VALUE=\"...\" OnClick=\"OpenPopup('"+intLine+"')\"></center>";
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;
}
}
</script>
<body OnLoad="CreateNewRow();">
<form name="frmMain" method="post" action="ActionController.php?action=order/CreateOrderAction">
<!--เวลาเก็บ /CreateOrderAction -->
<table>
<tr>
<input type="hidden" name="per_id" id="per_id" value="<?=$objResult['per_id']?>" />
<td>
<p>แผนก : </p>
<input type="text" name="depart_name" id="depart_name" class="texPay" value="<?=$objResult["depart_name"];?>"/>
</td>
<td>
<p>วันที่เบิก : </p>
<input type="text" name="pay_date" id="pay_date" class="texPay" value="<?=date('Y-m-d')?>"/>
</td>
</tr>
</table><br>
<table width="600" border="1" id="tbExp">
<tr>
<td><div align="center">ลำดับ</div></td>
<td><div align="center">รหัสเวชภัณฑ์</div></td>
<td><div align="center">ชื่อเวชภัณฑ์</div></td>
<td><div align="center">คงเหลือ</div></td>
<td><div align="center">หน่วย</div></td>
<td><div align="center">จำนวนเบิก</div></td>
<td><div align="center">เลือก</div></td>
</tr>
</table>
<br/><br/><br/><br/><br/>
<input type="hidden" name="hdnMaxLine" value="0">
<input name="btnAdd" type="button" id="btnAdd" value="+" onClick="CreateNewRow();">
<input name="btnDel" type="button" id="btnDel" value="-" onClick="RemoveRow();">
<input type="submit" name="btnSubmit" value="Submit">
</form>
หน้าเพิ่มข้อมูลครับ CreateOrderAction.php
<Code (PHP)
?php
$con = mysql_pconnect('localhost','root','12345');
if (!$con)
{
echo "Can't connect to database";
}
mysql_select_db(medical_db, $con);
$sql="INSERT INTO payequip (per_id, pay_date) VALUES('$_POST[per_id]','$_POST[pay_date]')";
mysql_query($sql,$con) or die(mysql_error()."<br />".$sql);
echo "<h4> Save 1 Successful.</h4>";
for($j=0; $j<$_POST["hdnMaxLine"]; $j++){
$sql1="INSERT INTO payequip_detail (pay_id, me_id, amount) VALUES( '{$_POST["pay_id".$j]}' ,'{$_POST["me_id".$j]}' , '{$_POST["amount".$j]}' )";
mysql_query($sql1,$con) or die(mysql_error()."<br />".$sql1);
echo "<h4> Save 2 Successful.</h4>";
}
mysql_close($con)
?>
แค่นี้ครับ
|
 |
 |
 |
 |
Date :
2012-02-11 14:51:17 |
By :
Mr.mee |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
for($j=0; $j<$_POST["hdnMaxLine"]; $j++){
$sql1="INSERT INTO payequip_detail (pay_id, me_id, amount) VALUES( '{$_POST["pay_id".$j]}' ,'{$_POST["me_id".$j]}' , '{$_POST["amount".$j]}' )";
mysql_query($sql1,$con) or die(mysql_error()."<br />".$sql1);
echo "<h4> Save 2 Successful.</h4>";
}
แก้ไขจากข้างบนเป็น
Code (PHP)
for($j=0; $j<$_POST["hdnMaxLine"]; $j++){
$sql1="INSERT INTO payequip_detail (pay_id, me_id, amount) VALUES( '{$_POST["pay_id"][$j]}' ,'{$_POST["me_id"][$j]}' , '{$_POST["amount"][$j]}' )";
mysql_query($sql1,$con) or die(mysql_error()."<br />".$sql1);
echo "<h4> Save 2 Successful.</h4>";
}
แต่ผมดูจากโค้ดทั้งหมดแล้ว ผมไม่เห็น input box ตัวไหนมีชื่อเป็น "pay_id" เลยน่ะครับ
คุณเลยไม่มีอะไรมาใส่ลงตารางข้อมูลล่ะครับ
|
 |
 |
 |
 |
Date :
2012-02-11 15:05:14 |
By :
Unidentier |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
pay_id เป็น PK ของตาราง payequip ครับคือมันรับแบบ auto ครับผมเลยไม่ทำ textbox ครับ
แล้วอยากให้ดึง pay_id มาใส่ในตาราง payequip_detail ครับ ประมาณนี้ครับ
|
 |
 |
 |
 |
Date :
2012-02-11 15:13:52 |
By :
saimee |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ได้ครับ ใช้ฟังก์ชั่น mysql_insert_id() มาช่วยครับ
Code (PHP)
mysql_select_db(medical_db, $con);
$sql="INSERT INTO payequip (per_id, pay_date) VALUES('$_POST[per_id]','$_POST[pay_date]')";
mysql_query($sql,$con) or die(mysql_error()."<br />".$sql);
for($j=0; $j<$_POST["hdnMaxLine"]; $j++){
$sql1="INSERT INTO payequip_detail (pay_id, me_id, amount) VALUES( '".mysql_insert_id()."' ,'{$_POST["me_id"][$j]}' , '{$_POST["amount"][$j]}' )";
mysql_query($sql1,$con) or die(mysql_error()."<br />".$sql1);
echo "<h4> Save 2 Successful.</h4>";
}
|
 |
 |
 |
 |
Date :
2012-02-11 15:19:35 |
By :
Unidentier |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พอเวลาเพิ่มข้อมูลหลายตัวแล้ว พอตัวสุดท้ายมันขึ้น รหัสถัดไปครับ
ไม่ได้ขึ้นรหัสเดียวกันหมดครับ ทั้งๆที่รหัสถัดไปยังไม่ได้เพิ่มทีครับ
เช่น pay_id p_id
0001 1
0001 2
0001 3
0002 1
ประมาณนี้ครับ
จากโค๊ดด้านล้างนี้ครับ
Code (PHP)
$sql="INSERT INTO payequip (per_id, pay_date) VALUES('$_POST[per_id]','$_POST[pay_date]')";
mysql_query($sql,$con) or die(mysql_error()."<br />".$sql);
for($j=1; $j<=$_POST["hdnMaxLine"]; $j++){
$sql1="INSERT INTO payequip_detail (pay_id, me_id, amount) VALUES( '".mysql_insert_id()."' ,'{$_POST["me_id".$j]}' , '{$_POST["amount".$j]}' )";
mysql_query($sql1,$con) or die(mysql_error()."<br />".$sql1);
echo "<h4> Save 2 Successful.</h4>";
}
|
 |
 |
 |
 |
Date :
2012-02-11 15:47:50 |
By :
saimee |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ข้อมูลจากฐานครับ


|
 |
 |
 |
 |
Date :
2012-02-11 15:54:02 |
By :
saimee |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ได้แล้วครับ ดีใจมากเลยครับ
ขอบคุณพี่ๆทุกๆคนมากๆเลยน่ะครับ
ทำให้ผมได้กำลังใจอย่างมากที่จะทำโปรเจ็กต่อครับ
เสร็จโปรเซสนี้โปรเจ็กของผมก็เหลือประมาณ 30% ครับ
คงจะทันเดือนมีนานี้แน่ๆครับ(โปรเจ็กตัวแรกของผมครับ)
|
 |
 |
 |
 |
Date :
2012-02-11 19:44:52 |
By :
saimee |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอให้มีกำลังใจต่อไป อย่ายอมแพ้ง่ายๆน่ะครับ
ทุกปัญหาที่พบเจอล้วนเป็นประสบการณ์ที่มีค่า
|
 |
 |
 |
 |
Date :
2012-02-11 20:58:08 |
By :
sakuraei |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
for($j=0; $j<$_POST["hdnMaxLine"]; $j++){
$sql1="update stock set stock=stock-{$_POST["amount"][$j]} where ...... ";
mysql_query($sql1,$con) or die(mysql_error()."<br />".$sql1);
echo "<h4> Save 2 Successful.</h4>";
}
|
 |
 |
 |
 |
Date :
2012-02-15 16:27:10 |
By :
sakuraei |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
$last_id = mysql_insert_id();
for($j=1; $j<=$_POST["hdnMaxLine"]; $j++){
$sql1="INSERT INTO payequip_detail (pay_id, me_id, amount) VALUES( '$last_id' ,'{$_POST["me_id".$j]}' , '{$_POST["amount".$j]}' )";
mysql_query($sql1,$con) or die(mysql_error()."<br />".$sql1);
$sql2="update medical set stock=stock-{$_POST["amount"][$j]} where me_id={$_POST["me_id"]}";
mysql_query($sql2,$con) or die(mysql_error()."<br />".$sql2);
echo "<h4> Save 2 Successful.</h4>";
}
|
ประวัติการแก้ไข 2012-02-15 16:53:22
 |
 |
 |
 |
Date :
2012-02-15 16:52:27 |
By :
sakuraei |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ทำไมโค้ดสี ทำงานได้บ้าง ไม่ได้บ้างล่ะครับ
.... where me_id=".$_POST["me_id"]; หรือ
..... where me_id='{$_POST["me_id"]}' ";หรือ
..... where me_id='".$_POST["me_id"]."' ";
|
 |
 |
 |
 |
Date :
2012-02-15 16:56:20 |
By :
sakuraei |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
$last_id = mysql_insert_id();
for($j=1; $j<=$_POST["hdnMaxLine"]; $j++){
$sql1="INSERT INTO payequip_detail (pay_id, me_id, amount) VALUES( '$last_id' ,'{$_POST["me_id".$j]}' , '{$_POST["amount".$j]}' )";
mysql_query($sql1,$con) or die(mysql_error()."<br />".$sql1);
$sql2="update medical set stock=stock-{$_POST["amount".$j]} where me_id={$_POST["me_id.$j"]}";
mysql_query($sql2,$con) or die(mysql_error()."<br />".$sql2);
echo "<h4> Save 2 Successful.</h4>";
}
|
 |
 |
 |
 |
Date :
2012-02-15 17:14:09 |
By :
sakuraei |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ทำไมคุณไม่ดูครับ update medical set stock=stock-5 where me_id= ปัญหาคือมันไม่มีค่า id ครับ มันก็โชว์ให้เห็นอยู่ครับ
อันที่จริงควรจะใส่ 'xx' ให้ด้วยครับ กรณีที่ไม่มีค่าจะได้ไม่ error
Code (PHP)
$sql2="update medical set stock=stock-{$_POST["amount"][$j]} where me_id='".$_POST["me_id"]."'";
|
 |
 |
 |
 |
Date :
2012-02-15 17:27:30 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
{$_POST["amount"][$j]} ก็ไม่มีค่า
$_POST["me_id"] ก็ไม่มีค่า
จาก error ก็น่าจะดูออกครับ 
|
 |
 |
 |
 |
Date :
2012-02-15 17:40:59 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แล้วต้อง select ก่อนหรือครับ
|
 |
 |
 |
 |
Date :
2012-02-15 17:43:44 |
By :
Mr.mee |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
?.......  
|
 |
 |
 |
 |
Date :
2012-02-15 22:18:34 |
By :
Mr.mee |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เอาแบบนี้ครับ แก้ให้ใหม่ล่ะ
Code (PHP)
$last_id = mysql_insert_id();
for($j=1; $j<=$_POST["hdnMaxLine"]; $j++){
$sql1="INSERT INTO payequip_detail (pay_id, me_id, amount) VALUES( '$last_id' ,'{$_POST["me_id".$j]}' , '{$_POST["amount".$j]}' )";
mysql_query($sql1,$con) or die(mysql_error()."<br />".$sql1);
$sql2="update medical set stock=stock-'{$_POST["amount".$j]}' where me_id='{$_POST["me_id".$j]}' ";
mysql_query($sql2,$con) or die(mysql_error()."<br />".$sql2);
echo "<h4> Save 2 Successful.</h4>";
}
|
ประวัติการแก้ไข 2012-02-15 22:25:57
 |
 |
 |
 |
Date :
2012-02-15 22:25:03 |
By :
sakuraei |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อันดับแรกคุณต้องรู้ก่อนว่าจะ update ข้อมูลอะไรบ้าง ต่อมาคุณก็เตรียมข้อมูลที่จะเอาไป update โดยจะรับค่ามาหรือเก็บไว้ไในตัวแปรก็ได้ ควร check ก่อนนะครับว่า ได้ค่ามาหรือป่าว อาจจะ ลอง echo มาดูก่อนก็ได้ว่ามันมีค่าหรือป่าว ถ้ามีก็ทำการ update ได้เลย แต่ถ้าไม่มีต้องไปล่ดูตอนนำค่ามาครับ ดูจาก error syntax มันผิดครับ อาจเกิดจากหลายสาเหตุ เช่น คำสั่ง sql มีถูกต้อง อาจมีเคาะ เป็นต้น หรือคำสั่งถูกแต่ค่าไม่มีก็อาจทำให้รูปแบบคำสั่งผิดครับ หรือ fild ในฐานข้อมูลไม่สมารถใส่ค่าว่างได้ ก็มีส่วนนะครับ เพราะเท่าที่ดูแล้วมัน where ที่ค่าว่าง ตัว $_POST["me_id"] มันไม่มีค่าครับ ลองกับไปดูครับว่าได้ส่งค่ามาหรือป่าว หรือไม่งั้นสร้างตัวแปนมารับค่าก่อนครับ แล้วค่อยเอาตัวแปรไปใช้อีกทีครับ เช่น $mid=$_POST["me_id"]; หลังจากนั้นก็เรียกใช $mid ได้เลยครับ ลองดูครับ ลองไล่ที่ละส่วนดูไม่น่ายากครับ  
|
 |
 |
 |
 |
Date :
2012-02-15 22:37:04 |
By :
slurpee55555 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อ้อได้แล้วครับ สำเร็จแล้วครับ
ขอบคุณพี่ๆทุกคำแนะนำมากๆเลยน่ะครับ
ใช่จริงด้วยครับ ค่า me_id มันเป็นค่าว่างครับ
กำหนด ให้รับค่าครับ where me_id='{$_POST["me_id".$j]}' ";
|
 |
 |
 |
 |
Date :
2012-02-15 23:05:33 |
By :
Mr.mee |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เฮ้อ.... ผ่านซะทีน่ะ โชคดีให้ได้เกรดสวยๆน่ะ
|
 |
 |
 |
 |
Date :
2012-02-15 23:21:27 |
By :
sakuraei |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ยินดีด้วยครับ ปัญหานี้เป็นปัญหาที่ต้องจดจำไว้ครับ เพราะจะต้องเจอมันอีกในโอกาศหน้า ฮ่าๆๆๆๆ   
|
 |
 |
 |
 |
Date :
2012-02-15 23:32:58 |
By :
slurpee55555 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|