 |
PHP Insert // กด Summit แล้วไม่บันทึก ไม่รู้ว่าผิดตรงส่วนไหน ช่วยดู Code ให้ด้วยนะคะ Please |
|
 |
|
|
 |
 |
|
PHP Insert // กด Summit แล้วไม่บันทึก ไม่รู้ว่าผิดตรงส่วนไหน ช่วยดู Code ให้ด้วยนะคะ Please  
Code (PHP)
<? include ('conn.php');
$std =("SELECT MAX(Std_value)as MAXID FROM standardvalue "); // query อ่านค่า id สูงสุด
// คืนค่า id ที่ insert สูงสุด
$id1 = ("SELECT MAX(LotID) as MAXID FROM qc_record "); // query อ่านค่า id สูงสุด
// คืนค่า id ที่ insert สูงสุด
$sql = "INSERT INTO inspection_rec";
$sql .= "(Pressure,Weight,PerA,PerB,PerC,Diff,MoldShape,MoldRing,Condition,Std_value,LotID)";
$sql .= "VALUES";
$sql .= "('".$_POST['txtair']."','".$_POST['txtweight']."','".$_POST['txta']."','".$_POST['txtb']."'";
$sql .= ",'".$_POST['txtc']."','".$_POST['txtdiff']."','".$_POST['Rdomold']."','".$_POST['selectring2']."'";
$sql .= ",'".$_POST['selectcondition2']."','".$std."','".$id1."')";
mysql_query("SET NAMES utf8");
$query = mysql_query($sql);
if ($query){
echo "success for save";
}
else
{
echo "not save";
}
Tag : PHP, MySQL, HTML/CSS, JavaScript, Tablets
|
|
 |
 |
 |
 |
Date :
2013-06-04 13:53:22 |
By :
PattiraPikul |
View :
955 |
Reply :
15 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เขียนโค๊ดได้น่ากลัวจริงๆ จับใส่เดื้อๆเลย
ลอง
Code (PHP)
<? include ('conn.php');
$std =("SELECT MAX(Std_value)as MAXID FROM standardvalue "); // query อ่านค่า id สูงสุด
// คืนค่า id ที่ insert สูงสุด ตรงนี้ดึงมาทำอะไร
$id1 = ("SELECT MAX(LotID) as MAXID FROM qc_record "); // query อ่านค่า id สูงสุด
// คืนค่า id ที่ insert สูงสุด ตรงนี้ด้วย
$sql = "INSERT INTO inspection_rec";
$sql .= "(Pressure,Weight,PerA,PerB,PerC,Diff,MoldShape,MoldRing,Condition,Std_value,LotID)";
$sql .= "VALUES";
$sql .= "('".$_POST['txtair']."','".$_POST['txtweight']."','".$_POST['txta']."','".$_POST['txtb']."'";
$sql .= ",'".$_POST['txtc']."','".$_POST['txtdiff']."','".$_POST['Rdomold']."','".$_POST['selectring2']."'";
$sql .= ",'".$_POST['selectcondition2']."','".$std."','".$id1."')";
echo $sql;//echo ค่า ออกมาก่อนสิ
mysql_query("SET NAMES utf8");
$query = mysql_query($sql);
if ($query){
echo "success for save";
}
else
{
echo "not save";
}
|
 |
 |
 |
 |
Date :
2013-06-04 14:00:17 |
By :
dekkuza |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
field อาจจะไม่ตรงกันก็ได้ค่ะ
กันพลาด ลอง insert จาก phpmyadmin ดูนะคะ พอมัน return sql script ก็ก๊อปเอาไปแทนค่าใหม่
หรือจากโค้ด อาจจะตกหล่นบาง syntax ก็ได้ค่ะ เพราะใส่ตรงๆ แบบนี้มันลายตา เวลาจะไล่ดูมันลำบาก
|
 |
 |
 |
 |
Date :
2013-06-04 14:07:49 |
By :
pharkram |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เพราะ $std และ $id1 เป็น Sub Query ที่จะใช้เป็นค่าใน VALUES
จึงจำเป็นต้องมีวงเล็บครอบ
แต่อย่างที่เห็น จขกท. เอาวงเล็บไว้นอกสตริง จึงไม่มีความหมายใดๆ ในค่าของสตริง
เมื่อเอาไปใช้ใน VALUES มันจะกลายเป็น SQL ที่ผิดไวยากรณ์
$std =("SELECT MAX(Std_value)as MAXID FROM standardvalue "); // query อ่านค่า id สูงสุด
// คืนค่า id ที่ insert สูงสุด
$id1 = ("SELECT MAX(LotID) as MAXID FROM qc_record "); // query อ่านค่า id สูงสุด
// คืนค่า id ที่ insert สูงสุด
ต้องเอาวงเล็บไว้ในค่าของสตริง
$std ="(SELECT MAX(Std_value)as MAXID FROM standardvalue)"; // query อ่านค่า id สูงสุด
// คืนค่า id ที่ insert สูงสุด
$id1 = "(SELECT MAX(LotID) as MAXID FROM qc_record)"; // query อ่านค่า id สูงสุด
// คืนค่า id ที่ insert สูงสุด
|
 |
 |
 |
 |
Date :
2013-06-04 14:39:10 |
By :
cookiephp |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แต่อย่างที่เห็น การเขียน query ของจขกท.ก็เป็นแบบที่อ่านยาก เนื่องจากใช้การเชื่อมต่อสตริง
ลองมาเขียนแบบนี้ดูมั้ยครับ
ใช้ sprintf() กับค่าที่รับมาจากภายนอก
และไม่ต้องใช้ตัวแปร หากเป็นค่าที่ไม่เปลี่ยนแปลง (ในที่นี้คือ sub query)
$sql = sprintf(
"
INSERT INTO inspection_rec
(
Pressure,
Weight,
PerA,
PerB,
PerC,
Diff,
MoldShape,
MoldRing,
Condition,
Std_value,
LotID
)
VALUES
(
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
(SELECT MAX(Std_value) FROM standardvalue),
(SELECT MAX(LotID) FROM qc_record)
)
",
mysql_real_escape_string($_POST['txtair']),
mysql_real_escape_string($_POST['txtweight']),
mysql_real_escape_string($_POST['txta']),
mysql_real_escape_string($_POST['txtb']),
mysql_real_escape_string($_POST['txtc']),
mysql_real_escape_string($_POST['txtdiff']),
mysql_real_escape_string($_POST['Rdomold']),
mysql_real_escape_string($_POST['selectring2']),
mysql_real_escape_string($_POST['selectcondition2'])
);
mysql_query("SET NAMES utf8");
$query = mysql_query($sql);
if ($query){
echo "success for save";
}
else
{
echo "not save";
}
|
 |
 |
 |
 |
Date :
2013-06-04 14:43:01 |
By :
cookiephp |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอน query ให้ตรวจ error ด้วยครับ
Code (PHP)
$query = mysql_query($sql) or die (mysql_error() . " SQL = [ $sql ]");
แล้วเอา error message มาดู
|
ประวัติการแก้ไข 2013-06-05 10:28:13
 |
 |
 |
 |
Date :
2013-06-05 10:26:48 |
By :
cookiephp |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมลองแปลง แบบง่าย ๆ ไม่ได้ test นะ
Code (PHP)
<? include ('conn.php');
$s1 ="(SELECT MAX(Std_value) as M1 FROM standardvalue)";
$s2 ="(SELECT MAX(LotID) as M2 FROM qc_record)";
$q1=mysql_query($s1)or die(mysql_error());
$q2=mysql_query($s2)or die(mysql_error());
$row1 = mysql_fetch_assoc($q1);
$row2 = mysql_fetch_assoc($q2);
$M1=$row1['M1'];
$M2 = $row2['M2'];
$air = $_POST['txtair'];
$weight = $_POST['txtweight'];
$a = $_POST['txta'];
$b = $_POST['txtb'];
$c = $_POST['txtc'];
$diff = $_POST['txtdiff'];
$rd = $_POST['Rdomold'];
$s1 = $_POST['selectring2'];
$s2 = $_POST['selectcondition2'];
$strSQL = "INSERT INTO inspection_rec";
$strSQL .= "(Pressure,
Weight,
PerA,
PerB,
PerC,
Diff,
MoldShape,
MoldRing,
Condition,
Std_value,
LotID)
VALUES('$air',
'$weight',
'$a',
'$b',
'$c',
'$diff',
'$rd',
'$s1',
'$s2',
'$M1',
'$M2')";
$query = mysql_query($strSQL)or die(mysql_error());
ผมแนะนำ
ยังไงเวลาเขียน ควรเขียน โชว์ error ด้วยเพราะสะดวกต่อการแก้ไขนะครับ
|
 |
 |
 |
 |
Date :
2013-06-05 10:41:07 |
By :
miccoli |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 10 เขียนโดย : cookiephp เมื่อวันที่ 2013-06-05 10:44:31
รายละเอียดของการตอบ ::
Code (PHP)
INSERT INTO inspection_rec(Pressure,Weight,PerA,PerB,PerC,Diff,MoldShape,MoldRing,
Condition,Std_value,LotID)VALUES('','','','','','','','','','S57','L17')You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax to use near 'Condition,Std_value,LotID)VALUES('','','','','','','','','','S57','L17')' at line 1 SQL = [INSERT INTO inspection_rec(Pressure,Weight,PerA,PerB,PerC,Diff,MoldShape,MoldRing,Condition,Std_value,LotID)
VALUES('','','','','','','','','','S57','L17')]
มันแจ้ง error ว่าผิดหลักไวยากรณ์ แต่ก็ดูแล้วนะคะ ก็ตรวจทานดูแล้วก็ไม่เหนมี
|
ประวัติการแก้ไข 2013-06-05 11:29:01
 |
 |
 |
 |
Date :
2013-06-05 11:28:14 |
By :
PattiraPikul |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (SQL)
INSERT INTO inspection_rec(Pressure,Weight,PerA,PerB,PerC,Diff,MoldShape,MoldRing,
Condition,Std_value,LotID)VALUES('','','','','','','','','','S57','L17')You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax to use near 'Condition,Std_value,LotID)VALUES('','','','','','','','','','S57','L17')' at line 1 SQL = [INSERT INTO inspection_rec(Pressure,Weight,PerA,PerB,PerC,Diff,MoldShape,MoldRing,Condition,Std_value,LotID)
VALUES('','','','','','','','','','S57','L17')]
มันแจ้ง error ว่าผิดหลักไวยากรณ์ แต่ก็ดูแล้วนะคะ ก็ตรวจทานดูแล้วก็ไม่เหนมี
|
 |
 |
 |
 |
Date :
2013-06-05 11:56:16 |
By :
PattiraPikul |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ทราบสาเหตุแล้วครับ Condition เป็น reserved word ครับ
เอาใหม่ ต่อไปนี้ อะไรที่เป็นชื่อตาราง หรือชื่อคอลัมน์ ให้ครอบมันด้วย ` นะครับ
แม้มันจะเป็นหรือไม่เป็น reserved word ก็ตามแต่ แต่มั่นใจได้ว่าจะไม่ชนกับ reserved word แน่นอน
$sql = sprintf(
"
INSERT INTO `inspection_rec`
(
`Pressure`,
`Weight`,
`PerA`,
`PerB`,
`PerC`,
`Diff`,
`MoldShape`,
`MoldRing`,
`Condition`,
`Std_value`,
`LotID`
)
VALUES
(
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
(SELECT MAX(`Std_value`) FROM `standardvalue`),
(SELECT MAX(`LotID`) FROM `qc_record`)
)
",
mysql_real_escape_string($_POST['txtair']),
mysql_real_escape_string($_POST['txtweight']),
mysql_real_escape_string($_POST['txta']),
mysql_real_escape_string($_POST['txtb']),
mysql_real_escape_string($_POST['txtc']),
mysql_real_escape_string($_POST['txtdiff']),
mysql_real_escape_string($_POST['Rdomold']),
mysql_real_escape_string($_POST['selectring2']),
mysql_real_escape_string($_POST['selectcondition2'])
);
mysql_query("SET NAMES utf8");
$query = mysql_query($sql);
if ($query){
echo "success for save";
}
else
{
echo "not save";
}
|
ประวัติการแก้ไข 2013-06-05 12:10:17
 |
 |
 |
 |
Date :
2013-06-05 12:09:48 |
By :
cookiephp |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|